2017-05-09 178 views
0
Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem) 
    Dim oAttachment As Outlook.Attachment 
    Dim sSaveFolder As String 
    sSaveFolder = "C:\Users\axt112\Desktop\downloads\" 
    For Each oAttachment In MItem.Attachments 
    If oAttachment = "Checkpoint Volume and Movement Times*" Then oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName 
    Next 
End Sub 

我使用Outlook VBA代碼將特定文件的附件保存在指定的文件夾中。規則運行這個腳本,我沒有看到任何規則錯誤,所以我假設它的代碼。SaveAttachmentsToDisk VBA代碼不能正常工作

這很奇怪,因爲它上週運行良好,我沒有改變任何東西。你們在代碼中看到什麼奇怪的東西嗎?

謝謝。

+0

嘗試在'Then'後面的同一行中移動'oAttachment.SaveAsFile ...',或者在'oAttachment.SaveAsFile ...'之後插入'End if'作爲新行' – Tehscript

+0

它已經在那之後,我試圖結束,但如果它不工作:/ – arthur6523

+2

'如果oAttachment喜歡「檢查點數量和運動時間*」那麼'除非你的附件有這個確切的名字......也可能應該'如果oAttachment.DisplayName' –

回答

0

您無法比較VBA中的Outlook對象和字符串。很可能您希望比較Attachment類的DisplayName屬性,該類返回表示名稱的字符串,該名稱不必是實際文件名,顯示在代表嵌入式附件的圖標下方。

Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem) 
    Dim oAttachment As Outlook.Attachment 
    Dim sSaveFolder As String 
    sSaveFolder = "C:\Users\axt112\Desktop\downloads\" 
    For Each oAttachment In MItem.Attachments 
    If oAttachment.DisplayName = "Checkpoint Volume and Movement Times*" Then   
     oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName 
    Next 
End Sub 

,或者使用類似功能:

Public Sub SaveAttachmentsToDisk(MItem As Outlook.MailItem) 
    Dim oAttachment As Outlook.Attachment 
    Dim sSaveFolder As String 
    sSaveFolder = "C:\Users\axt112\Desktop\downloads\" 
    For Each oAttachment In MItem.Attachments 
    If oAttachment.DisplayName Like "Checkpoint Volume and Movement Times*" Then   
     oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName 
    Next 
End Sub 

你可能會發現Getting Started with VBA in Outlook 2010文章很有幫助。