Selkie
Selkie

Reputation: 1255

Outlook attachments not downloading automatically or properly - using a script

I have a short Outlook VBA macro that downloads the attachments from specific e-mails to a specific folder.

My settings are as follows:

Apply this rule after the message arrives
From ******@***.com
And on this computer only
Move it to the pricing folder
And run Project1.saveAttachtoDrive

For the outlook rules.

Macro Settings: Default on the pictures, "Enable all macros" on the Macro settings.

The code:

Public Sub saveAttachtoDrive(itm As Outlook.MailItem)
'Created by Selkie in order to automatically download attachments
Dim objAtt As Outlook.Attachment
'Shortening things
Dim dt As Date
'The date
Dim saveFolder As String
'We need to save the file somewhere
dt = Format(Date, "MMDDYY")
'Gotta get the date, and need it in a useable format
saveFolder = "L:\*******\Testing Folder\"
'Save location - change as needed
     For Each objAtt In itm.Attachments
     'For every attachment we get
          objAtt.SaveAsFile saveFolder & dt & objAtt.DisplayName
          'Save it
          Set objAtt = Nothing
     Next
End Sub

Now, when I run something fairly similar, but as a folder scrape instead of a trigger when getting an email, I do manage to download the attachments in a given file.

Am I doing something obviously wrong? Is there some Outlook setting I need to have enabled? Or have I completely borked the code? (Looks pretty similar to things I've seen in 3-4 different locations, just the location, comments, and adding in the date are unique"

Upvotes: 2

Views: 713

Answers (1)

Tim Williams
Tim Williams

Reputation: 166530

Your issue is that your filename is not what you think it is, because you've declared dt as Date, so the string output from Format is coerced back into a Date instead of remaining as a string.

Sub Tester()

    Dim dt As Date                '<<
    dt = Format(Date, "MMDDYY")
    Debug.Print dt                '>>  1/5/1986 :same as CDate("031417")

    Dim dt2 As String             '<<   Note
    dt2 = Format(Date, "MMDDYY")
    Debug.Print dt2               '>>  031417

End Sub

Upvotes: 2

Related Questions