在 VB.Net 中侦听 Visio MarkerEvent 事件

Listen for Visio MarkerEvent event in VB.Net

在 MS Visio 中,我有一个表示面积的形状。我希望我的形状在每次更改时更新区域文本。为此,我在形状表中使用了 QUEUEMARKER 函数,其中包含上下文字符串 "Shape is changed" 和 DEPENDSON。因此,每次修改形状时,它都会在 Visio 的事件列表中创建一个 MarkerEvent。 我还有一个 COM 加载项,我想在其中侦听此 "Shape is changed" 事件,以便一旦触发该事件,我想 运行 更新形状区域的函数之一正在修改中。

我在Visio SDK中看到了MarkerEvent和Add Advise的示例并复制了它,但是我无法收听该事件。你能帮我理解我做错了什么吗?

这是我从 Visio SDK 改编的代码:

    Private WithEvents visioApplication As Microsoft.Office.Interop.Visio.Application
Private beginQueuedEvent As Integer
Private endQueuedEvent As Integer
Private betweenMarker As Boolean

Public Sub UseMarker(ByVal markedPage As Microsoft.Office.Interop.Visio.Page)

    ' Get the Application object of the currently running Visio application.
    visioApplication = CType(markedPage.Application, Microsoft.Office.Interop.Visio.Application)

    Try

        betweenMarker = False

        beginQueuedEvent = visioApplication.QueueMarkerEvent("Shape is changed")


    Catch err As System.Runtime.InteropServices.COMException
        System.Diagnostics.Debug.WriteLine(err.Message)
    End Try

End Sub


Private Sub applicationMarkerEventHandler(ByVal theApplication As Microsoft.Office.Interop.Visio.Application, ByVal sequenceNumber As Integer, ByVal context As String) Handles visioApplication.MarkerEvent

    Try

        ' Ignore marker events with no context string
        If Not IsNothing(context) Then
            If context.Length() <> 0 Then

                ' If the value of sequenceNumber is either beginQueuedEvent or
                ' endQueuedEvent, the event results from the calls to the
                ' QueueMarkerEvent method.

                ' Note: betweenMarker is used in the 
                ' shapeAddedToPageEventHandler to process the ShapeAdded
                ' messages fired between the two QueueMarkerEvent calls. 

                If (sequenceNumber = beginQueuedEvent) Then

                    betweenMarker = True
                End If

            End If
        End If

    Catch err As System.Runtime.InteropServices.COMException
        System.Diagnostics.Debug.WriteLine(err.Message)
    End Try

End Sub


Private Sub shapeAddedToPageEventHandler(ByVal addedShape As Microsoft.Office.Interop.Visio.Shape) Handles visioApplication.CellChanged

    Dim dA As Double        'Area value
    Dim dP As Double        'Perimeter value
    Dim sName As String     'Shape name
    Dim xPS As Visio.Shape  'Visio Shape

    Try

        If (betweenMarker) Then

            xPS = Application.ActiveWindow.Selection.PrimaryItem

            If InStr(xPS.Name, ".") > 0 Then
                sName = Left(xPS.Name, InStr(xPS.Name, ".") - 1)
            Else
                sName = xPS.Name
            End If

            Select Case sName
                Case "My Area"
                    dA = xPS.AreaIU
                    dP = xPS.LengthIU

                    xPS.Cells("User.Area").FormulaForceU = dA
                    xPS.Cells("User.Perimeter").FormulaForceU = dP
            End Select

        End If

    Catch err As System.Runtime.InteropServices.COMException
        System.Diagnostics.Debug.WriteLine(err.Message)
    End Try

End Sub

好吧,我自己解决了。

我没有正确引发 Application.MarkerEvent 事件。

Private Sub applicationMarkerEventHandler(ByVal theApplication As Microsoft.Office.Interop.Visio.Application, ByVal sequenceNumber As Integer, ByVal context As String) Handles Application.MarkerEvent

现在有效。