vba 动态设置 CommandBar“弹出”子菜单图标
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4738102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set a CommandBar "Popup" submenu icon dynamically
提问by downwitch
I'm just trying to confirm this: In Office 2003, I want to create a custom submenu--what is known in CommandBar parlance as a popup (msoControlPopup)--at runtime, and set an image for it. With a CommandBarButton, this is very straightforward
我只是想确认一下:在 Office 2003 中,我想在运行时创建一个自定义子菜单——在 CommandBar 中称为弹出窗口 (msoControlPopup),并为其设置一个图像。使用 CommandBarButton,这非常简单
Set btn1 = mnu.Controls.Add(msoControlButton, temporary:=True)
btn1.Caption = "Btn1"
btn1.Picture = stdole.LoadPicture("C:\temp\test.bmp")
But with a CommandBarPopup, or CommandBarControl of type msoControlPopup, it fails
但是使用 CommandBarPopup 或类型为 msoControlPopup 的 CommandBarControl,它会失败
Set sub1 = mnu.Controls.Add(msoControlPopup, temporary:=True)
sub1.Caption = "Sub1"
'object doesn't support this property or method for next line
sub1.Picture = stdole.LoadPicture("C:\temp\test.bmp")
The msoControlPopup type doesn't seem to allow a .Style
property either, which is how Office determines what to show--icon, text, both--on the control. I haven't found this proven yet, so am holding out a last hope that I'm doing something wrong, and there is, in fact, a way to insert an icon on a submenu at runtime.
msoControlPopup 类型似乎也不允许.Style
属性,这就是 Office 确定要在控件上显示的内容(图标、文本等)的方式。我还没有发现这一点得到证实,所以我最后希望我做错了什么,事实上,有一种方法可以在运行时在子菜单上插入一个图标。
Thanks for any light you can shed.
感谢您提供的任何光线。
采纳答案by downwitch
Ok well more tumbleweeds. I'm pretty sure the answer to this is, it can't be done. And here's my "proof": None of the built-in submenus have icons (which I didn't realize until afterI posted the above, and if you run the above code, go to Tools > Customize in the menu bar, then click on the Test menu to drop it down, and right-click on Sub1, you should see all the button and style options greyed out. Right-click on Btn1, and they're available.
好吧,还有更多风滚草。我很确定这个问题的答案是,这是不可能的。这是我的“证明”:所有内置子菜单都没有图标(直到我发布上述内容后我才意识到这一点,如果您运行上面的代码,请转到菜单栏中的工具 > 自定义,然后单击在 Test 菜单上将其下拉,然后右键单击 Sub1,您应该会看到所有按钮和样式选项都变灰了。右键单击 Btn1,它们就可用了。
Any other thoughts still welcome.
任何其他想法仍然欢迎。
回答by Curtis Inderwiesche
Of course if you need to set the image or FaceID of the submenu heading, that is not an available method for submenu headings, but if you want to set the image or FaceID on the submenu itself, I modified code from hereto accomplish this:
当然,如果您需要设置子菜单标题的图像或FaceID,这不是子菜单标题可用的方法,但是如果您想在子菜单本身上设置图像或FaceID,我从这里修改了代码来实现这一点:
Public Sub newSubMenu()
Dim menuBar As CommandBar
Dim newMenu As CommandBarControl
Dim menuItem As CommandBarControl
Dim subMenuItem As CommandBarControl
CommandBars("Sub Menu Bar").Delete
Set menuBar = CommandBars.Add(menuBar:=False, Position:=msoBarPopup, Name:="Sub Menu Bar", Temporary:=True)
Set newMenu = menuBar.Controls.Add(Type:=msoControlPopup)
newMenu.Caption = "&First Menu"
Set newMenu = menuBar.Controls.Add(Type:=msoControlPopup)
newMenu.Caption = "&Second Menu"
Set newMenu = menuBar.Controls.Add(Type:=msoControlPopup)
newMenu.Caption = "&Third Menu"
Set menuItem = newMenu.Controls.Add(Type:=msoControlButton)
With menuItem
.Caption = "F&irst Sub"
.FaceId = "356"
.OnAction = "myTest"
End With
Set menuItem = newMenu.Controls.Add(Type:=msoControlButton)
With menuItem
.Caption = "S&econd Sub"
.FaceId = "333"
.OnAction = "otherTest"
End With
Set menuItem = newMenu.Controls.Add(Type:=msoControlPopup)
menuItem.Caption = "Sub Menus"
Set subMenuItem = menuItem.Controls.Add(Type:=msoControlButton)
With subMenuItem
.Caption = "Item 1"
.FaceId = 321
.OnAction = "firstMacro"
End With
Set subMenuItem = menuItem.Controls.Add(Type:=msoControlButton)
With subMenuItem
.Caption = "Item 2"
'.FaceId = 432
.Picture = stdole.StdFunctions.LoadPicture("C:\temp\test.bmp")
.OnAction = "secondMacro"
End With
CommandBars("Sub Menu Bar").ShowPopup
End Sub
I tested this and it appears to work fine for both FaceID's and loaded pictures.
我对此进行了测试,它似乎适用于 FaceID 和加载的图片。
Of course to get the "on run time" affect, I would recommend placing this in a function which was called each time the user clicked on a specific control.
当然,为了获得“运行时”效果,我建议将其放置在每次用户单击特定控件时调用的函数中。
It could be further expended to handle variable pictures here as well.
在这里也可以进一步处理可变图片。
Hope this helps.
希望这可以帮助。