tags:

views:

340

answers:

1

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

+1  A: 

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
Jon Fournier
THANK YOU VERY MUCH. FOR THIS GREAT EXPLAINATION!!!
minduser
If that works for you please accept the answer
Jon Fournier