Custom Data Store In Word For VSTO Solutions

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 = Microsoft.Office.Core;

......

Office.DocumentProperties properties = (Core.DocumentProperties)doc.BuiltInDocumentProperties;

properties ["Title"].Value = yourvalue;

Globals.ThisAddIn.Application.ActiveDocument.Saved = false; //important sometime

.....

Warning: limited 255 chars.

All of the BuiltinDocumentProperties:

1) Title
2) Subject
3) Author
4) Keywords
5) Comments
6) Template
7) Last Author
8) Revision Number
9) Application Name
10) Last Print Date
11) Creation Date
12) Last Save Time
13) Total Editing Time
14) Number of Pages
15) Number of Words
16) Number of Characters
17) Security
18) Category
19) Format
20) Manager
21) Company
22) Number of Bytes
23) Number of Lines
24) Number of Paragraphs
25) Number of Slides
26) Number of Notes
27) Number of Hidden Slides
28) Number of Multimedia Clips
29) Hyperlink Base
30) Number of Characters (with spaces)

Working with Custom Document Properties

When you want to store custom data, you may use Custom DocumentProperties as follows:

1. Save:

Microsoft.Office.Core.DocumentProperties properties= (DocumentProperties)Globals.ThisAddIn.Application.ActiveDocument.CustomDocumentProperties;

string propertyName = YourCustomDefinePropertyName;

if (properties.Cast<DocumentProperty>().Where(c => c.Name == propertyName).Count() == 0)

{

properties.Add(propertyName, false, MsoDocProperties.msoPropertyTypeString, _storeContainer[i].Expression);

}

else

{

properties[propertyName].Value = _storeContainer[i].Expression ;

}

Globals.ThisAddIn.Application.ActiveDocument.Saved = false; //important sometime

Warning: limited 255 chars.

2. Read:

String queryResult = String.Empty;

Microsoft.Office.Core.DocumentProperties properties = (DocumentProperties)Globals.ThisAddIn.Application.ActiveDocument.CustomDocumentProperties;

string propertyName = YourCustomDefinePropertyName;

if (properties.Cast<DocumentProperty>().Where(c => c.Name == propertyName).Count() > 0)

{

queryResult = properties[propertyName].Value;

}

Else

{

queryResult = String.Empty;

}

Working with Custom Xml Store

When you want store your custom xml to word document, you can try the following:

1. Save:

CustomXMLPart targetPart = null;

String mustSaveContent = yourStoreContent;

String yourXmlRootName = "yourXmlRootname";

foreach(CustomXMLPart eachPart in Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts)

{

if (eachPart.DocumentElement.BaseName == yourXmlRootName)

{

targetPart = eachPart;

break;

}

}

if (targetPart != null)

{

targetPart.Delete();

}

targetPart = Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts.Add(mustSaveContent, System.Reflection.Missing.Value);

Globals.ThisAddIn.Application.ActiveDocument.Saved = false; //important sometime

2. Read:

CustomXMLPart ?targetPart = null;

String queryResultXml = String.Empty;

String yourXmlRootName = "yourXmlRootname";

Foreach(CustomXMLPart eachPart in Globals.ThisAddIn.Application.ActiveDocument.CustomXMLParts)

{

if (eachPart.DocumentElement.BaseName == yourXmlRootName)

{

targetPart = eachPart;

break;

}

}

if (targetPart != null)

{

queryResultXml = targetPart.XML;

}