我正在VBA(word)运行时构建一个包含许多子菜单和子子菜单的大菜单 我想让菜单的每个尾部(我们可以说每个菜单项)与 sub 链接,这样当单击标题为 xxx 的菜单项时,xxx 就会显示在消息框中
构建菜单时 我用过
myMenu.Caption ="xxx"
myMenu.onAction ="MySub"
myMenu.Parameters = myMenu.Caption
搜索如何使用.parameters 我发现我需要提及 MySub 中的菜单对象! (因为 MySub 没有接受参数)
所以这不能解决问题:(
我想将标题传递给 MySub,因此所有菜单都像我上面提到的那样工作,
像这样 MySub(menuCaption as string)
但是我发现我必须这样使用它
MySub()
Msgbox myMenu.Parameter
end sub
所以在 MySub 中,如果我需要提及 myMenu 那么它如何是动态的并适用于所有菜单项,
这是构建我的菜单的子
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
有什么线索吗??
非常感谢,
最佳答案
你必须像这样构建所有菜单
myMenu.Caption ="xxx" 'change this caption every time
myMenu.onAction ="MySub"
myMenu.Parameters = myMenu.Caption
然后,在您的子目录中,使用
Public Sub MySub()
MsgBox CommandBars.ActionControl.Parameter
End Sub
这总是为您提供所单击的菜单的标题。
关于vba - 使用 onAction 和参数在运行时在 word vba 中构建菜单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22602874/