如何从模具调用 visio 宏

发布于 2024-08-24 12:10:23 字数 62 浏览 8 评论 0原文

我为 Visio 编写了一些宏。现在我将它们复制到名为 Macros.vss 的模板中 我现在如何调用我的宏?

i have written some Macros for Visio. Now I copied these to a Stencil called Macros.vss
How can I call my Macros now?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若有似无的小暗淡 2024-08-31 12:10:23

这完全取决于宏的作用以及您希望如何调用它们。我假设它们只是在活动 Visio 页面中执行某些操作的宏。

默认情况下,在 Visio VBA 中,任何不带参数的公共子项都会添加到 Visio 工具 -> 宏菜单中,位于由保存宏(在本例中为宏)的文档命名的文件夹中,然后按模块名称分隔到文件夹中。如果您是唯一使用宏的人,那么您可能不需要执行任何其他操作。

但是,由于您将它们放入 vss 文件中,我假设您想将它们分发给其他人。

关于 Visio 以及以编程方式添加的工具栏和按钮的工作原理,有一些有趣的事情(我所说的有趣是指令人恼火的事情)。不幸的是,当您使用 UIObject 以及 Toolbar 和 ToolbarItem 类创建工具栏时,Visio 将假定您调用的代码驻留在活动绘图中,而不能位于模板中。因此,我可以为您提供有关使用这些类的一些指导,但基本上它包括将 .vst 模板与 .vss 文件一起分发,并且在 .vst 文件中仅包含一个必需的子文件。

因此,您可以将代码附加到 .vss 文件中的形状母版,而不是使用自定义工具栏,当形状母版拖放到绘图文档上时执行代码(使用形状表中的 CALLTHIS 和 EventDrop 事件)。使用此方法,我只需使用 callthis 调用一个子程序,该子程序将形状对象作为参数,执行一些代码,然后删除该形状(如果我不再需要它)。

最后,您可以通过编程方式操作 Visio UI,为宏添加工具栏和按钮。下面是一些示例代码,基本上是我使用我开发的解决方案执行此操作的方式。正如我上面提到的,使用此方法最重要的部分是拥有一个文档模板 (.vst),其中包含一个以字符串作为参数的子模板(在下面的代码中,它必须命名为 RunStencilMacro)。该字符串应该是“DocumentName.ModuleName.SubName”。该子程序必须从字符串中取出 DocumentName,并获取该文档的 Document 对象句柄。然后它必须使用 ModuleName.SubName 部分对该文档执行 ExecuteLine。您必须单步执行代码并弄清楚一些事情,但是一旦您掌握了正在发生的事情,它就应该有意义了。

我不确定是否还有其他方法可以与 VBA 交互执行宏。我认为 exe 和 COM 插件的工具栏可能没有这个问题...

