I'm just trying to confirm this: In Office 2003, I want to create a custom submenu--what is known in 开发者_Python百科CommandBar parlance as a popup (msoControlPopup)--at runtime, and set an image for it. With a CommandBarButton, this is very straightforward
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
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.
Thanks for any light you can shed.
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 after I 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.
Any other thoughts still welcome.
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 here to accomplish this:
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.
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.
精彩评论