Microsoft Project的RibbonXml VBA项目中不能使用带参数的过程

如果在Microsoft Project 使用以下代码:

Sub gallery_MSN_getItemCount(control As IRibbonControl, ByRef returnedVal)
     
     On Error Resume Next
     returnedVal = 12

End Sub

Public Sub gallery_MSN_getItemLabels(control As IRibbonControl, index As Integer, ByRef returnedVal)
 'This callback runs for every item (label).
 'This example uses the values in the array for Label names.
    
     Dim Labelname As Variant
     On Error Resume Next
     Labelname = _
     Array("Sheila Webster", _
           "Brian Main", _
           "Susan Zhang", _
           "Anne Walzer", _
           "Andrea Vogel", _
           "Ronda Viescas", _
           "Norman Harker", _
           "Michelle Wells", _
           "Wilma Yang", _
           "Angel Wang", _
           "Raymond Denny", _
           "June Winograd")

    On Error Resume Next
     returnedVal = Labelname(index)
     On Error GoTo 0


 End Sub

Sub gallery_MSN_Click(control As IRibbonControl, id As String, index As Integer)

'Call the macro that belongs to the label when you click one of the labels.
 'Example: When you click the first label it runs the macro named "macro_1".
     On Error Resume Next
     MsgBox ("It works !")
     On Error GoTo 0

End Sub

Sub test()
    Dim strXML As String
    
    strXML = "<mso:customUI xmlns:mso=""http://schemas.microsoft.com/office/2009/07/customui"">"
     strXML = strXML & "<mso:ribbon>"
     strXML = strXML & "<mso:qat/>"
     strXML = strXML & "     <mso:tabs>"
     strXML = strXML & "     <mso:tab id=""PlannersTab"" label=""Planners"" insertBeforeQ=""mso:TabResource"">"
     
     strXML = strXML & "         <mso:group id=""GroupMove"" label=""Move"" autoScale=""true"">"



    strXML = strXML & "         <mso:gallery  id=""MSN"" "
     strXML = strXML & "                label=""Go to MSN"" "
     strXML = strXML & "                imageMso=""MenuTaskWellArrange"" "
     strXML = strXML & "                size=""large"""

     strXML = strXML & "                columns=""3"" "
     strXML = strXML & "                rows=""10"" "

    strXML = strXML & "                getItemCount=""gallery_MSN_getItemCount"" "
     strXML = strXML & "                getItemLabel=""gallery_MSN_getItemLabels"" "
     
     strXML = strXML & "                showItemLabel=""true"" "
      
     strXML = strXML & "                onAction=""gallery_MSN_Click"" >"

    strXML = strXML & "          </mso:gallery>"


    strXML = strXML & "         </mso:group>"
     strXML = strXML & "     </mso:tab>"
     strXML = strXML & "     </mso:tabs>"
     strXML = strXML & "</mso:ribbon>"
     strXML = strXML & "</mso:customUI>"
     
 ActiveProject.SetCustomUI (strXML)

End Sub

会报"Automation error"的错误。其原因在于不能使用带项目的参数。如果改成下面这个样子就可以了:

Option Explicit

Sub gallery_MSN_Click()

 'Call the macro that belongs to the label when you click one of the labels.
 'Example: When you click the first label it runs the macro named "macro_1".
     On Error Resume Next
     MsgBox ("It works")
     On Error GoTo 0

End Sub

Private Sub AddHighlightRibbon()
    Dim ribbonXml As String
    Dim MyArray As Variant
    Dim item As Variant
    Dim cnt As Integer
    
    cnt = 0
    MyArray = Array("Sheila Webster", "Brian Main", "Susan Zhang", "Anne Walzer", "Andrea Vogel", "Ronda Viescas", _
           "Norman Harker", _
           "Michelle Wells", _
           "Wilma Yang", _
           "Angel Wang", _
           "Raymond Denny", _
           "June Winograd")
           
    ribbonXml = "<mso:customUI xmlns:mso=""http://schemas.microsoft.com/office/2009/07/customui"">"
    ribbonXml = ribbonXml + "  <mso:ribbon>"
    ribbonXml = ribbonXml + "    <mso:qat/>"
    ribbonXml = ribbonXml + "    <mso:tabs>"
    ribbonXml = ribbonXml + "      <mso:tab id=""highlightTab"" label=""Highlight"" insertBeforeQ=""mso:TabFormat"">"
    ribbonXml = ribbonXml + "        <mso:group id=""testGroup"" label=""Test"" autoScale=""true"">"
    ribbonXml = ribbonXml + "         <mso:gallery  id=""MSN"" "
    ribbonXml = ribbonXml + "                label=""Go to MSN"" "
    ribbonXml = ribbonXml + "                imageMso=""MenuTaskWellArrange"" "
    ribbonXml = ribbonXml + "                size=""large"""
    ribbonXml = ribbonXml + "                columns=""3"" "
    ribbonXml = ribbonXml + "                rows=""10"" "
    ribbonXml = ribbonXml + "                showItemLabel=""true"" "
    ribbonXml = ribbonXml + "                onAction=""gallery_MSN_Click"" >"
    For Each item In MyArray
        ribbonXml = ribbonXml + "               <mso:item id=""item" + CStr(cnt) + """ label=""" + item + """></mso:item>"
        cnt = cnt + 1
    Next
    ribbonXml = ribbonXml + "          </mso:gallery>"
    ribbonXml = ribbonXml + "        </mso:group>"
    ribbonXml = ribbonXml + "      </mso:tab>"
    ribbonXml = ribbonXml + "    </mso:tabs>"
    ribbonXml = ribbonXml + "  </mso:ribbon>"
    ribbonXml = ribbonXml + "</mso:customUI>"
    ActiveProject.SetCustomUI (ribbonXml)
End Sub



欢迎访问《 许阳的红泥屋》
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ribbon XML is used to customize the Ribbon in Microsoft Office applications, including Word. Here is an example of how to create a custom Ribbon using VBA in Word: 1. Open Word and press Alt+F11 to open the Visual Basic Editor. 2. In the Project Explorer, right-click on your document project and select Insert > Module. 3. In the new module, write the following code to create a custom Ribbon: ``` Sub AddCustomRibbon() Dim ribbonXML As String ribbonXML = "<customUI xmlns='http://schemas.microsoft.com/office/2009/07/customui'>" _ & "<ribbon>" _ & "<tabs>" _ & "<tab id='CustomTab' label='My Tab'>" _ & "<group id='CustomGroup' label='My Group'>" _ & "<button id='CustomButton' label='My Button' onAction='MyMacro' imageMso='HappyFace'/>" _ & "</group>" _ & "</tab>" _ & "</tabs>" _ & "</ribbon>" _ & "</customUI>" CustomUI = ribbonXML End Sub ``` 4. In the Ribbon XML code, you can customize the tab, group, and button labels and IDs, as well as the onAction and imageMso properties of the button. 5. Save the module and close the Visual Basic Editor. 6. In Word, go to File > Options > Customize Ribbon. 7. In the right-hand column, select Macros and then click the New Group button to create a new group in your custom tab. 8. Select your new group and click the Rename button to give it a name. 9. In the left-hand column, select Macros and then select the macro you created in step 3. 10. Click the Add button to add the macro to your new group. 11. Click OK to close the Options dialog box. 12. Your custom Ribbon should now be visible in Word with your custom tab, group, and button.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值