【发布时间】: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