在VB.Net中侦听Visio MarkerEvent事件

在VB.Net中侦听Visio MarkerEvent事件,vb.net,event-handling,visio,Vb.net,Event Handling,Visio,在MS Visio中,我有一个表示面积的形状。我希望我的形状在每次更改时更新区域文本。为此,我在shapesheet中使用了QUEUEMARKER函数和上下文字符串“Shape is changed”以及DEPENDSON。因此,每次修改形状时,它都会在Visio的事件列表中创建一个MarkeEvent。 我还有一个COM外接程序,我想在其中侦听此“Shape is changed”事件,以便在触发该事件后,我想运行一个函数来更新正在修改的形状区域 我在VisioSDK中看到了MarkerEve

在MS Visio中,我有一个表示面积的形状。我希望我的形状在每次更改时更新区域文本。为此,我在shapesheet中使用了QUEUEMARKER函数和上下文字符串“Shape is changed”以及DEPENDSON。因此,每次修改形状时,它都会在Visio的事件列表中创建一个MarkeEvent。 我还有一个COM外接程序,我想在其中侦听此“Shape is changed”事件,以便在触发该事件后,我想运行一个函数来更新正在修改的形状区域

我在VisioSDK中看到了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
Private WithEvents visioApplication作为Microsoft.Office.Interop.Visio.Application
Private beginQueuedEvent为整数
私有endQueuedEvent为整数
作为布尔值的私有中间标记
Public Sub-UseMarker(ByVal将页面标记为Microsoft.Office.Interop.Visio.Page)
'获取当前运行的Visio应用程序的应用程序对象。
visioApplication=CType(markedPage.Application,Microsoft.Office.Interop.Visio.Application)
尝试
betweenMarker=假
beginQueuedEvent=visioApplication.QueueMarkerEvent(“形状已更改”)
捕获错误为System.Runtime.InteropServices.COMException
System.Diagnostics.Debug.WriteLine(错误消息)
结束尝试
端接头
专用子应用程序MarkerEventHandler(ByVal应用程序为Microsoft.Office.Interop.Visio.Application,ByVal sequenceNumber为整数,ByVal上下文为字符串)处理visioApplication.MarkerEvent
尝试
'忽略没有上下文字符串的标记事件
如果不是空(上下文),那么
如果context.Length()为0,则
'如果sequenceNumber的值为beginQueuedEvent或
'endQueuedEvent,事件是调用
'队列标记事件方法。
'注意:中间标记用于
'ShapedDedTopageEventHandler以处理ShapedDedTopageEventHandler
'在两个QueueMarkerEvent调用之间激发的消息。
如果(sequenceNumber=beginQueuedEvent),则
betweenMarker=True
如果结束
如果结束
如果结束
捕获错误为System.Runtime.InteropServices.COMException
System.Diagnostics.Debug.WriteLine(错误消息)
结束尝试
端接头
私有子shapeAddedToPageEventHandler(ByVal将图形添加为Microsoft.Office.Interop.Visio.Shape)处理visioApplication.CellChanged
将dA设置为双“面积值”
将dP调整为双“周长值”
Dim sName作为字符串的形状名称
将xPS暗显为Visio.Shape“Visio Shape”
尝试
如果(在Marker之间)那么
xPS=Application.ActiveWindow.Selection.PrimaryItem
如果InStr(xPS.Name,“.”)大于0,则
sName=Left(xPS.Name,InStr(xPS.Name,“.”-1)
其他的
sName=xPS.Name
如果结束
选择案例sName
案例“我的领域”
dA=xPS.AreaIU
dP=xPS.LengthIU
xPS.Cells(“User.Area”).FormulaForceU=dA
xPS.Cells(“用户周长”).FormulaForceU=dP
结束选择
如果结束
捕获错误为System.Runtime.InteropServices.COMException
System.Diagnostics.Debug.WriteLine(错误消息)
结束尝试
端接头

我自己解决了这个问题

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

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