VBA Code in Stencil file

G

garet

Hi All,
I have written some code in VBA, which I want to eventually convert to
a VB.NET program. But in the meantime I want to move the code out of
the template and into a stencil. Part of what the program does is
create a toolbar and associated buttons. Unfortunately when the
stencil libary is loaded, my toolbar/buttons do not appear. It does
work when the code is in opendocument subroutine of the document.
Junethesecond mentioned on one of the other forums to use the
withevents. The withevents seems to need to be in the class. But
beyond that I am not sure where put the activation of the code. I
appreciate any help!

Thanks!
Garet
 
V

Vidush

Hi,
The toolbar can be added to the application or to a drawing document when
the stencil is opened. Write the code that will build the toolbar in the
stencil "DocumentOpened" event handler. You can use the UIObject.

This piece of code adds a costume toolbar to the application.
Move to the Visual basic editor, under the stencil Visio Objects library,
add the following code to ThisDocument page.

Private Sub Document_DocumentOpened(ByVal Doc As IVDocument)

Dim vsoUIObject As Visio.UIObject
Dim vsoToolbarSet As Visio.ToolbarSet
Dim vsoToolbarItems As Visio.ToolbarItems
' The tool bar will contain up to 5 buttons.
Dim vsoToolbarItem(5) As Visio.ToolbarItem
Dim vsoToolbar As Visio.Toolbar
Dim i As Integer

'Get the UIObject object
Set vsoUIObject = Application.BuiltInToolbars(0)

Set vsoToolbarSet = vsoUIObject.ToolbarSets.ItemAtID(visUIObjSetDrawing)

'Get the new Toolbar from Toolbars colection
Set vsoToolbar = vsoToolbarSet.Toolbars.Add
vsoToolbar.Caption = "ATSys"
vsoToolbar.Position = visBarTop
Set vsoToolbarItems = vsoToolbar.ToolbarItems

'Add a new buttons
For i = LBound(vsoToolbarItem) To UBound(vsoToolbarItem)
Set vsoToolbarItem(i) = vsoToolbarItems.Add
vsoToolbarItem(i).CntrlType = visCtrlTypeBUTTON
Next

'Set the properties for the new toolbar item
vsoToolbarItem(0).Caption = "Compile"
' ATE – the stencil name
' ShowInMenu – the module (that contains the macro) name
' Compile – the macro (that will be executed onClick) name.
vsoToolbarItem(0).AddOnName = "ATE.ShowInMenu.Compile"
vsoToolbarItem(0).IconFileName "C:\temp\compile.ico"

' Declare up to 5 buttons…

' Add the toolbar to the application
Application.SetCustomToolbars vsoUIObject

End Sub

If you wand to write some code behind the stencil that will be executed only
on the opened drawing document, Get the drawing document from the
application documents collection (it should be opened before the stencil is
being opened) Change the last row to:

' Add the tool bar to the drawing document
DrawingDocument.SetCustomToolbars vsoUIObject

In this case, the toolbar will appear only when the drawing document is
active.
Pay attention, the stencil must be in the drawing document references list
so the toolbar buttons macros can be executed from the stencil modules.

Good luck!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top