Rediger

Del via


Add custom XML parts to documents by using VSTO Add-ins

You can store XML data in the following types of documents by creating a custom XML part in a VSTO Add-in:

To add a custom XML part to an Excel workbook

  1. Add a new CustomXMLPart object to the CustomXMLParts collection in the workbook. The 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 void AddCustomXmlPartToWorkbook(Excel.Workbook workbook)
    {
        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 = workbook.CustomXMLParts.Add(xmlString, missing);
    }
    
  2. Add the AddCustomXmlPartToWorkbook method to the ThisAddIn class in a VSTO Add-in project for Excel.

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

To add a custom XML part to a Word document

  1. Add a new CustomXMLPart object to the CustomXMLParts collection in the document. The 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 void AddCustomXmlPartToActiveDocument(Word.Document document)
    {
        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 = document.CustomXMLParts.Add(xmlString, missing);
    }
    
  2. Add the AddCustomXmlPartToDocument method to the ThisAddIn class in a VSTO Add-in project for Word.

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

To add a custom XML part to a PowerPoint presentation

  1. Add a new CustomXMLPart object to the Microsoft.Office.Interop.PowerPoint._Presentation.CustomXMLParts collection in the presentation. The 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 void AddCustomXmlPartToPresentation(PowerPoint.Presentation presentation)
    {
        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 = 
            presentation.CustomXMLParts.Add(xmlString, missing);
    }
    
  2. Add the AddCustomXmlPartToPresentation method to the ThisAddIn class in a VSTO Add-in project for PowerPoint.

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