We are developing a VSTO Document-level customisation for MS word. We need to access the document from a background thread so that we do not stop the UI refreshing.
This works fine for properties such as Sections / Table etc on DocumentBase.
When trying to access CustomDocumentProperties or BuiltInDocumentProperties the following exception is received
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Core.DocumentProperties'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{2DF8D04D-5BFA-101B-BDE5-00AA0044DE52}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Is it possible to access these properties from a background thread?
Thanks
First of all, you should not access the Office object model from background threads because Office applications using the single threaded apartment model.
Use the late-binding technology for accessing the document properties, see the Type.InvokeMember method for more information. For example:
object properties = workBk.GetType().InvokeMember("CustomDocumentProperties", BindingFlags.Default | BindingFlags.GetProperty, null, workBk, null);
object property = properties.GetType().InvokeMember("Item", BindingFlags.Default | BindingFlags.GetProperty, null, properties, new object[] { propertyIndex });
object propertyValue = property.GetType().InvokeMember("Value", BindingFlags.Default | BindingFlags.GetProperty, null, propertyWrapper.Object, null);
Also you may take a look at the similar pages:
User contributions licensed under CC BY-SA 3.0