Error saving attachments when they are embedded

后端 未结 2 644
没有蜡笔的小新
没有蜡笔的小新 2021-01-29 02:22

I\'m saving Outlook attachments (as part of a copy).

I get an error message from the line objAtt.SaveAsFile strFile when the attachment is an embedded image

2条回答
  •  我在风中等你
    2021-01-29 02:54

    First of all, make sure the file path is fully qualified, i.e. you end up with a valid string here:

    strFile = strPath & objAtt.FileName
    

    Second, when you call the Attachments.Add make sure the file exists on the disk. The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.

    You may try to run the following code which saves an attachment on the disk:

    Sub SaveAttachment()  
     Dim myInspector As Outlook.Inspector  
     Dim myItem As Outlook.MailItem  
     Dim myAttachments As Outlook.Attachments 
    
     Set myInspector = Application.ActiveInspector  
     If Not TypeName(myInspector) = "Nothing" Then  
       If TypeName(myInspector.CurrentItem) = "MailItem" Then  
         Set myItem = myInspector.CurrentItem  
         Set myAttachments = myItem.Attachments  
    
         'Prompt the user for confirmation  
         Dim strPrompt As String  
         strPrompt = "Are you sure you want to save the first attachment " & _  
         "in the current item to the Documents folder? If a file with the " & _  
         "same name already exists in the destination folder, " & _  
         "it will be overwritten with this copy of the file."  
    
         If MsgBox(strPrompt, vbYesNo + vbQuestion) = vbYes Then  
           myAttachments.Item(1).SaveAsFile Environ("HOMEPATH") & "\My Documents\" & _  
           myAttachments.Item(1).DisplayName  
         End If  
       Else  
         MsgBox "The item is of the wrong type."  
       End If  
     End If  
    End Sub
    

提交回复
热议问题