使用 Open XML 格式檔案自訂 Office Fluent 功能區

The ribbon component of the Microsoft Office Fluent user interface gives users a flexible way to work with Office applications. Ribbon Extensibility (RibbonX) uses a simple, text-based, declarative XML markup to create and customize the ribbon.

本主題中的程式碼範例示範如何將自訂群組件新增至單一檔的功能區,而不是新增應用層級自訂。 在下列步驟中,您會將自訂索引標籤、自訂群組和自訂按鈕新增至 Word 中的現有功能區。 並且為在文件中插入公司名稱的按鈕實作回呼程序。

  1. 在桌面上建立名為 [customUI] 的資料夾,然後將 XML 自訂檔案複製至該資料夾。

  2. 使用自訂的結構描述驗證 XML 標記。

     <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui"> 
       <ribbon> 
         <tabs> 
           <tab id="CustomTab" label="My Tab"> 
             <group id="SampleGroup" label="Sample Group"> 
               <button id="Button" label="Insert Company Name" size="large" onAction="ThisDocument.InsertCompanyName" /> 
             </group > 
           </tab> 
         </tabs> 
       </ribbon> 
     </customUI> 
    
  3. Create a folder on your desktop named customUI and copy the XML customization file to the folder.

  4. Validate the XML markup with a custom schema.

    注意事項

    這是選擇性的步驟。

  5. 在 Word 中建立檔,並以 RibbonSample.docm名稱儲存檔。

  6. 開啟 Visual Basic 編輯器,並將下列程式新增至 ThisDocument 程式 代碼模組。 關閉並儲存檔。

      Sub InsertCompanyName(ByVal control As IRibbonControl) 
      ' Inserts the specified text at the beginning of a range or selection. 
      Dim MyText As String 
      Dim MyRange As Object 
      Set MyRange = ActiveDocument.Range 
      MyText = "Microsoft Corporation" 
      ' Range Example: Inserts text at the beginning 
      ' of the active document 
      MyRange.InsertBefore (MyText) 
      ' Selection Example: 
      'Selection.InsertBefore (MyText) 
    End Sub 
    
    
  7. Add a .zip extension to the document file name and then double-click it to open the file.

  8. Add the customization file to the container by dragging the customUI folder from the desktop to the .zip file.

  9. Extract the .rels file to your desktop. A _rels folder that contains the .rels file is copied to your desktop.

  10. Open the .rels file and add the following line between the last Relationship tag and the Relationships tag. This creates a relationship between the document file and the customization file.

     <Relationship Id="someID" Type="http://schemas.microsoft.com/office/2006/relationships/ui/extensibility" Target="customUI/customUI.xml" />
    
  11. 移除 .zip 副檔名,將檔案重新命名為原始名稱。

  12. .rels 資料夾從桌面拖曳回到容器檔案,並覆寫現有的檔案。

  13. Rename the document file to its original name by removing the .zip extension.

  14. Open the document and notice that the ribbon now displays the My Tab tab.

  15. 選擇索引標籤,並注意具有按鈕控制項的范 例群 組群組。

  16. 選擇按鈕以將公司名稱插入檔中。

另請參閱

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應