Saving email attachments with VBA - code *nearly* working

jamesgeorgewalker

New Member
Joined
Nov 13, 2024
Messages
3
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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 1
Solution
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
Brilliant, that worked!

I was so close :/

Thanks for your help - I really wish I was better at VBA, would make many thing easier
 
Upvote 0
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
I spoke too soon

not working today... hard to understand why it worked for one email and not another
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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