找到了附件,但没有保存文件。在调试时,它似乎是成功节省的,但是文件不在测试路径中。
private void button1_Click(object sender, RibbonControlEventArgs e)
{
Outlook.Inspector currInspector = null;
Outlook.MailItem mail = null;
Outlook.Attachments attachments = null;
//change to production paths when working, below are test paths
string path = @"H:Customer Service";
string archivePath = @"H:Customer ServiceHelpers";
try
{
currInspector = Globals.ThisAddIn.Application.ActiveWindow();
mail = (Outlook.MailItem)currInspector.CurrentItem;
attachments = mail.Attachments;
for (int i = 1; i <= attachments.Count; i++)
{
Outlook.Attachment vendFile = attachments[i];
//save original in archive folder
vendFile.SaveAsFile(archivePath + vendFile);
//begin document modification and save to path
//StreamWriter streamWriter = new StreamWriter(new FileStream(path, FileMode.Create, FileAccess.ReadWrite));
//dispose of object
Marshal.ReleaseComObject(vendFile);
}
}
catch
{
MessageBox.Show("Unable to retrieve attachment");
}
finally
{
if (attachments != null) Marshal.ReleaseComObject(attachments);
if (mail != null) Marshal.ReleaseComObject(mail);
if (currInspector != null) Marshal.ReleaseComObject(currInspector);
}
}
}
您需要指定SaveAsFile
方法的完全资格的路径和文件名,而不仅仅是文件夹路径:
string filename = Path.Combine(archivePath , vendFile.FileName);
vendFile.SaveAsFile(filename);