0

I am trying to save a Excel (.xlsm) file which is in an email. I get this email through another email.

I want to download the said .xlsm file to a shared drive. Looking up multiple online forums and MSDN help I wrote the following code:

When I run this code I don't get any error. Nor are any files saved.

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim msgAtt As Outlook.Attachment
    Dim saveFolder As String
   ' a to i are dummy names and have been replaced with correct folder path in real code
    saveFolder = "\\a\b\c\d\e\f\g\h\i"
    For Each objAtt In itm.Attachments
        If InStr(objAtt.DisplayName, ".msg") Then
            For Each msgAtt In itm.Attachments
                If InStr(msgAtt.DisplayName, ".xlsm") Then
                    msgAtt.SaveAsFile saveFolder & "\" & msgAtt.DisplayName
                End If
            Next
        Set msgAtt = Nothing
    End If
Next
Set objAtt = Nothing
End Sub
Community
  • 1
  • 1
  • 2
    Possible duplicate of [VBA Code to save an attachment (excel file) from an Outlook email that was inside another email as an attachment](https://stackoverflow.com/questions/7890612/vba-code-to-save-an-attachment-excel-file-from-an-outlook-email-that-was-insid) – niton Sep 11 '17 at 14:06
  • @niton I agree that the outcome desired is the same but the particular problem faced in this case is different than that case – Abhishek Modi Sep 13 '17 at 05:48

1 Answers1

0

I found the solution while I run the code line by line.

The if statement says to look for ".msg" and ".xlsm" word in the displayname of the attachment. On investigation, the display name don't take file extension into display name.

Hence to solve this, ".msg" and ".xlsm" had to be replaced by the file name.

Thanks,