Skip to content

Latest commit

 

History

History
66 lines (49 loc) · 3.86 KB

how-to-add-custom-xml-parts-to-document-level-customizations.md

File metadata and controls

66 lines (49 loc) · 3.86 KB
title description titleSuffix ms.date ms.topic dev_langs helpviewer_keywords author ms.author manager ms.subservice
Add custom XML parts to doc-level customizations (Visual Studio)
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.
02/02/2017
how-to
VB
CSharp
document-level customizations [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
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
John-Hart
johnhart
mijacobs
office-development

Add custom XML parts to document-level customizations

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.

[!INCLUDEappliesto_alldoc]

Note

Visual Studio does not provide document-level projects for Microsoft Office PowerPoint. For information about adding a custom XML part to a PowerPoint presentation by using a VSTO Add-in, see How to: Add custom XML parts to documents by using VSTO Add-ins.

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.Core.CustomXMLParts collection in the workbook. The xref:Microsoft.Office.Core.CustomXMLPart contains the XML string that you want to store in the workbook.

    :::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_AddCustomXmlPartExcelDocLevel/ThisWorkbook.cs" id="Snippet1":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/Trin_AddCustomXmlPartExcelDocLevel/ThisWorkbook.vb" id="Snippet1":::

  2. Add the AddCustomXmlPartToWorkbook method to the ThisWorkbook class in a document-level 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 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 xref:Microsoft.Office.Core.CustomXMLPart object to the xref:Microsoft.Office.Core.CustomXMLParts collection in the document. The xref:Microsoft.Office.Core.CustomXMLPart contains the XML string that you want to store in the document.

    :::code language="csharp" source="../vsto/codesnippet/CSharp/Trin_AddCustomXmlPartWordDocLevel/ThisDocument.cs" id="Snippet1":::

    :::code language="vb" source="../vsto/codesnippet/VisualBasic/Trin_AddCustomXmlPartWordDocLevel/ThisDocument.vb" id="Snippet1":::

  2. Add the AddCustomXmlPartToDocument method to the ThisDocument class in a document-level 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 the document, call the method from the ThisDocument_Startup event handler.

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