Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
楼主: 事后诸葛亮

[已解决]VB中制作COM加载项时,如何添加子菜单

[复制链接]
发表于 2013-7-19 11:21 | 显示全部楼层
  1. Dim oXL As Object
  2. Dim mnMain As Office.CommandBarPopup
  3. Dim mnMain2 As Office.CommandBarPopup
  4. Dim WithEvents mn1 As Office.CommandBarButton
  5. Dim WithEvents mn2 As Office.CommandBarButton
  6. Dim WithEvents MyButton As Office.CommandBarButton

  7. Private Sub AddinInstance_OnConnection(ByVal Application As Object, _
  8.                                        ByVal ConnectMode As AddInDesignerObjects.ext_ConnectMode, _
  9.                                        ByVal AddInInst As Object, custom() As Variant)


  10.     On Error Resume Next

  11.     Set oXL = Application
  12.     oXL.CommandBars("Worksheet Menu Bar").Controls("期末处理(K)").Delete
  13.     Set mnMain = oXL.CommandBars("Worksheet Menu Bar").Controls.Add(Type:=msoControlPopup, Before:=11)
  14.     mnMain.Caption = "期末处理(K)"
  15.     Set mn1 = mnMain.Controls.Add(Type:=msoControlButton, Before:=1)
  16.     With mn1
  17.         .Caption = "移动1"
  18.         .OnAction = "!<" & AddInInst.ProgId & ">"
  19.         .FaceId = 162
  20.         '.Visible = True
  21.     End With
  22.     Set mnMain2 = mnMain.Controls.Add(Type:=msoControlPopup, Before:=2)
  23.     With mnMain2
  24.         .Caption = "移动2"
  25.         .BeginGroup = True    '增加间隔符
  26.         '.OnAction = "!<" & AddInInst.ProgId & ">"
  27.         '.FaceId = 300
  28.         '.Visible = True
  29.         Set mn1 = .Controls.Add(Type:=msoControlButton)
  30.         With mn1
  31.             .Caption = "子菜单1"
  32.             .Visible = True
  33.         End With
  34.         Set mn2 = .Controls.Add(Type:=msoControlButton)
  35.         With mn2
  36.             .Caption = "子菜单2"
  37.             .Visible = True
  38.         End With


  39.     End With

  40. End Sub

  41. Private Sub AddinInstance_OnDisconnection(ByVal RemoveMode As _
  42.                                           AddInDesignerObjects.ext_DisconnectMode, custom() As Variant)
  43.     On Error Resume Next
  44.     MsgBox "My Addin was disconnected by " & _
  45.            IIf(RemoveMode = ext_dm_HostShutdown, _
  46.                "Excel shutdown.", "end user.")
  47.     mn2.Delete
  48.     mn1.Delete
  49.     mnMain.Delete
  50.     Set mn2 = Nothing
  51.     Set mn1 = Nothing
  52.     Set mnMain = Nothing
  53.     Set oXL = Nothing
  54. End Sub

  55. Private Sub mn1_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
  56.     MsgBox "Hello!"
  57. End Sub

  58. Private Sub mn2_Click(ByVal Ctrl As Office.CommandBarButton, CancelDefault As Boolean)
  59.     MsgBox "Hi!"
  60. End Sub
复制代码
回复

使用道具 举报

发表于 2013-7-19 11:23 | 显示全部楼层
回复

使用道具 举报

发表于 2013-7-19 11:23 | 显示全部楼层
因为之前类型是COMMANDBUTTON的类型,就不会有CONTROLS属性了。
必须是COMMANDPOPUP才成。
回复

使用道具 举报

 楼主| 发表于 2013-7-19 11:27 | 显示全部楼层
hwc2ycy 发表于 2013-7-19 11:23

把源文件打包传给我可以么?谢谢了、。
回复

使用道具 举报

发表于 2013-7-19 11:31 | 显示全部楼层
你直接把我贴的源代码复制过去,原来的代码删除就行了。
回复

使用道具 举报

发表于 2013-7-19 11:32 | 显示全部楼层    本楼为最佳答案   
自定义菜单(COM加载项).rar (12.21 KB, 下载次数: 76)
回复

使用道具 举报

 楼主| 发表于 2013-7-19 11:51 | 显示全部楼层
hwc2ycy 发表于 2013-7-19 11:32

刚才在研究代码,没来的及说谢谢。

Before:=11    Before:=2  的是什么意思呀?

.OnAction = "!<" & AddInInst.ProgId & ">" 又是什么意思?
谢谢咯
回复

使用道具 举报

发表于 2013-7-19 12:25 | 显示全部楼层
Before 可选 Variant 一个指示新控件在命令栏上位置的数值。新控件将插入到位于此位置的控件之前。如果忽略该参数,控件将添加到指定命令栏的末端。
回复

使用道具 举报

发表于 2013-7-19 12:25 | 显示全部楼层
AddIn.progID 属性
返回对象的程序标识符。String 型,只读。
语法

表达式.progID

表达式   一个代表 AddIn 对象的变量。

示例


此示例为第一张工作表中所有 OLE 对象创建程序标识符列表。

Visual Basic for Applications
rw = 0
For Each o in Worksheets(1).OLEObjects
    With Worksheets(2)
        rw = rw + 1
        .cells(rw, 1).Value = o.ProgId
    End With
Next
回复

使用道具 举报

发表于 2013-7-24 13:35 | 显示全部楼层
这个也没给最佳呀,
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-4 07:27 , Processed in 0.351610 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表