{"id":5679,"date":"2013-06-28T14:23:03","date_gmt":"2013-06-28T06:23:03","guid":{"rendered":"http:\/\/www.boostsolutions.com\/blog\/?p=5679"},"modified":"2023-07-31T11:38:53","modified_gmt":"2023-07-31T03:38:53","slug":"custom-data-store-in-word-for-vsto-solutions","status":"publish","type":"post","link":"https:\/\/www.boostsolutions.com\/blog\/custom-data-store-in-word-for-vsto-solutions\/","title":{"rendered":"Custom Data Store In Word For VSTO Solutions"},"content":{"rendered":"<h3 style=\"color: #0b72ba;\">Working with Document Properties<\/h3>\n<p>The DocumentProperties collection and DocumentProperty object are found in Microsoft Office Object Library (office.dll), its namespace is Microsoft.Office.Core, which contains objects shared by all the Office applications. These objects are typically brought into your code in an Office namespace alias as shown here:<\/p>\n<pre>using Office = Microsoft.Office.Core;\r\n\r\nkey codes:\r\n\r\nusing Office = Microsoft.Office.Core;\r\n\r\n......\r\n\r\nOffice.DocumentProperties properties = (Core.DocumentProperties)doc.BuiltInDocumentProperties;\r\n\r\nproperties [\"Title\"].Value = yourvalue;\r\n\r\nGlobals.ThisAddIn.Application.ActiveDocument.Saved = false; \/\/important sometime\r\n\r\n.....\r\n\r\nWarning: limited 255 chars.<\/pre>\n<p>All of the BuiltinDocumentProperties:<\/p>\n<p>1) Title<br \/>\n2) Subject<br \/>\n3) Author<br \/>\n4) Keywords<br \/>\n5) Comments<br \/>\n6) Template<br \/>\n7) Last Author<br \/>\n8) Revision Number<br \/>\n9) Application Name<br \/>\n10) Last Print Date<br \/>\n11) Creation Date<br \/>\n12) Last Save Time<br \/>\n13) Total Editing Time<br \/>\n14) Number of Pages<br \/>\n15) Number of Words<br \/>\n16) Number of Characters<br \/>\n17) Security<br \/>\n18) Category<br \/>\n19) Format<br \/>\n20) Manager<br \/>\n21) Company<br \/>\n22) Number of Bytes<br \/>\n23) Number of Lines<br \/>\n24) Number of Paragraphs<br \/>\n25) Number of Slides<br \/>\n26) Number of Notes<br \/>\n27) Number of Hidden Slides<br \/>\n28) Number of Multimedia Clips<br \/>\n29) Hyperlink Base<br \/>\n30) Number of Characters (with spaces)<\/p>\n<h3 style=\"color: #0b72ba;\">Working with Custom Document Properties<\/h3>\n<p>When you want to store custom data, you may use Custom DocumentProperties as follows:<\/p>\n<p>1. Save:<\/p>\n<pre>Microsoft.Office.Core.DocumentProperties properties= (DocumentProperties)Globals.ThisAddIn.Application.ActiveDocument.CustomDocumentProperties;\r\n\r\nstring propertyName = YourCustomDefinePropertyName;\r\n\r\nif (properties.Cast&lt;DocumentProperty&gt;().Where(c =&gt; c.Name == propertyName).Count() == 0)\r\n\r\n{\r\n\r\nproperties.Add(propertyName, false, MsoDocProperties.msoPropertyTypeString, _storeContainer[i].Expression);\r\n\r\n}\r\n\r\nelse\r\n\r\n{\r\n\r\nproperties[propertyName].Value = _storeContainer[i].Expression ;\r\n\r\n}\r\n\r\nGlobals.ThisAddIn.Application.ActiveDocument.Saved = false; \/\/important sometime\r\n\r\nWarning: limited 255 chars.<\/pre>\n<p>2. Read:<\/p>\n<pre>String queryResult = String.Empty;\r\n\r\nMicrosoft.Office.Core.DocumentProperties properties = (DocumentProperties)Globals.ThisAddIn.Application.ActiveDocument.CustomDocumentProperties;\r\n\r\nstring propertyName = YourCustomDefinePropertyName;\r\n\r\nif (properties.Cast&lt;DocumentProperty&gt;().Where(c =&gt; c.Name == propertyName).Count() &gt; 0)\r\n\r\n{\r\n\r\nqueryResult = properties[propertyName].Value;\r\n\r\n}\r\n\r\nElse\r\n\r\n{\r\n\r\nqueryResult = String.Empty;\r\n\r\n}<\/pre>\n<h3 style=\"color: #0b72ba;\">Working with Custom Xml Store<\/h3>\n<p>When you want store your custom xml to word document, you can try the following:<\/p>\n<p>1. Save:<\/p>\n<pre>CustomXMLPart targetPart = null;\r\n\r\nString mustSaveContent = yourStoreContent;\r\n\r\nString yourXmlRootName = \"yourXmlRootname\";\r\n\r\nforeach(CustomXMLPart eachPart in Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts)\r\n\r\n{\r\n\r\nif (eachPart.DocumentElement.BaseName == yourXmlRootName)\r\n\r\n{\r\n\r\ntargetPart = eachPart;\r\n\r\nbreak;\r\n\r\n}\r\n\r\n}\r\n\r\nif (targetPart != null)\r\n\r\n{\r\n\r\ntargetPart.Delete();\r\n\r\n}\r\n\r\ntargetPart = Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts.Add(mustSaveContent, System.Reflection.Missing.Value);\r\n\r\nGlobals.ThisAddIn.Application.ActiveDocument.Saved = false; \/\/important sometime<\/pre>\n<p>2. Read:<\/p>\n<pre>CustomXMLPart ?targetPart = null;\r\n\r\nString queryResultXml = String.Empty;\r\n\r\nString yourXmlRootName = \"yourXmlRootname\";\r\n\r\nForeach(CustomXMLPart eachPart in Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts)\r\n\r\n{\r\n\r\nif (eachPart.DocumentElement.BaseName == yourXmlRootName)\r\n\r\n{\r\n\r\ntargetPart = eachPart;\r\n\r\nbreak;\r\n\r\n}\r\n\r\n}\r\n\r\nif (targetPart != null)\r\n\r\n{\r\n\r\nqueryResultXml = targetPart.XML;\r\n\r\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Working with Document Properties The DocumentProperties collection and DocumentProperty object are found in Microsoft Office Object Library (office.dll), its namespace is Microsoft.Office.Core, which contains objects shared by all the Office applications. These objects are typically brought into your code in an Office namespace alias as shown here: using Office = Microsoft.Office.Core; key codes: using Office [&hellip;]<\/p>\n","protected":false},"author":65,"featured_media":5697,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5679"}],"collection":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/users\/65"}],"replies":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=5679"}],"version-history":[{"count":17,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5679\/revisions"}],"predecessor-version":[{"id":5698,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/5679\/revisions\/5698"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/media\/5697"}],"wp:attachment":[{"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=5679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=5679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.boostsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=5679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}