Personalizar a Faixa de Opções do Office Fluent usando um arquivo de formatos Open XML

O componente da Faixa de Opções da interface do usuário do Microsoft Office Fluent proporciona aos usuários uma maneira flexível de trabalhar com os aplicativos do Office. Ribbon Extensibility (RibbonX) uses a simple, text-based, declarative XML markup to create and customize the ribbon.

The code example in this topic shows how to add custom components to the ribbon for a single document, as opposed to adding application-level customizations. In the following steps, you add a custom tab, a custom group, and a custom button to the existing ribbon in Microsoft Word. You also implement a callback procedure for the button that inserts a company name into the document.

  1. Create the customization file in any text editor and save the file with the name customUI.xml.

  2. Add the following XML markup to the file and then close and save the file.

     <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.

    Observação

    Essa etapa é opcional.

  5. Crie um documento no Word e salve-o com o nome RibbonSample.docm.

  6. Abra o Editor do Visual Basic e adicione o procedimento a seguir ao módulo de código ThisDocument . Feche e salve o documento.

      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. Salve e feche o arquivo.

  12. Adicione a pasta .rels ao arquivo do contêiner arrastando-a da área de trabalho, substituindo o arquivo existente.

  13. Renomeie o arquivo do documento com o nome original, removendo a extensão .zip.

  14. Abra o documento e observe que a faixa de opções agora exibe a guia Minha guia.

  15. Clique na guia e veja o grupo Grupo de exemplo com um controle de botão.

  16. Feche o botão para inserir o nome da empresa no documento.

Confira também

Suporte e comentários

Tem dúvidas ou quer enviar comentários sobre o VBA para Office ou sobre esta documentação? Confira Suporte e comentários sobre o VBA para Office a fim de obter orientação sobre as maneiras pelas quais você pode receber suporte e fornecer comentários.