Code CAN be attached to Visio Shapes
David A Edson
Mechanics and History
For a long time Visio has had a ShapeSheet function called RUNADDON(). This function was originally designed to call and launch additional functionality written as a .VSL (C or C++ DLL extension to Visio), or written as a stand-alone executable, .EXE (VB application). As VBA was introduced in Visio Technical and Professional under 4.5 and extended to the entire product line under 5.0, the RUNADDON() function can now call VBA functionality as well. You would normally add an ACTIONS section to any Visio SmartShape symbol or to a page in a Visio document. The Action Cell would contain the RUNADDON() function with its only argument being the name of the .VSL or .EXE that you desired to be launched. Below are two examples of this:
=RUNADDON("Property Reporting Wizard.EXE")
=RUNADDON("Valve Builder")
Note that no path to the .VSL or .EXE is listed. Rather, the .VSL or .EXE must be in Visio’s search path as set from the Tools > Options Dialog, in the File Paths Tab, on the Add-ons: line. Note that the file paths are semi-colon deliminated.
Presuming that you had created a VBA add-on named "Border_Colour", you could call it with the following syntax:
=RUNADDON("Border_Colour")
and Visio will be sable to suss out that it is a VBA code item and launch it appropriately.
Now here’s the slick wee bit o’ business….. Visio’s RUNADDON() function can also recognise standard VBA functions as weel as an argument to the function. The next two examples illustrate this making use of the lowly MsgBox VBA function:
=RUNADDON("MsgBox ""I’ve been clicked!""")
=RUNADDON("For I = 1 to 5 ; MsgBox I ; Next I")
Lets have a wee look at the first… In this case the RUNADDON() function is calling VBA’s MsgBox function a single time and displaying the text "I’ve been clicked!" in the message box. Note that the string to be displayed must be enclosed in double-double-quotes in order for the function parser to understand that it is text, and the entire argument to the RUNADDON() function must be a quoted string. This explains the preponderance of double-quotes in the code.
The second case calls VBA’s MsgBox function five times, and displays the numbers "1", "2", "3", "4", and "5" in each successive message box. Note that each "line" of VBA cone is deliminated here with a colon, and as such three lines of VBA code are executed.
Be aware of the limitations of string length in a Visio SmartShape symbol’s ShapeSheet cell. It is generally about 127 characters. You can programmatically force greater length strings into a cell formula but ANY attempt to access or edit the cell at a later date will truncate it back to the 127 character limit, thereby rendering it, quite, possibly, unusable.
A few wee examples
In this first example, I am gang tae add a wee bit of functionality tae a Shape’s Double-Click event as found in the EventDoubleClick cell in the Events section of the ShapeSheet. This functionality will pop up a message box and display the shape’s Name and filled area each time the shape is double-clicked. The single line of code in the cell looks like:
=RUNADDON("Set shp = ActiveWindow.Selection(1) : s = ""Shape Name = "" & shp.Name : s = s & chr(13) : s = s & ""Shape Area = "" & shp.AreaIU & "" Square Inches."" : MsgBox s")
Breakin’ this doon… RUNADDON() is first bindin’ the variable "shp" to the currently selected shape, that is the first item in the selection collection of the active window. Then a variable "s" is set to the string "Shape Name = " and the concatinated name property of the shape "shp" object. This string is then concatinated with a character string "13", which is a carrage return, and further concatinated with the string "Shape Area = " and the concatinated area-in-internal-units property of the shape "shp" object. Finally this full concatinated string is passed as the argument "s" to the VBA MsgBox() function.
Presumin’ tha’ the shape is a rectangle 3 inches wide by 2 inches tall and it is the third shape on the current page, the output to the message box would be:
Shape Name = Sheet.3
Shape Area = 6 Square Inches.
If this same code were to be written in the VBA editor it would look like:
Public Sub WhoAmI ()
Dim shp As Visio.Shape
Dim s As String
Set shp = Visio.ActiveWindow.Selection.Item(1)
s = "Shape Name = " & shp.Name
s = s & chr(13)
s = s & "Shape Area = " & shp.AreaIU & " Square Inches."
MsgBox s
End Sub
If packin’ a’ tha’ code intae a single line is causin’ ye tae "fash" (worry, for those of you not of the Celtic persuasion), you can place the individuaql pieces into User Defined Cells and then concatenate them into the EventDoubleClick cell similar to the following:
User.L10 = "For i = 1 to 3 :"
User.L20 = "MsgBox i:"
User.L30 = "Next i"
EventDblClick = RUNADDON(User.L10 & User.L20 & User.L30)
In this second example, I’m going to show you how you can place a large notes field as an attachment to any shape. The user can read an’ edit the notes as required. To accomplish this you will need to add an Actions section to your ShapeSheet. This will allow the user to "right-click" on the shape and call your functionality. You will be setting the values of both the Menu cell and the Action cell in the Actions section.
Actions[1].Menu = "Notes…"
Actions[1].Action = RUNADDON("Set shp = ActiveWindow.Selection(1) : p = shp.Data1 : t = ""Notes for "" & shp.Name : d = shp.Data1 : ib = InputBox(p,t,d) : If ib <> """" Then shp.Data1 = ib")
The VBA equivalent looks similar to:
Public Sub MyNotes ()
Dim shp As Visio.Shape
Dim p As String
Dim t As String
Dim d As String
Dim ib As String
Set shp = Visio.ActiveWindow.Selection.Item(1)
p = shp.Data1
t = "Notes for " & shp.Name
d = shp.Data1
ib = InputBox(p, t, d)
If ib <> " " Then shp.Data1 = ib
End Sub
Note tha’ we need tae check the string returned by the input box tae see if it is empty.
If ib <> " " Then shp.Data1 = ib
If the Cancel button on the Input Box were clicked, then the string returned would be empty. Without this check, the statement:
shp.Data1 = ib
would clear oot the notes when the Cancel button was clicked. This micht no’ be what ye had desired, especially if there were lots of notes to the shape, oor the user thought that Cancel just meant that he oor she didn’t want to change anything. However, the functionality above negates the possibility of truly deleting the note if that is what the user actually had in mind. To remedy this the following Actions line will clear oot the notes content:
Actions[1].Menu = "Clear Notes"
Actions[1].Action = RUNADDON("ActiveWindow.Selection(1).Data1 = "" ")
Weel thar ye ha’e it folk!!! You can now set aboot addin’ yer ain notes to Visio SmartShapes symbols and additionally, begin tae experiment wi’ addin’ your own bits o’ VBA functionality to your verra smart Visio shapes!!!
I want tae thank Chris Roth at Visio for a’ his weark in researchin’ an’ documentin’ this functionality!!! Ceuid Mile Thanks Chris Laddie!!!!!
‘Till next month, enjoy yer American Thanksgivin’, dinnae eat tae much, tak a weel desearved rest, an…
"Haste ye back."
댓글을 달아 주세요