I want to create a number of macros and be able to use them in any Visio document. I know the rudiments of creating a macro with the "Record Macro function开发者_如何学Python", but it appears that the macro so recorded is only available to that Visio file. I've searched to see how this could be done but have not had success on how to do so. Any assistance would be appreciated.
I'm assuming you mean you want to write code in a Visio document that calls code written in another Visio document. There are two ways I know of to do this with VBA.
The first is to use the ExecuteLine method on a Document object. You just pass in a line of VBA you want to call in that document as a string argument, and the code gets called. This is really only good for calling macros that take no arguments, or if they do, just string/number arguments.
The second way is to save your document (usually a vss* stencil file, if you provide template shapes with your macros), and in any other document you just set a reference to your code file. This allows the VB Editor to have intellisense and see all the methods you can call in your macro file, and makes it possible to pass whatever type of arguments that your methods can take.
Now, if you are only interested in invoking/calling simple methods in a central file through UI actions, there are many different ways to do this. You can assign code to different shapes, ie whenever that shape moves, or when it'd double clicked. This can be assigned in the ShapeSheet window for a shape in the Events section.
You can also draw command buttons or any other VBA controls right on a Visio page and assign your macros to those buttons.
Or you can go to Tools->Macros->(your document name) and there will be more dropdowns for each module in your code file, where you can call any public subs that have no arguments.
Or you can build a toolbar (in the pre-Visio 2010) or ribbon interface with buttons that call your code. Building a toolbar for a VBA solution is kinda crappy though, from my experience. A toolbar button has to call code contained in the document you want to have your code act on. So let's say you have your code all written and saved in a VSS file that, any time a new document is opened, calls a method that adds a toolbar for the new document. When you assign the code to the buttons on that toolbar, the toolbar is assuming the code you've assigned resides in the new document you've opened, not your VSS file. So, to make your buttons call your VSS file, you have to have a method in your new document that uses ExecuteLine to call your actual code in your VSS file. This is a pain, because now your user has to use a VST template file you provide, rather than, theoretically, use your tool on any drawing they want to.
Sorry for the rant, but Visio is pretty flexible in terms of how to call code, so you can probably do just about anything you want.
My solution is to save my macros in a stencil. As long as you have the stencil open, the code is available to the active drawing. Now, if I just figure out how to put "buttons" in the stencil to activate a specific macro...
精彩评论