Private Sub ExampleUI()
    Dim UI As Visio.UIObject
    Dim ToolbarSet As Visio.ToolbarSet
    Dim Toolbars As Visio.Toolbars
    Dim Toolbar As Visio.Toolbar
    Dim ToolbarItems As Visio.ToolbarItems
    Dim ToolbarItem As Visio.ToolbarItem
    Dim TotalToolBars As Integer

    Dim Toolbarpos As Integer

    Const ToolbarName = "My Toolbar"

    ' Get the UIObject object for the toolbars.
    If Visio.Application.CustomToolbars Is Nothing Then
        If Visio.ActiveDocument.CustomToolbars Is Nothing Then
            Set UI = Visio.Application.BuiltInToolbars(0)
        Else
            Set UI = Visio.ActiveDocument.CustomToolbars
        End If
    Else
       Set UI = Visio.Application.CustomToolbars
    End If

    Set ToolbarSet = UI.ToolbarSets.ItemAtID(visUIObjSetDrawing)
    ' Delete toolbar if it exists already
    TotalToolBars = ToolbarSet.Toolbars.Count
    For i = 1 To TotalToolBars
        Set Toolbar = ToolbarSet.Toolbars.Item(i - 1)
        If Toolbar.Caption = ToolbarName Then
            Toolbar.Visible = False
            Toolbar.Delete
            Exit For
        End If
    Next

    ' create toolbar
    Set Toolbar = ToolbarSet.Toolbars.Add
    Toolbar.Caption = ToolbarName

    Dim IconPos As Long ' counter to determine where to put a button in the toolbar

    IconPos = IconPos + 1

    Dim IconFunction As String
    IconFunction = """Macros.Module1.SubName"""

    Set ToolbarItem = Toolbar.ToolbarItems.AddAt(IconPos)
    With ToolbarItem
        .AddOnName = "RunStencilMacro """ & IconFunction & """"
        .Caption = "Button 1"
        .CntrlType = Visio.visCtrlTypeBUTTON
        .Enabled = True
        .state = Visio.visButtonUp
        .Style = Visio.visButtonIcon
        .Visible = True
        .IconFileName ("16x16IconFullFilePath.ico")
    End With

    ' Now establish the position of this toolbar
    With Toolbar
        .Position = visBarTop 'Top overall docking area
        .Left = 0 'Puts it x pixels from the left
        .RowIndex = 13
        .Protection = visBarNoCustomize
        Toolbar.Enabled = True
        .Visible = True
    End With

    Visio.Application.SetCustomToolbars UI
    Visio.ActiveDocument.SetCustomToolbars UI
End Sub

It all depends on what the macros do and how you'd like to call them. I'm going to assume they're simply macros that will execute something within the active Visio page.

By default in Visio VBA, any public subs with no arguments get added to the Visio Tools->Macros menu, in a folder named by the document holding the macros (in this case Macros) and then separated into folders by module name. If you're the only person using the macros then you probably don't need to do anything else.

However, since you put them in a vss file I'll assume you'd like to distribute them to other people.

There's something funny (and by funny I mean irritating) about Visio and how toolbars and buttons work, when added programmatically. Unfortunately, when you create a toolbar using the UIObject and Toolbar and ToolbarItem classes, Visio is going to assume the code you're calling resides in the active drawing, and cannot be in a stencil. So I can give you a little guidance on using those classes, but basically it consists of distributing a .vst template along with your .vss files, with just a single required sub in the .vst file.

So, instead of using a custom toolbar, you can attach code to shape masters in your .vss file that execute the code when they get dropped on a drawing document (using CALLTHIS and the EventDrop event in the shapesheet). With this method I just have a sub that gets called using callthis that takes a shape object as an argument, executes some code, then deletes the shape (if I don't want it around anymore).

And lastly, you can manipulate the Visio UI programmatically to add a toolbar and buttons for your macros. Below is some sample code, basically the way I do it with a solution I developed. As I mentioned above, the most important part of using this method is to have a document template (.vst) that holds a sub (with the below code it must be named RunStencilMacro) that takes a string as an argument. This string should be the "DocumentName.ModuleName.SubName". This sub must take the DocumentName out of the string, and get a Document object handle to that document. Then it must do ExecuteLine on that document with the ModuleName.SubName portion. You'll have to step through the code and figure some things out, but once you get the hang of what's going on it should make sense.

I'm not sure of any other ways to execute the macros interactively with VBA. I think exe and COM addons may not have this issue with toolbars...

Private Sub ExampleUI()
    Dim UI As Visio.UIObject
    Dim ToolbarSet As Visio.ToolbarSet
    Dim Toolbars As Visio.Toolbars
    Dim Toolbar As Visio.Toolbar
    Dim ToolbarItems As Visio.ToolbarItems
    Dim ToolbarItem As Visio.ToolbarItem
    Dim TotalToolBars As Integer

    Dim Toolbarpos As Integer

    Const ToolbarName = "My Toolbar"

    ' Get the UIObject object for the toolbars.
    If Visio.Application.CustomToolbars Is Nothing Then
        If Visio.ActiveDocument.CustomToolbars Is Nothing Then
            Set UI = Visio.Application.BuiltInToolbars(0)
        Else
            Set UI = Visio.ActiveDocument.CustomToolbars
        End If
    Else
       Set UI = Visio.Application.CustomToolbars
    End If

    Set ToolbarSet = UI.ToolbarSets.ItemAtID(visUIObjSetDrawing)
    ' Delete toolbar if it exists already
    TotalToolBars = ToolbarSet.Toolbars.Count
    For i = 1 To TotalToolBars
        Set Toolbar = ToolbarSet.Toolbars.Item(i - 1)
        If Toolbar.Caption = ToolbarName Then
            Toolbar.Visible = False
            Toolbar.Delete
            Exit For
        End If
    Next

    ' create toolbar
    Set Toolbar = ToolbarSet.Toolbars.Add
    Toolbar.Caption = ToolbarName

    Dim IconPos As Long ' counter to determine where to put a button in the toolbar

    IconPos = IconPos + 1

    Dim IconFunction As String
    IconFunction = """Macros.Module1.SubName"""

    Set ToolbarItem = Toolbar.ToolbarItems.AddAt(IconPos)
    With ToolbarItem
        .AddOnName = "RunStencilMacro """ & IconFunction & """"
        .Caption = "Button 1"
        .CntrlType = Visio.visCtrlTypeBUTTON
        .Enabled = True
        .state = Visio.visButtonUp
        .Style = Visio.visButtonIcon
        .Visible = True
        .IconFileName ("16x16IconFullFilePath.ico")
    End With

    ' Now establish the position of this toolbar
    With Toolbar
        .Position = visBarTop 'Top overall docking area
        .Left = 0 'Puts it x pixels from the left
        .RowIndex = 13
        .Protection = visBarNoCustomize
        Toolbar.Enabled = True
        .Visible = True
    End With

    Visio.Application.SetCustomToolbars UI
    Visio.ActiveDocument.SetCustomToolbars UI
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文