在word中动态创建菜单并处理菜单点击事件的代码--

发表于:2007-05-25来源:作者:点击数: 标签:点击菜单Word创建动态
Sub Create_Menu() Const Menu_Name As String = "My New Main_Menu" Dim Before_number As Integer Dim X As Integer On Error Resume Next CommandBars("Menu Bar").Controls(Menu_Name).Delete '删除最后菜单 Before_number = CommandBars("Menu Bar").Co

 


Sub Create_Menu()
    Const Menu_Name As String = "My New Main_Menu"
    Dim Before_number As Integer
    Dim X As Integer

    On Error Resume Next
    CommandBars("Menu Bar").Controls(Menu_Name).Delete '删除最后菜单
    Before_number = CommandBars("Menu Bar").Controls.Count + 1

    Err.Clear
    X = 1        '删除历史自定义菜单
    Do Until Err.Number <> 0
        CommandBars("Custom Popup " & X).Delete
        X = X + 1
    Loop
    Err.Clear
   
    CommandBars("Menu Bar").Controls.Add Type:=msoControlPopup, Before:=Before_number
    CommandBars("Menu Bar").Controls(Before_number).Caption = Menu_Name
    For X = 1 To 10 '这里可以加入数据库的访问代码
        CommandBars("Custom Popup 1").Controls.Add Type:=msoControlButton, Before:=X
        CommandBars("Custom Popup 1").Controls(X).Caption = "吸海垂虹" & X 
        CommandBars("Custom Popup 1").Controls(X).OnAction = "NewMacros.Proc_Menu"
    Next

End Sub

Sub Proc_Menu() '这里可以加入菜单的处理代码
    MsgBox  CommandBars.ActionControl.Caption 
End Sub

原文转自:http://www.ltesting.net