Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

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

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

  • Document-level projects

Microsoft Office version

  • Excel 2007

  • Word 2007

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

You can store XML data in a Microsoft Office Excel workbook or Microsoft Office Word document by creating a custom XML part in a document-level customization. For more information, see Custom XML Parts Overview.

NoteNote:

Visual Studio Tools for Office does not provide document-level projects for Microsoft Office PowerPoint. For information about adding a custom XML part to a PowerPoint presentation by using an application-level add-in, see How to: Add Custom XML Parts to Documents by Using Application-Level Add-Ins.

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.

    private void AddCustomXmlPartToWorkbook()
    {
        string xmlString =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<employees xmlns=\"http://schemas.microsoft.com/vsto/samples\">" +
                "<employee>" +
                    "<name>Karina Leal</name>" +
                    "<hireDate>1999-04-01</hireDate>" +
                    "<title>Manager</title>" +
                "</employee>" +
            "</employees>";
    
        Office.CustomXMLPart employeeXMLPart = this.CustomXMLParts.Add(xmlString, missing);
    }
    
  2. Add the AddCustomXmlPartToWorkbook method to the ThisWorkbook class in a document-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 the workbook, call the method from the ThisWorkbook_Startup event handler.

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.

    private void AddCustomXmlPartToDocument()
    {
        string xmlString =
            "<?xml version=\"1.0\" encoding=\"utf-8\" ?>" +
            "<employees xmlns=\"http://schemas.microsoft.com/vsto/samples\">" +
                "<employee>" +
                    "<name>Karina Leal</name>" +
                    "<hireDate>1999-04-01</hireDate>" +
                    "<title>Manager</title>" +
                "</employee>" +
            "</employees>";
    
        Office.CustomXMLPart employeeXMLPart = this.CustomXMLParts.Add(xmlString, missing);
    }
    
  2. Add the AddCustomXmlPartToDocument method to the ThisDocument class in a document-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 the document, call the method from the ThisDocument_Startup event handler.

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.

Community Additions

ADD
Show:
© 2015 Microsoft