title | description | ms.date | ms.topic | dev_langs | helpviewer_keywords | author | ms.author | manager | ms.subservice | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Add custom XML parts to documents using VSTO Add-ins |
Learn how you can store XML data in the following types of documents by creating a custom XML part in a VSTO Add-in. |
02/02/2017 |
how-to |
|
|
John-Hart |
johnhart |
mijacobs |
office-development |
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.
-
Add a new xref:Microsoft.Office.Core.CustomXMLPart object to the xref:Microsoft.Office.Interop.Excel._Workbook.CustomXMLParts%2A collection in the workbook. The xref: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.
:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_AddCustomXmlPartExcelAppLevel/ThisAddIn.cs" id="Snippet1":::
-
Add the
AddCustomXmlPartToWorkbook
method to theThisAddIn
class in a 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 xref:Microsoft.Office.Interop.Excel.AppEvents_Event.WorkbookOpen event.
-
Add a new xref:Microsoft.Office.Core.CustomXMLPart object to the xref:Microsoft.Office.Interop.Word._Document.CustomXMLParts%2A collection in the document. The xref: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.
:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_AddCustomXmlPartWordAppLevel/ThisAddIn.cs" id="Snippet1":::
-
Add the
AddCustomXmlPartToDocument
method to theThisAddIn
class in a 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 xref:Microsoft.Office.Interop.Word.ApplicationEvents4_Event.DocumentOpen event.
-
Add a new xref:Microsoft.Office.Core.CustomXMLPart object to the Microsoft.Office.Interop.PowerPoint._Presentation.CustomXMLParts collection in the presentation. The xref: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.
:::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_AddCustomXmlPartPowerPointAppLevel/ThisAddIn.cs" id="Snippet1":::
-
Add the
AddCustomXmlPartToPresentation
method to theThisAddIn
class in a 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 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.