Question

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,

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top