我正在尝试使用Word文档中的一些嵌入式对象。较早的海报告诉我,这不是直截了当的。这是链接答案的摘录:
“正如我之前提到的,利用嵌入式对象的编程模型执行保存是一种捷径。还有一种更复杂的解决方案可以与任何嵌入式对象一起使用。为了首先将对象嵌入,必须支持COM IPersist接口之一(即IPersistStorage,IPersistStreamInit,IPersistFile等),因此,始终可以通过在OLEFormat.Object上调用Marshal.QueryInterface(以确定适当的持久性接口)来提取嵌入式对象,并进行相应的转换并然后调用适当的方法。根据所使用的持久性接口,您可能需要调用一些其他方法,以在文件顶部显示适当的存储空间。此外,根据嵌入对象的类型,您可能仍需要先激活对象,然后才能成功为持久性接口使用QueryInterface。”
因此,我有兴趣公开对象正在实现哪个接口。我能找到的最接近的是这里。到目前为止,下面的代码非常感谢Marshal.QueryInterface的任何帮助。
// Opening the word document
object missing = Type.Missing;
this.document = wordApp.Documents.Open(
ref fn, ref confirmConversions, ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
foreach (Microsoft.Office.Interop.Word.InlineShape inlineShape in this.document.InlineShapes)
{
if (inlineShape.OLEFormat.ProgID != null)
{
switch (inlineShape.OLEFormat.ProgID)
{
// This is a pdf file
case "AcroExch.Document.7":
//Marshal.QueryInterface(IntPtr pUnk, ref Guid iid, out IntPtr ppv);
break;
default:
break;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Marshal.QueryInterface不需要-如果您将COM对象转换为COM接口类型,则.NET会QueryInterface为您进行调用。也就是说,您可以编写:IPersistStorage persist = (IPersistStorage) obj;
但是我不清楚代码中的哪个对象实现了IPersistStorage,IPersistStreamInit等等。