2014-01-19 66 views
0

我試圖保存txt文件,這是msg的附件,它是傳入電子郵件附件到我的Outlook。我想出瞭如何做到這一點,它工作正常,我在這裏張貼我的解決方案。但它不是最有效的解決方案,因爲它首先將附加的msg文件保存到我的硬盤驅動器,然後再次用程序CreateItemFromTemplate讀取它,並最終保存附加的txt文件。如何將郵件的附件本身保存爲傳入電子郵件的附件?

我試圖存儲在我的硬盤中的txt文件相當大。當Outlook顯示節省提示時,每次打開Outlook時都會浪費時間下載郵件內容兩次。

這裏是我的嘗試:

Sub saveESGMtoDisk(itm As Outlook.MailItem) 
    Dim objAtt As Outlook.Attachment 
    Dim objAtt_sec As Outlook.Attachment 
    Dim oMsg As Outlook.MailItem 
    Dim saveFolder As String: saveFolder = "C:\test" 
    Dim i As Integer: i = 0 

    Dim oApp As Outlook.Application 
    Set oApp = New Outlook.Application 

    For Each objAtt In itm.Attachments 
     If Left(objAtt.DisplayName,7) = "txtdata" Then 

      On Error Resume Next 
      objAtt.SaveAsFile (saveFolder & "\Item[" & i & "].msg") 
      If Err.Number Then objAtt.SaveAsFile (saveFolder & "\Item[" & i & "].msg") 

      Set oMsg = oApp.CreateItemFromTemplate(saveFolder & "\Item[" & i & "].msg") 

      For Each objAtt_sec In oMsg.Attachments 
       If Right(objAtt_sec.DisplayName, 3) = "txt" Then _ 
        OobjAtt_sec.SaveAsFile saveFolder & "\" & objAtt_sec.DisplayName 
       Set objAtt_sec = Nothing 
      Next 

      Kill saveFolder & "\Item[" & i & "].msg" 
      Set oMsg = Nothing 
      i = i + 1 

     End If 
     Set objAtt = Nothing 
    Next 
End Sub 
+0

你見過[這](http://stackoverflow.com/問題/ 10241659/save-email-attachments-to-a-network-location/10243178#10243178)和[THIS](http://stackoverflow.com/questions/11781320/download-attachment-from-outlook-and-open -in-的excel/11782153#11782153) –

回答

0

Outlook對象模型不會讓你直接訪問嵌入郵件附件 - 如果你正在使用C++或Delphi,你可以使用擴展MAPI打開使用IAttach嵌入郵件附件:: OpenProperty(PR_ATTACH_DATA_OBJ,IID_IMessage)。

或者你可以使用Redemption(它包裝擴展MAPI,可以從任何語言來使用),並做類似如下:

RDOSession session = new RDOSession(); 
RDOMail msg = (RDOMail)session.GetRDOObjectFromOutlookObject(YourOutlookMailItem); 
foreach (RDOAttachment att in msg.Attachments) 
{ 
    if (att.Type == rdoAttachmentType.olEmbeddedItem) 
    { 
     RDOMail embeddedMsg = att.EmbeddedMsg; 
     ProcessEmbeddedMessage(embeddedMsg); 
    } 
}