问题
I'm creating a macro to add a menu button to a selected Visio shape object, so whenever the user right-clicks on the box, an option will appear and will call a macro. I created a couple of properties to the object, which will be used by the action to be called.
I can do it (SUCCESSFULLY) manually by using the ShapeSheet editor -> View Sections -> Actions -> and configuring the action with the Action value of
=CALLTHIS("ThisDocument.myFunction",,Prop.IPAddress)
sub myFunction (shpObj as Visio.shape, strIPAddress as String)
'working code with the functionsI want it to do. here I use the strIPAddress passed as an argument
What I'm trying to do is automate this by creating a macro that does the same:
Public Sub AddActionToShape()
Dim vsoShape1 As Visio.Shape
Dim intActionRow As Integer
'Performs this action to the selected item
Set vsoShape1 = Application.ActiveWindow.Selection(1)
'create row in the action section (http://office.microsoft.com/en-gb/visio-help/HV080902125.aspx)
intActionRow = vsoShape1.AddRow(visSectionAction, visRowLast, visTagDefault)
'add action to the row (http://msdn.microsoft.com/en-us/library/office/ff765539(v=office.15).aspx)
'HERE IS THE PROBLEM
**vsoShape1.CellsSRC(visSectionAction, intActionRow, visActionAction).FormulaU = """myFunction(vsoShape1, vsoShape1.Prop.IPAddress)"""**
vsoShape1.CellsSRC(visSectionAction, intActionRow, visActionMenu).FormulaU = """My Function"""
End Sub
My question:
What value should I put on the FormulaU to refer to a sub routine defined in my macro, while passing parameters. If I should not be using this FormulaU attribute, please point me to the correct one.
回答1:
You should set FormulaU exactly to the content to which you set it manually. That is, to
CALLTHIS("ThisDocument.myFunction",,Prop.IPAddress)
Try:
vsoShape1.CellsSRC(visSectionAction, intActionRow, visActionMenu).FormulaU = _
"CALLTHIS(""ThisDocument.myFunction"",,Prop.IPAddress)"
回答2:
I ended up doing it like this and it works just fine.
Dim formula As String
formula = "=CALLTHIS([MODULE],,[ARG1],[ARG2])"
formula = Replace(formula, "[MODULE]", Chr(34) & "ThisDocument.myFunction" & Chr(34))
formula = Replace(formula, "[ARG1]", "Prop.IPAddress")
formula = Replace(formula, "[ARG2]", "Prop.Username")
'After the formula has been created, apply it to the row
shape.CellsSRC(visSectionAction, rowBeingEdited, visActionAction).formula = formula
来源:https://stackoverflow.com/questions/26811268/add-menu-action-programatically-to-visio