Saving email attachments with VBA - code *nearly* working

jamesgeorgewalker

New Member
Joined
Nov 13, 2024
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
Not strictly Excel, but I guess you folks know VBA.

At work, I receive steel test certificates by email, and want to automate saving them onto my PC into folders based on the subject of the email, it uses a rule in Outlook to identify the email, then this code to save the attachement(s).

At the moment it is creating the folders on my PC, but not saving the files into them. The code runs without stopping, but doesn't have the desired outcome.

Here's the code:

Public Sub Test_Cert_Save(MItem As Outlook.MailItem)
Dim sSaveFolder As String
Dim sSubject As String
sSubject = MItem.Subject

sSaveFolder = "C:\Users\James\Downloads\Attachments\" & sSubject & "\"

'If Dir(sSaveFolder, vbDirectory) = "" Then
If Dir(sSaveFolder) = "" Then
sSaveFolder = "C:\Users\James\Downloads\Attachments\" & sSubject & "\"
Shell ("cmd /c mkdir """ & sSaveFolder & """")
For Each oAttachment In MItem.Attachments
MItem.Attachments.SaveAsFile sSaveFolder & oAttachment.DisplayName
Next

'ElseIf Dir(sSaveFolder, vbDirectory) <> "" Then
ElseIf Dir(sSaveFolder) <> "" Then
sSaveFolder = "C:\Users\James\Downloads\Attachments\" & sSubject & "\"
For Each oAttachment In MItem.Attachments
oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
Next
End If

End Sub


(I've removed some irrelevant bits which I'm sure are working)

Can anyone help me with making the code actually put the email attachment into the folder I've created?

Thanks
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Try this:
VBA Code:
Public Sub Test_Cert_Save(MItem As Outlook.MailItem)

    Dim sSaveFolder As String
    Dim sSubject As String
    Dim oAttachment As Outlook.Attachment
    
    sSubject = MItem.Subject
    
    sSaveFolder = "C:\Users\James\Downloads\Attachments\" & sSubject & "\"    
    If Dir(sSaveFolder, vbDirectory) = vbNullString Then
        Shell "cmd /c mkdir """ & sSaveFolder & """"
    End If
    
    For Each oAttachment In MItem.Attachments
        oAttachment.SaveAsFile sSaveFolder & oAttachment.DisplayName
    Next

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,630
Messages
6,173,456
Members
452,514
Latest member
cjkelly15

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top