Where to Call UI-modifying Code?

Started by bouananimeher, September 26, 2011, 10:34:42 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

bouananimeher

I have two Marcos , Macro1 which is processing on an IE instance, and Persist which deals with MSSQL. I've created two buttons for these macros to execute them, and here is the source code to do that:

Sub CreateMyButtonBar()
Dim CB As CommandBar
Dim CBS As CommandBars
Set CBS = Application.CommandBars
Set CB = CBS.Add(CommandBarName, msoBarFloating)
AddButtonToBar CB, "Macro1", "Run security application", 186
AddButtonToBar CB, "Persist", "Persist Data", 180
CB.Visible = True
End Sub

Private Sub AddButtonToBar(Bar As CommandBar, _
MacroName As String, _
Caption As String, _
Button As Integer)
Dim CBC As CommandBarControl
Set CBC = Bar.Controls.Add(msoControlButton, 1, , , True)
CBC.Style = msoButtonAutomatic
CBC.FaceID = Button
CBC.Caption = Caption
CBC.OnAction = MacroName
End Sub


The problem still now where should I put the call of the function CreateMyButtonBar inside"ThisDocument" of the Stencil in order to create these two buttons that appear each time I tried to open Visio or when I create a new document or when I am working on an existing document !!?

Visio Guy

Hi bouananimeher,

You can call your toolbar-creating code inside of ThisDocument.RunModeEntered. This fires whenever the document is opened, whether the original is opened or a copy of it. Also, RunModeEntered fires when you toggle the blue triangle button in VBA, so it makes it easy to test your code without having to close and re-open the document every time.

You could put the code inside of a stencil that you load as a sort of library. The UI code and the database and website procedures could all be in the stencil's VBA project.

Hope that makes sense!
For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010

bouananimeher

Hi,

Thank you for your reply.. I put my code inside of a stencil, i put the call to my CreateMyButtonBar function inside Document_RunModeEntered. I open Visio, create a blanc page and i try to open my stencil but i have an error in the Set CB = CBS.Add(CommandBarName, msoBarFloating) line ! CommandBarName does not recognize yet...

Jumpy

Maybe you have to check first, if the commandbar already exists?

bouananimeher

hello guys, any way i just finished the remaining work and know i am looking again on this problem .. So the trick now is that I created the buttons but it does not recognize the actions assigned to each button from macros

this is the code that i put in ThisDocument class into my Stencil

Sub Run()
Call ThisDocument.ExecuteLine("Module1.test")
End Sub

Private Sub Document_DocumentOpened(ByVal Doc As IVDocument)
Run
End Sub


and in Module1 I have this code

Sub test()
CreateMyButtonBar
End Sub

Sub CreateMyButtonBar()

Dim CB As CommandBar
Dim CBS As CommandBars
Dim Var As Variant
Set CBS = Application.CommandBars

On Error GoTo Oops
Set CB = CBS.Add("test bar", msoBarFloating)
AddButtonToBar CB, "testing", "test", 186
CB.Visible = True
Oops:
End Sub
Private Sub AddButtonToBar(Bar As CommandBar, _
       MacroName As String, _
       Caption As String, _
       Button As Integer)
Dim CBC As CommandBarControl
Set CBC = Bar.Controls.Add(msoControlButton, 1, , , True)
CBC.Style = msoButtonAutomatic
CBC.FaceID = Button
CBC.Caption = Caption
CBC.OnAction = MacroName
End Sub


and this is Module2 code

Sub testing()
MsgBox "Worked"
End Sub


so in AddButtonToBar CB, "testing", "test", 186 line I need to call testing sub in another way ? I tried with ThisDocument.Module2.testing and some others combinations but it didn't work...

bouananimeher

If I put Module1 and Module2 in my .vsd document it work !! But I need to get Macros in my Stencil :(

Jumpy

#6
Hello,

is testing public?

"ThisDocument.Modul2.testing" won't work, but the combination "Modulname.Macroname" like in "Modul2.testing" should work.

hth Jumpy

Edit:
I was too slow, to see your last post:
Where resides the code that creates the buttons? In the stencil, too?

Normally it is not possible to start macros in a stencil from the drawing, because the drawing doesn't "know" the code in the stencils. For that to work you normally have to create a reference to the stencil code. Like to external librarys you add them in the VBA-Editor via Options->References (I hope that are the right commands, as I have no English Visio.

It may be possible to set those references on the fly using code. Here's an example how it is done in Excel:
http://www.vbaexpress.com/kb/getarticle.php?kb_id=267

Somewhere in this forum there is a thread discussing the problems on can encounter with code in stencils and workarrounds for those.

bouananimeher

Hello Jumpy,

Thank you for your reply I just add the Reference and it works now... It will be nice to find some code which add this reference automatically :)

I'll do some research on this issue.

Cheers

Jumpy

Hi again!

Found the thread I remembered:

http://visguy.com/vgforum/index.php?topic=249.msg1075#msg1075

By reading this additional thread:

http://visguy.com/vgforum/index.php?topic=359.msg2080;topicseen#msg2080

I had an idea for a workarround:

- Create a User defined cell in the ShapeSheet of the document or page, for example: User.Makroname
- Create a further User defined cell in the ShapeSheet of the document or page, for example: User.Modulname
- Set the cell of User.Modulname.Prompt to TRUE
- Create a third User defined cell in the ShapeSheet of the document or page, for example: User.Projektname
- Place a Formula for example in the User.Makroname.Prompt cell:
=Callthis(User.Modulname&"."&User.Makroname,User.Projektname)+Dependson(User.Modulname.Prompt)

Now in a modul of the drawing you write a Sub to call a Sub in the stencil with the help of thoose cells:


Sub CallStencilSub(Makroname as String, Modulname as String, Projektname as String)
  Dim pg as PageSheet
  Set pg = ActivePage.PageSheet  'Only for example

  pg.Cells("User.Makroname") = Makroname
  pg.Cells("User.Modulname") = Modulname
  pg.Cells("User.Projektname") = Projektname
  pg.Cells("User.Modulname.Prompt").FormulaU = Not(pg.Cells("User.Modulname.Prompt").FormulaU)

End Sub


Problem that remains is to tie such a procedure to your buttons...

bouananimeher

Your explanation with the first link helped me to understand how it works, I will look over and try to follow these steps it should take me some time because i am pretty new here and i am a beginer with VBA development... Thanks a lot :)

DJozef87

Hello guys,

I have a question related to this topic...

I have Macros in a stencil and I add manually the reference of the Stencil it will be nice to automate this :)

but my question is that I need to get the Document_DocumentSaved event of my active document from the Stencil... How can I get it ?

Jumpy

The stencil is saved?
Or:
The drawing is saved and the code to react to that is in the stencil?

DJozef87

yes The drawing is saved and the code to react to that is in the stencil and the Stencil is open in read only so I can't put my code in Stencil Saved event

Jumpy

Then your goal is the same like bouananimeher's. Try AlAdlund's idea in the link above.

DJozef87

hello guys,
I create a very simple Sub which progmatically add reference that I need in my project when I load my Stencil and it's very useful for me.
So I put the Sub into my Stencil and i call it in the openDocument event


Sub AddRefs()
    Dim VBAEditor As VBIDE.Vbe
    Dim vbProj As VBIDE.VBProject
    Set VBAEditor = Application.Vbe
    Set vbProj = VBAEditor.ActiveVBProject
   
    On Error Resume Next
   
          With vbProj.References
            .AddFromFile "C:\Users\Me\StencilwithMacros.vss"
          End With
 
End Sub