3
votes

I am building a big menu with many of sub and sub sub menus at runtime in VBA (word) I want to let every tail of the menu (we can just say every menu item) to be linked with sub so when menu item with caption xxx be clicked the xxx show up in message box

while building the menus I used

myMenu.Caption ="xxx"
myMenu.onAction ="MySub"
myMenu.Parameters = myMenu.Caption

searching for how to use the .parameters I found that I need to mention the menu object in MySub !!! (as MySub didn't take a parameter)

so this will not solve it :( I want to pass the caption to MySub, so all menus work just like I mentioned above, like this MySub(menuCaption as string)

but I found that I have to use it like this

MySub()
  Msgbox myMenu.Parameter
end sub

so in MySub if I need to mention myMenu then how it could be dynamic and work on all menu items,

Here is the sub which builds my menus

Sub BuildCommentsMenu()
 Dim men As CommandBarControl
 Dim i%
 Do
 'test line to be removed
 'If i > 299 Then Exit Do

    If Len(commentsObjs(i).comment) = 0 Then Exit Do
Set men = categoriesObj.getMenuUponName(commentsObjs(i).Type_).Controls.Add(msoControlButton, , , , True)
    With men
        .caption = Left(commentsObjs(i).comment, 200)
        If Len(commentsObjs(i).comment) > 200 Then .caption = .caption + "..."
        .Visible = True
        'uncomment next line when you put the functions
        .OnAction = "fnMen" & CStr(commentsObjs(i).ID)
    End With
Set commentsObjs(i).CommentMenu = men
    i = i + 1
 Loop
 End Sub

enter image description here any clue ??

Thank you so much,

1
right, it seems that you need to pass either the menu object to your MySub() or pass the caption like MySub(caption as String and then MsgBox caption.user2140173
What version of Word are you using?Olle Sjögren
Great mehow, but I can't do this, my version is word 2011 for Macamr osama
@mehow or let's say, how I can do that? my tries failedamr osama
@amrosama have a READuser2140173

1 Answers

1
votes

You have to build all your menus like this

myMenu.Caption ="xxx" 'change this caption every time
myMenu.onAction ="MySub"
myMenu.Parameters = myMenu.Caption

Then, in your sub, use

Public Sub MySub()
    MsgBox CommandBars.ActionControl.Parameter
End Sub

This always give you the caption of the menu which was clicked.