如何获取 Word 文档的附加文件?

发布于 2024-10-18 12:11:56 字数 1821 浏览 3 评论 0原文

我正在使用 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 不使用附件,但我不知道它叫什么。有什么想法吗?

I am using Microsoft.Office.Interop.Word to access Word documents through c#. Some of the Word documents have objects inside them. This is the equivalent of email attachments.

To insert some file in a Word document in Word 2007, you go to Insert -> Object -> Object... and select some file.

My question is, how do I get the file out using C#?

Here is an example of how it is done with an email using 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;
}

Apparently the Microsoft.Office.Interop.Word doesn't use attachments, but then I don't know what it is called. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冰之心 2024-10-25 12:11:56

您可能指的是 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 许可的,这意味着商业项目也可以使用该代码,而无需担心许可费用或法律责任。

You may be referring to OLE, which is heavily used in Office documents. From the wikipedia article: http://en.wikipedia.org/wiki/Object_Linking_and_Embedding

Object Linking and Embedding (OLE) is a technology developed by Microsoft that allows embedding and linking to documents and other objects. For developers, it brought OLE Control eXtension (OCX), a way to develop and use custom user interface elements. On a technical level, an OLE object is any object that implements the IOleObject interface, possibly along with a wide range of other interfaces, depending on the object's needs.

That website will initially look to be unrelated to your question, however, It's what is being used.

If you want to skip the meat, scroll right down to the bottom where you'll find an 'external link' to: http://www.pldaniels.com/ripole/

ripOLE is a small program/library designed to pull out attachments from OLE2 data files (ie, MS Office documents). ripOLE is BSD licenced meaning that commercial projects can also use the code without worry of licence costs or legal liabilities.

妄断弥空 2024-10-25 12:11:56

您可以尝试使用 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-我们/library/bb448854(v=office.12).aspx

You could try using the System.IO.Packaging classes to read the data. A Word 2007 file is just a zip file, so the objects you're after are probably inside in a format you can read.

There's a collection of articles on MSDN titled "Word 2007 Visual How Tos" that might be of some use:
http://msdn.microsoft.com/en-us/library/gg537324(v=office.12).aspx

You can read about the Open XML Format SDK here:
http://msdn.microsoft.com/en-us/library/bb448854(v=office.12).aspx

从﹋此江山别 2024-10-25 12:11:56

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

As said by Arafangion they are OLE objects, for most of them if you know what they are you could ask them to export their content somewhere else see Extract embedded document with the word document for other you may need to extract the binary content and hope that your user could find an application to read it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文