设置 CommandBar“弹出”动态子菜单图标

发布于 2024-10-13 04:05:33 字数 769 浏览 1 评论 0原文

我只是想确认这一点:在 Office 2003 中,我想在运行时创建一个自定义子菜单(在 CommandBar 中称为弹出窗口 (msoControlPopup)),并为其设置一个图像。对于 CommandBarButton,这非常简单,

Set btn1 = mnu.Controls.Add(msoControlButton, temporary:=True)
btn1.Caption = "Btn1"
btn1.Picture = stdole.LoadPicture("C:\temp\test.bmp")

但对于 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")

msoControlPopup 类型似乎也不允许 .Style 属性,这就是 Office 确定要显示的内容的方式在控件上显示-图标、文本、两者。我还没有发现这一点得到证实,所以我抱着最后的希望,认为我做错了什么,事实上,有一种方法可以在运行时在子菜单上插入图标。

感谢您能提供的任何光芒。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

洒一地阳光 2024-10-20 04:05:33

好吧,还有更多的风滚草。我很确定这个问题的答案是,这是不可能的。这是我的“证据”:所有内置子菜单都没有图标(直到我发布上述内容之后我才意识到这一点,如果您运行上面的代码,请转到“工具”>“在菜单栏中自定义,然后单击“测试”菜单将其下拉,然后右键单击“Sub1”,您应该看到所有按钮和样式选项都变灰。右键单击“Btn1”,它们是可用的

。想法仍然受欢迎。

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.

岁月静好 2024-10-20 04:05:33

当然,如果您需要设置子菜单标题的图像或 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

我对此进行了测试,它似乎工作正常对于 FaceID 和加载的图片。

当然,为了获得“运行时”效果,我建议将其放置在一个函数中,每次用户单击特定控件时都会调用该函数。

它也可以进一步扩展以处理此处的可变图片。

希望这有帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文