EAA
EAA

Reputation: 87

How to Download Attachment that is an Excel file?

I want to download attachments from email going to a specific folder. I wrote a VBA code and set a rule for emails going in that folder.

They're all xlsx. Been processing them manually for months, and it's always the same file.

When it downloads the attachment, it saves it as a "File" Type, not xlsx. If I open it, it is all random nonsense.

¬Ia‹YéÎÞé}yý¶ª;ßëÖëê—zõ^CBš¥Î¹]U]u·ÿ~ÿzoÝGôÉñÉñ!8„w1uÒä

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)

    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim strFileType As String
    Dim DtString As String
    Dim SName As String
    Dim File_Name As String
    Dim Full_Path As String
    
    saveFolder = "\\path\path\path\path\Path\"
    
    strFileType = ".xlsx"
    DtString = Format(Now(), "mm-dd")
    SName = itm.SenderName
    File_Name = DtString & "_" & SName
    Full_Path = saveFolder & "\" & File_Name
    
    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile Full_Path
        Set objAtt = Nothing
    Next
         
End Sub

I tried adding ".xslx" at the end, I tried opening an instance of Excel, opening the file, save and close.

I tried saving it as a Txt file.

Upvotes: 0

Views: 1316

Answers (3)

0m3r
0m3r

Reputation: 12497

Should be

File_Name = DtString & "_" & SName & strFileType

Example

Option Explicit
Private Sub saveAttachtoDisk(itm As Outlook.MailItem)
    Dim objAtt As Outlook.Attachment
    Dim saveFolder As String
    Dim strFileType As String
    Dim DtString As String
    Dim SName As String
    Dim File_Name As String
    Dim Full_Path As String

    saveFolder = "\\path\path\path\path\Path\"

    strFileType = ".xlsx"

    DtString = Format(Now(), "mm-dd")

    SName = itm.SenderName

    File_Name = DtString & "_" & SName & strFileType

    Full_Path = saveFolder & File_Name
    Debug.Print Full_Path ' print on Immediate Window

    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile Full_Path
        Set objAtt = Nothing
    Next

End Sub

Upvotes: 0

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66286

Use the attachment file name.

objAtt.SaveAsFile saveFolder & "\" & objAtt.FileName

Upvotes: 0

tsdn
tsdn

Reputation: 427

change:

Full_Path = saveFolder & "\" & File_Name

to:

Full_Path = saveFolder & "\" & File_Name & strFileType

Upvotes: 0

Related Questions