【问题标题】:How do I get files that are attached to a word document?如何获取附加到 Word 文档的文件?
【发布时间】:2011-02-21 09:51:22
【问题描述】:

我正在使用 Microsoft.Office.Interop.Word 通过 c# 访问 Word 文档。一些 Word 文档中包含对象。这相当于电子邮件附件。

要在 Word 2007 的 Word 文档中插入一些文件,请转到插入 -> 对象 -> 对象...并选择一些文件。

我的问题是,如何使用 C# 获取文件?

以下是如何使用 Outlook 处理电子邮件的示例:

    protected Microsoft.Office.Interop.Outlook.ApplicationClass outlookApplication = null;
    protected Microsoft.Office.Interop.Outlook._MailItem        mailItem           = null;
    protected Microsoft.Office.Interop.Outlook.NameSpace        mapi               = null;

public OutlookFileExtracter(string filename, string contentPrefix, int startAttachmentNumber)
    this.outlookApplication = new Microsoft.Office.Interop.Outlook.ApplicationClass();
    this.mapi = outlookApplication.GetNamespace("MAPI");
    mailItem = mapi.OpenSharedItem(filename) as Microsoft.Office.Interop.Outlook._MailItem;
}

public Collection<string> GetFileNames()
{
    String extension;
    if (this.fileNamesOrig == null)
    {
        int numberOfFiles = this.mailItem.Attachments.Count;

        this.fileNamesOrig = new Collection<string>();
        this.fileNamesDest = new Collection<string>();
        this.fileValidBools = new Collection<bool>();

        for (int i = 0; i < numberOfFiles; i++)
        {
            //First attachment number is 1
            fileNamesOrig.Add(this.mailItem.Attachments[i + 1].FileName);
            this.fileValidBools.Add(false);
        }

        for (int la = 0; la < numberOfFiles; la++)
        {
            extension = Path.GetExtension(fileNamesOrig[la]).ToUpper().Trim('.');
            this.fileNamesDest.Add(this.contentPrefix + (this.startAttachmentNumber + la) + "." + extension);
        }
    }
    return this.fileNamesOrig;
}

显然 Microsoft.Office.Interop.Word 不使用附件,但是我不知道它叫什么。有什么想法吗?

【问题讨论】:

  • 除非您有不寻常的设置,否则 Word 对象与 Outlook 附件完全不同。这篇文章是 VBScript,但它可能会有所帮助:stackoverflow.com/questions/2735218/vbs-and-multilevel-ole/…
  • +1 在帖子 remou 提到。这正是您在 Word 文档中检索嵌入的 OLE 对象所需要做的事情。

标签: c# ms-word office-interop


【解决方案1】:

您可能指的是 OLE,它在 Office 文档中大量使用。来自维基百科文章:http://en.wikipedia.org/wiki/Object_Linking_and_Embedding

对象链接和嵌入 (OLE) 是 Microsoft 开发的一种技术,允许嵌入和链接到文档和其他对象。对于开发人员,它带来了 OLE Con​​trol eXtension (OCX),一种开发和使用自定义用户界面元素的方法。在技​​术层面上,OLE 对象是任何实现 IOleObject 接口的对象,根据对象的需要,可能还包括各种其他接口。

该网站最初看起来与您的问题无关,但是,它是正在使用的。

如果您想跳过肉,请向下滚动到底部,您会在此处找到“外部链接”:http://www.pldaniels.com/ripole/

ripOLE 是一个小程序/库,旨在从 OLE2 数据文件(即 MS Office 文档)中提取附件。 ripOLE 是 BSD 许可的,这意味着商业项目也可以使用该代码,而无需担心许可成本或法律责任。

【讨论】:

    【解决方案2】:

    您可以尝试使用System.IO.Packaging 类来读取数据。 Word 2007 文件只是一个 zip 文件,因此您需要的对象可能以您可以阅读的格式保存在其中。

    在 MSDN 上有一系列标题为“Word 2007 Visual How Tos”的文章可能会有一些用处: http://msdn.microsoft.com/en-us/library/gg537324(v=office.12).aspx

    您可以在此处阅读有关 Open XML 格式 SDK 的信息: http://msdn.microsoft.com/en-us/library/bb448854(v=office.12).aspx

    【讨论】:

      【解决方案3】:

      正如 Arafangion 所说,它们是 OLE 对象,对于它们中的大多数,如果你知道它们是什么,你可以要求它们将它们的内容导出到其他地方,请参阅 Extract embedded document with the word document 对于其他你可能需要提取二进制内容并希望你的用户可以找到一个应用程序来阅读它。

      【讨论】:

        猜你喜欢
        • 2017-03-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-22
        • 2013-12-13
        • 1970-01-01
        相关资源
        最近更新 更多