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 |
|
|
John-Hart |
johnhart |
mijacobs |
office-development |
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.
-
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":::
-
Add the
AddCustomXmlPartToWorkbook
method to theThisWorkbook
class in a document-level project for Excel. -
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.
-
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":::
-
Add the
AddCustomXmlPartToDocument
method to theThisDocument
class in a document-level project for Word. -
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.
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.