Visio Guy

Visio Discussions => Programming & Code => Topic started by: Miki on May 22, 2017, 04:47:52 PM

Title: Listen for MarkerEvent in a COM Add-In
Post by: Miki on May 22, 2017, 04:47:52 PM
In Visio, I have a shape that represents area. I want my shape to update the area text every time it is changed. For this, I used QUEUEMARKER function in the shapesheet with a context string "Shape is changed" along with DEPENDSON. So, every time the shape is modified, it creates a MarkerEvent in the eventlist of Visio. I also have a COM Add-in in which I want to listen for this "Shape is changed" event so that once that event is triggered, I want to run one of the function that updates the area of the shape that is being modified.

I saw the example of MarkerEvent and Add Advise in Visio SDK and copied that, but I am not able to listen to the event. Can you please help me understand what am I doing wrong?

I am using VB.Net.

Here is the code:
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 cellChangedEventHandler(ByVal xPS 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

        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 "Wheeled Area", "Terminal Area", "Support Services Area", "CapEx Area", "Open Storage Area", "Covered Storage Area", "RORO Parking 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


Thank you.

- Miki
Title: Re: Listen for MarkerEvent in a COM Add-In
Post by: Miki on May 24, 2017, 04:12:18 PM
I tried to invoke the "MarkerEvent" from startup, but was unsuccessful.
Still unresolved.

-Miki
Title: Re: Listen for MarkerEvent in a COM Add-In
Post by: Yacine on May 24, 2017, 05:08:16 PM
Would a CallThis be acceptable?

Call the macro from the EventXFMod cell in the shapesheet.
LEt the macro update either directly the text, or a field in the shapesheet.
cf attachement.
Title: Re: Listen for MarkerEvent in a COM Add-In
Post by: Miki on May 24, 2017, 05:58:56 PM
CallThis can only be used if my code is in the document or stencil (VBA).

My code is in the Add-in that I created using VB.Net.
Title: Re: Listen for MarkerEvent in a COM Add-In
Post by: Nikolay on May 24, 2017, 07:25:06 PM
Cant you go just with Application.MarkerEvent? Why all those complexities?
https://msdn.microsoft.com/en-us/library/office/ff765486.aspx

Means, it looks like that code you list is is optimized for efficiency.
I think you can first make it work with plain event handler, and then start thinking about other things...

Simple example is in the documentation link above.
Title: Re: Listen for MarkerEvent in a COM Add-In
Post by: Miki on May 24, 2017, 08:23:33 PM
Finally I was able to get this thing working.
I found that I was not able to raise the Application.MarkerEvent event correctly.

I tested it on VBA first and then transferred it in .net and it worked!!!

I was able to queue up the "Shape is changed" marker event in the event monitor and was able to listen to it in my add-in.

Thank you for the help guys!

- Miki