How to: Add Custom XML Parts to Documents by Using Application-Level Add-Ins

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

Microsoft Office version

  • Excel 2007

  • PowerPoint 2007

  • Word 2007

For more information, see Features Available by Application and Project Type.

You can store XML data in the following types of documents by creating a custom XML part in an application-level add-in:

  • A Microsoft Office Excel workbook.

  • A Microsoft Office Word document.

  • A Microsoft Office PowerPoint presentation.

For more information, see Custom XML Parts Overview.

To add a custom XML part to an Excel workbook

  1. Add a new Microsoft.Office.Core.CustomXMLPart object to the Microsoft.Office.Core.CustomXMLParts collection in the workbook. The Microsoft.Office.Core.CustomXMLPart contains the XML string that you want to store in the workbook.

    The following code example adds a custom XML part to a specified workbook.

    Private Sub AddCustomXmlPartToWorkbook(ByVal workbook As Excel.Workbook)
        Dim xmlString As String = _
            "<?xml version=""1.0"" encoding=""utf-8"" ?>" & _
                "<employees https://schemas.microsoft.com/vsto/samples"">" & _
                    "<employee>" & _
                        "<name>Karina Leal</name>" & _
                        "<hireDate>1999-04-01</hireDate>" & _
                        "<title>Manager</title>" & _
                    "</employee>" & _
                "</employees>" 
    
        Dim employeeXMLPart As Office.CustomXMLPart = _
            workbook.CustomXMLParts.Add(xmlString)
    End Sub
    
    private void AddCustomXmlPartToWorkbook(Excel.Workbook workbook)
    {
        string xmlString =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<employees xmlns=\"https://schemas.microsoft.com/vsto/samples\">" +
                "<employee>" +
                    "<name>Karina Leal</name>" +
                    "<hireDate>1999-04-01</hireDate>" +
                    "<title>Manager</title>" +
                "</employee>" +
            "</employees>";
    
        Office.CustomXMLPart employeeXMLPart = workbook.CustomXMLParts.Add(xmlString, missing);
    }
    
  2. Add the AddCustomXmlPartToWorkbook method to the ThisAddIn class in an application-level project for Excel 2007.

  3. Call the method from other code in your project. For example, to create the custom XML part when the user opens a workbook, call the method from an event handler for the Microsoft.Office.Interop.Excel.Application.WorkbookOpen event.

To add a custom XML part to a Word document

  1. Add a new Microsoft.Office.Core.CustomXMLPart object to the Microsoft.Office.Core.CustomXMLParts collection in the document. The Microsoft.Office.Core.CustomXMLPart contains the XML string that you want to store in the document.

    The following code example adds a custom XML part to a specified document.

    Private Sub AddCustomXmlPartToActiveDocument(ByVal document As Word.Document)
        Dim xmlString As String = _
            "<?xml version=""1.0"" encoding=""utf-8"" ?>" & _
                "<employees https://schemas.microsoft.com/vsto/samples"">" & _
                    "<employee>" & _
                        "<name>Karina Leal</name>" & _
                        "<hireDate>1999-04-01</hireDate>" & _
                        "<title>Manager</title>" & _
                    "</employee>" & _
                "</employees>" 
    
        Dim employeeXMLPart As Office.CustomXMLPart = _
            document.CustomXMLParts.Add(xmlString)
    End Sub
    
    private void AddCustomXmlPartToActiveDocument(Word.Document document)
    {
        string xmlString =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<employees xmlns=\"https://schemas.microsoft.com/vsto/samples\">" +
                "<employee>" +
                    "<name>Karina Leal</name>" +
                    "<hireDate>1999-04-01</hireDate>" +
                    "<title>Manager</title>" +
                "</employee>" +
            "</employees>";
    
        Office.CustomXMLPart employeeXMLPart = document.CustomXMLParts.Add(xmlString, missing);
    }
    
  2. Add the AddCustomXmlPartToDocument method to the ThisAddIn class in an application-level project for Word 2007.

  3. Call the method from other code in your project. For example, to create the custom XML part when the user opens a document, call the method from an event handler for the Microsoft.Office.Interop.Word.Application.DocumentOpen event.

To add a custom XML part to a PowerPoint presentation

  1. Add a new Microsoft.Office.Core.CustomXMLPart object to the Microsoft.Office.Core.CustomXMLParts collection in the presentation. The Microsoft.Office.Core.CustomXMLPart contains the XML string that you want to store in the presentation.

    The following code example adds a custom XML part to a specified presentation.

    Private Sub AddCustomXmlPartToPresentation(ByVal presentation As PowerPoint.Presentation)
        Dim xmlString As String = _
            "<?xml version=""1.0"" encoding=""utf-8"" ?>" & _
                "<employees https://schemas.microsoft.com/vsto/samples"">" & _
                    "<employee>" & _
                        "<name>Karina Leal</name>" & _
                        "<hireDate>1999-04-01</hireDate>" & _
                        "<title>Manager</title>" & _
                    "</employee>" & _
                "</employees>" 
        Dim employeeXMLPart As Office.CustomXMLPart = _
            presentation.CustomXMLParts.Add(xmlString)
    End Sub
    
    private void AddCustomXmlPartToPresentation(PowerPoint.Presentation presentation)
    {
        string xmlString =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<employees xmlns=\"https://schemas.microsoft.com/vsto/samples\">" +
                "<employee>" +
                    "<name>Karina Leal</name>" +
                    "<hireDate>1999-04-01</hireDate>" +
                    "<title>Manager</title>" +
                "</employee>" +
            "</employees>";
    
        Office.CustomXMLPart employeeXMLPart = 
            presentation.CustomXMLParts.Add(xmlString, missing);
    }
    
  2. Add the AddCustomXmlPartToPresentation method to the ThisAddIn class in an application-level project for PowerPoint 2007.

  3. Call the method from other code in your project. For example, to create the custom XML part when the user opens a presentation, call the method from an event handler for the Microsoft.Office.Interop.PowerPoint.Application.AfterPresentationOpen event.

Robust Programming

For simplicity, this example uses an XML string that is defined as a local variable in the method. Typically, you should obtain the XML from an external source, such as a file or a database.

See Also

Tasks

How to: Add Custom XML Parts to Document-Level Customizations

How to: Add Custom XML Parts to Documents Without Starting Microsoft Office

Concepts

Custom XML Parts Overview