Skip to content

Latest commit

 

History

History
93 lines (64 loc) · 5.42 KB

how-to-add-custom-xml-parts-to-documents-by-using-vsto-add-ins.md

File metadata and controls

93 lines (64 loc) · 5.42 KB
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
VB
CSharp
add-ins [Office development in Visual Studio], custom XML parts
Office documents [Office development in Visual Studio, custom XML parts
Word [Office development in Visual Studio], custom XML parts
PowerPoint [Office development in Visual Studio], custom XML parts
Excel [Office development in Visual Studio], custom XML parts
custom XML parts [Office development in Visual Studio], adding
documents [Office development in Visual Studio], custom XML parts
application-level add-ins [Office development in Visual Studio], custom XML parts
John-Hart
johnhart
mijacobs
office-development

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 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":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/trin_addcustomxmlpartexcelapplevel/ThisAddIn.vb" id="Snippet1":::

  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 xref:Microsoft.Office.Interop.Excel.AppEvents_Event.WorkbookOpen event.

To add a custom XML part to a Word document

  1. 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":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/Trin_AddCustomXmlPartWordAppLevel/ThisAddIn.vb" id="Snippet1":::

  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 xref:Microsoft.Office.Interop.Word.ApplicationEvents4_Event.DocumentOpen event.

To add a custom XML part to a PowerPoint presentation

  1. 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":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/Trin_AddCustomXmlPartPowerPointAppLevel/ThisAddIn.vb" id="Snippet1":::

  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.

Related content