我一直在 Microsoft Visio 16 中使用 VBA v7.1,以便在选择选定形状时将其带到前层。我已经设置了一个事件处理程序来触发 SelectionChanged 事件,但没有取得任何成功。
这是我当前使用的代码:
Public MyEvents As New MyVisioEvents
Sub InitializeEvents()
Set MyEvents.vApp = Visio.Application
End Sub
Public WithEvents vApp As Visio.Application
Private Sub vApp_SelectionChanged(ByVal Selection As IVSelection)
If Not Selection Is Nothing Then
If Selection.Count > 0 Then
Dim shape As Visio.shape
Set shape = Selection.Item(1)
shape.BringToFront
Debug.Print "Shape moved to front: " & shape.Name
End If
End If
End Sub
最终我想对此进行设置,以便在打开 Visio 文档时调用 InitializeEvents;但是,首先,我希望代码在手动执行 InitializeEvents 时能够正常工作。我收到臭名昭著的错误:“过程声明与同名事件或过程的描述不匹配。”我在许多其他 VBA 帖子中看到此错误,但无法解决我的具体情况。这是我尝试过的调试步骤。到目前为止没有任何帮助。我不断收到同样的错误。
Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
InitializeEvents
End Sub
Private Sub Document_DocumentOpened(ByVal doc As IVDocument)
Debug.Print "Document opened and InitializeEvents is running."
InitializeEvents
End Sub
在我这边,这段代码有效!
Public WithEvents vApp As Visio.Application
Sub InitializeEvents()
Set vApp = Visio.Application
End Sub
Private Sub vApp_SelectionChanged(ByVal Window As IVWindow)
Dim sl As Selection
Set sl = Window.Selection
If Not sl Is Nothing Then
If sl.Count > 0 Then
Dim shape As Visio.shape
Set shape = sl.Item(1)
shape.BringToFront
Debug.Print "Shape moved to front: " & shape.Name
End If
End If
Set sl = Nothing
End Sub