“Directly caused by user” Visio events identifiable as such?

Started by Visisthebest, February 27, 2021, 10:51:57 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Visisthebest

Is it possible in Visio to distinguish between events that a user directly caused versus events that were generated by VBA or add-in code?

For instance, a user can add a shape by hand or code can add a shape, would be great to be able to distinguish the events generated as either user or code-caused.
Visio 2021 Professional

Thomas Winkel

Hi,

maybe you could consider some error handling to ensure that shapeAddedByCode is always false after your code finished or crashed.


Option Explicit

Dim shapeAddedByCode As Boolean

Private Sub Document_ShapeAdded(ByVal vsoShape As Visio.IVShape)
    If shapeAddedByCode Then
        MsgBox "Shape added by code"
    Else
        MsgBox "Shape added by user"
    End If
End Sub

Sub AddShapeToActivePage()
    Dim shp As Visio.Shape
   
    shapeAddedByCode = True
    Set shp = ActivePage.DrawRectangle(1, 4, 4, 1)
    shapeAddedByCode = False
End Sub

Visisthebest

Visio 2021 Professional

Thomas Winkel

Another option could be to disable events temporary:


Option Explicit

Private Sub Document_ShapeAdded(ByVal vsoShape As Visio.IVShape)
    MsgBox "Shape added"
End Sub

Sub AddShapeToActivePage()
    Dim shp As Visio.Shape
   
    Application.EventsEnabled = False
    Set shp = ActivePage.DrawRectangle(1, 4, 4, 1)
    Application.EventsEnabled = True
End Sub

Visisthebest

Yes  but wondering about the unintended effects of that.
Visio 2021 Professional