Best way to use and share macros

Started by perry59, December 16, 2010, 06:42:59 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

perry59

I have accumulated a number of helpful vba macro's and am looking for the most convenient way of invoking them. In Autocad I saved them out as individual files (binary vba projects) which I could invoke via a custom toolbar. This made it eas to use and easy to share. I would just give people my toolbar and macro files and they were off and running. It was also easy to maintain if we all kept the macros in a shared location.
Can I do the same with visio? It seems the only thing I can export from the vba editor is individual .bas/.cls/.frm files, not a complete project. I also read that making toolbars in visio is a real pain.
I have seen numerous mentions of putting macros on a stencil. That sounds like it might be the trick.
How do you do that? Can you add an actual vba "project", i.e. a macro that includes a form and/or class module in addition to the .bas?
Thanks for any tips
what, me worry?

perry59

Ok, I found the "stencil vba reference" post with its download and tried it out.
works like a champ! Just one question, can I drag a macro from the stencil without actually
dropping a shape onto the drawing? or perhaps some kind of "empty" shape?
what, me worry?

Jumpy

Quote from: perry59 on December 16, 2010, 07:57:10 PM
... can I drag a macro from the stencil without actually
dropping a shape onto the drawing? or perhaps some kind of "empty" shape?

What do you mean by dragging a macro from the stencil? Starting it? Adding it (the code) to the project of the drawing?

perry59

#3
I mean dragging it from a stencil.
you have a shape on a stencil, it has associated code, you drag it to a drawing and the code executes.

referencing vba modules in stencils
what, me worry?

perry59

#4
Ok, so I used the above example to get me going. At first I thought that each shape on a stencil was a "document" and thus had its own capactity for a vba project. Found out pretty quick that was not the case. The stencil itself is the document and so all shapes on it share whatever code it may contain. In order for a specific shape to call a specific routine the shapes drop event needs to specify the routine to run.
Once I came to that conclusion, I created a new stencil called "Macros" (pretty original eh?), put a couple shapes on it, and added some code to "thisdocument" (in the stencil). I edited each master so that each would call a particular routine. Something is not quite right somewhere though as no routines are invoked when I drag a shape onto a drawing. Can anyone tell me whats wrong here? (also attached is my stencil)
One shape has this in its dropevent: =CALLTHIS("ThisDocument.fontSizer","Macros")
the other has this: =CALLTHIS("ThisDocument.fitToPageAll","Macros")

The code in the stencil is this:

Public Sub fontSizer(shp As Visio.Shape)
   Call MsgBox("Hi, my name is: " & shp.ID, , "Code from stencil")
   'UserForm1.Show
   ' Change the font of all shapes to 10pt
   Dim shpObjs As Visio.Shapes, shpObj As Visio.Shape
   Dim i As Integer
   
   Set shpObjs = ActivePage.Shapes
   For i = 1 To shpObjs.Count
   Set shpObj = shpObjs(i)
   Set celObj = shpObj.Cells("Char.Size")
   celObj.Formula = "=10 pt."
   ' or
   ' shapeObj.Cells("Char.Size[1] ").Formula = "= 10 pt."
   Next

End Sub


Public Sub fitToPageAll(shp As Visio.Shape)
   Call MsgBox("Hi, my name is: " & shp.ID, , "Code from stencil")
   'UserForm1.Show
   'Setting the zoom factor of all pages to 'fit to page'
   Dim PageToIndex As Visio.Page
   Dim curPage As Visio.Page

   Set curPage = ActivePage

   ' loop through all the pages you have and set the zoom factor
   For Each PageToIndex In ActiveDocument.Pages
       ActiveWindow.Page = ActiveDocument.Pages(PageToIndex.Index).Name
       ActiveWindow.Zoom = -1
   Next
   
   ActiveWindow.Page = curPage
End Sub

what, me worry?

Jumpy

First you don't need to specify the modul (in your case ThisDocument), if you don't have more than one sub with the same name in different moduls.
So =CALLTHIS("fitToPageAll","Macros") should be enough.

Beside from that, the ThisDocument modul is somehow special, and you can't always fire code from it. I would add a new modul to the projekt of the stencil and place your code there. That should work.

vojo

dont you need module name if macro is in the stencil?

perry59

#7
Well, I moved the routines into a different module (other than "ThisDocument") and changed the "call" of course. That didnt work.
Then I took the module name out of the call. That didnt work either :(
Could one of the more experienced visio guys look at the stencil I attached and see if they can find something wrong?
Thanks
what, me worry?

perry59

Never mind. I found the problem.
Although in my events the "call" matched the name of the module and template, the name of the vba project did not match. Grrrrrr >:(
The name of the stencil, and the name of the vba project contained in it should (must?) be the same!
what, me worry?

perry59

I still want to know though, can I drag one of these "macro shapes" to my drawing without actually dropping a shape onto the drawing?
what, me worry?

Jumpy

No, but you could delete the shape directly afterwards in your VBA-Code.
Or/And you can start the macros the usual way Extras->Macros->...

Another way is to install a custom menu from which to start your macros.

Or look into deploying them from a Template, than you could use Shortcuts, too.

perry59

Quote from: Jumpy on December 17, 2010, 07:59:54 PM
No, but you could delete the shape directly afterwards in your VBA-Code.
Or/And you can start the macros the usual way Extras->Macros->...
Another way is to install a custom menu from which to start your macros.
Or look into deploying them from a Template, than you could use Shortcuts, too.
Yeah, I thought about the delete method but thought it would look funny for the shape to appear then immediately disappear. To have them in Extras->Macros-> means they would have to exist in the current document. This wont be the case when opening existing documents, and I do not want the same code in every single document any way. Same goes for the template, does no good when opening existing documents not based on that template.
Looks like I'm stuck with what I have. May just code them into an addin when I have enough to make it worthwhile. Autocad handled this scenario much better.

Thanks
what, me worry?

Paul Herber

Quote from: perry59 on December 17, 2010, 08:33:29 PM
Yeah, I thought about the delete method but thought it would look funny for the shape to appear then immediately disappear.
You could make the shape with no line, no fill, transparent and just to make sure, of zero size.
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

vojo

or go the other way.   Tie them to a header or footer or title block....something you need anyway

Jumpy

Quote from: perry59 on December 17, 2010, 08:33:29 PM
To have them in Extras->Macros-> means they would have to exist in the current document. This wont be the case when opening existing documents, and I do not want the same code in every single document any way.

That is not correct. If you have your macros in a stencil you find them in Extra->Macros. (As long as you don't place them in the ThisDocument modul. It is imho generally better to add a new modul to place macros to a projekt and only use the ThisDocument modul for events and such.) You don't need a reference in the drawing for that!

Another untested idea: Couldn't you make your own menu and start the code from that? The menu could be established in the Document_opened-event of the stencil.