したがって、連絡先アイテム (共有フォルダーにある) からすべての添付ファイルを抽出する次のコードがあります。
Outlook._Application objOutlook; //declare Outlook application
objOutlook = new Outlook.Application(); //create it
Outlook._NameSpace objNS = objOutlook.Session; //create new session
Outlook.MAPIFolder oAllPublicFolders; //what it says on the tin
Outlook.MAPIFolder oPublicFolders; // as above
Outlook.MAPIFolder objContacts; //as above
Outlook.Items itmsFiltered; //the filtered items list
oPublicFolders = objNS.Folders["Public Folders"];
oAllPublicFolders = oPublicFolders.Folders["All Public Folders"];
objContacts = oAllPublicFolders.Folders["Global Contacts"];
itmsFiltered = objContacts.Items.Restrict(strFilter);//restrict the search to our filter terms
for (int i = 1; i <= itmsFiltered.Count; i++) //loop through filtered items
{
var item = itmsFiltered[i];
Contact ctctNew = new Contact(); //create new contact
foreach (Outlook.Attachment oa in item.Attachments)
{ ctctNew.ImportedAttachments.Add(oa); }
lstContacts.Add(ctctNew); // add to the list that will be displayed in the OLV
}
return lstContacts;
これはうまくいくようです。
次に、これらをファイルに保存しようとします。
if (!System.IO.Directory.Exists(strPath))
{ System.IO.Directory.CreateDirectory(strPath); }
foreach (Outlook.Attachment o in ctLoaded.ImportedAttachments)
{
string strFilePath = strPath + @"\" + o.FileName;
if (!File.Exists(strFilePath))
{
o.SaveAsFile(strPath + @"\" + o.FileName); //exception here
}
}
この最後のビットは、.msg ではないファイルタイプに対しては正常に機能します。つまり、.pdf ファイルなどに対しては正常に機能します。.msg ファイルの場合、次の例外が発生します。
An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Event Management System.exe
Additional information: Cannot save the attachment. Could not complete the operation. One or more parameter values are not valid.
誰でも光を当てることができますか?
ありがとう
更新: 保存に失敗するのは、一部の .msg ファイルだけであることがわかりました。それらのいくつかは正常に動作します。特定のユーザーが添付したファイルは機能しているように見えますが、他のユーザーが添付したファイルは機能していないようです。それらの取り付け方に関係しているのではないかと思いますか?
また、コードはファイルを保存しているように見えますが (例外がスローされているにもかかわらず)、破損しているように見えます - 関連するフォルダーに 3Kb の .msg ファイルが表示されます。Outlook でアイテムを開けません。「アイテムを読み取れません」というメッセージが表示されるだけです。ファイルを開こうとすると、Outlook からメッセージ ボックスが表示されます。