文档级自定义线程

本文关键字:线程 自定义 文档 | 更新日期: 2023-09-27 18:01:30

我们正在为MS word开发VSTO文档级定制。我们需要从后台线程访问文档,这样我们就不会停止UI刷新。

对于DocumentBase上的Sections/Table等属性可以很好地工作。

当尝试访问CustomDocumentProperties或BuiltInDocumentProperties时,收到以下异常

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)).

是否可以从后台线程访问这些属性?

谢谢

文档级自定义线程

首先,您不应该从后台线程访问Office对象模型,因为Office应用程序使用单线程公寓模型。

使用后绑定技术访问文档属性,参见Type。方法获取更多信息。例如:

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);

你也可以看看类似的页面:

使用Word互操作设置自定义文档属性

以编程方式访问Excel自定义文档属性