如何获取 Word 文档的附加文件?
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能指的是 OLE,它在 Office 文档中大量使用。来自维基百科文章:http://en.wikipedia.org/wiki/Object_Linking_and_Embedding
该网站最初看起来与您的问题无关,但是,它就是正在使用的网站。
如果您想跳过肉,请直接向下滚动到底部,您将在其中找到“外部链接”: http://www.pldaniels.com/ripole/
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
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/
您可以尝试使用
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
正如 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.