How to: 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:
A Microsoft Office Excel workbook.
A Microsoft Office Word document.
A Microsoft Office PowerPoint presentation.
For more information, see Custom XML Parts Overview.
Applies to: The information in this topic applies to application-level projects for Excel, PowerPoint, and Word. For more information, see Features Available by Office Application and Project Type.
To add a custom XML part to an Excel workbook
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.
Add the
AddCustomXmlPartToWorkbookmethod to theThisAddInclass in an VSTO Add-in project for Excel.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
Add a new CustomXMLPart object to the P:Microsoft.Office.Interop.Word._Document.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.
Add the
AddCustomXmlPartToDocumentmethod to theThisAddInclass in an VSTO Add-in project for Word.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 E:Microsoft.Office.Interop.Word.ApplicationEvents4_Event.DocumentOpen event.
To add a custom XML part to a PowerPoint presentation
Add a new CustomXMLPart object to the P: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.
Add the
AddCustomXmlPartToPresentationmethod to theThisAddInclass in an VSTO Add-in project for PowerPoint.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 E:Microsoft.Office.Interop.PowerPoint.EApplication_Event.AfterPresentationOpen event.
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.
Custom XML Parts Overview
How to: Add Custom XML Parts to Document-Level Customizations