I am currently developing a VSTO AddIn for Microsoft Word using VB.NET. My primary goal is to access the "Document Library Properties" of Word documents. It's essential for my project to directly access these properties within the Word environment, rather than through SharePoint. This is particularly crucial because I aim to retrieve these properties even if the file is no longer stored in SharePoint. Is it even possible to access these properties in the Word environment? The properties are still visible under "File --> Information" even if the file is no longer stored in SharePoint. To clear up any misunderstandings, here is a description of the various document properties. MS Support about document properties
I have already tried to access the "BuiltInDocumentProperties" and "CustomDocumentProperties". However, I never got the correct values back.
Does anyone have experience or insights regarding this? I would appreciate any guidance or suggestions. Thank you to everyone who takes the time to assist!
Edit: I have migrated my Word AddIn from VB.net to C#.
When I try to retrieve the values via ActiveDocument.ContentTypeProperties in a loop, I get an error for all except ‘Description, DocId and DocUrlId’. Also for simple text values.
This is the code:
var doc = Globals.ThisAddIn.Application.ActiveDocument;
var propcount = doc.ContentTypeProperties.Count; // This returns the correct count of ContentTypeProperties
var props = doc.ContentTypeProperties;
foreach (MetaProperty item in props)
{
try
{
var name = item.Name;
var value = item.Value;
}
catch (Exception ex)
{
}
}
Solution:
I have retrieved the properties (Document Library Properties) directly from the customXMLParts with the NamespaceUri ‘http://schemas.microsoft.com/office/2006/metadata/properties’.
Thanks jonsson for your help!
Working from distant memory here...
Have a look at ActiveDocument.ContentTypeProperties
Some gotchas:
If the document doesn't have any, you get an untrappable error when you access this collection, I think.
Some columns may not be present in the collection - for example, calculated columns.
The individual items can be of many different types, and may have internal structure that you may need to work out to avoid runtime errors when retrieving the values. For example, a column containing a hyperlink (whether "just" a hyperlink or a link to a picture) will probably come through as a string array with elements numbered 0 and 1. Some types may even cause Word to crash when you try to retrieve the type using e.g. the TypeName() function (e.g. in tests here, a lookup type does that with the old 2013 version of Word I'm using for that.) For that reason, it may be safer to work with the data actually stored in the relevant CustomXMLPart.
The values are also stored in a Custom XML Part which is described by the XML that you can retrieve using the SchemaXml property. The Schema Xml isn't particularly easy to interpret but may be helpful
You should be able to retrieve the schema part (it actually contains more than one schema) using e.g.
Dim schemacxp As CustomXMLPart
Dim cxps As CustomXMLParts
Set cxps = ActiveDocument.CustomXMLParts.SelectByNameSpace("http://schemas.microsoft.com/office/2006/metadata/contentType")
If Not (cxps Is Nothing) Then
Set schemacxp = cxps(1)
End If
You should be able to retrieve the data part in a similar way, but looking for the NamespaceUri "http://schemas.microsoft.com/office/2006/metadata/properties" instead.
You may find that the names used in the XML are not always exactly the same as the ones defined in SharePoint and that you have to match them up in some way, e.g. by using ContentTypeProperties.GetItemByInternalName, but I'd have to revisit that so see if it is at all useful.