send mail with all attachment in a folder

KlausW

Active Member
Joined
Sep 9, 2020
Messages
453
Office Version
  1. 2016
Platform
  1. Windows
Hello
I use this VBA code to send mail with an attachment and it works as it should.
Someone who can help so that it sends all the files in a folder.
The name of the folder is in cell x18.
Anyone who can help?
Any help will be appreciated.
Best Regards
Klaus W

VBA Code:
Sub maj_Rektangelafrundedehjørner1_Klik()
'Skal bruges
Dim MyOutlook As Object
Set MyOutlook = CreateObject("Outlook.Application")

Dim MyMail As Object
Set MyMail = MyOutlook.CreateItem(olMailItem)

MyMail.To = Range("x2").Value ' & "; " & Range("y3").Value & "; " & Range("y4").Value & "; " & Range("y5").Value & "; " & Range("ay6").Value

MyMail.Subject = Range("x9").Value
MyMail.Body = Range("x10").Value & vbNewLine & vbNewLine & Range("x11").Value & vbNewLine & _
        Range("x12").Value & vbNewLine & Range("x13").Value & vbNewLine & Range("x14").Value & vbNewLine & _
        Range("x15").Value

Attached_File = Range("x18").Value
MyMail.Attachments.Add Attached_File

MyMail.Send

'MoveAFile

End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Untested. Replace:
VBA Code:
Attached_File = Range("x18").Value
MyMail.Attachments.Add Attached_File

with:

VBA Code:
Dim folder As String, file As String
folder = Range("x18").Value
If Right(folder, 1) <> "\" Then folder = folder & "\"
file = Dir(folder & "*.*")
While file <> vbNullString
    MyMail.Attachments.Add folder & file
    file = Dir()
Wend
 
Upvote 0
Using a FileSystemObject should be able to do this:

VBA Code:
Sub maj_Rektangelafrundedehjørner1_Klik()
    
    'Skal bruges
    Dim MyOutlook As Object
    Set MyOutlook = CreateObject("Outlook.Application")
    
    ' File system variables
    Dim folderName As String
    Dim FSOLibrary As Object
    Dim FSOFolder As Object
    Dim FileToAttach As Object
    
    'Set the file name to a variable
    folderName = Range("X18").Value2
    
    'Set all the references to the FSO Library
    Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
    Set FSOFolder = FSOLibrary.GetFolder(folderName)
    
    Dim MyMail As Object
    Set MyMail = MyOutlook.CreateItem(olMailItem)
    
    MyMail.To = Range("x2").Value ' & "; " & Range("y3").Value & "; " & Range("y4").Value & "; " & Range("y5").Value & "; " & Range("ay6").Value
    
    MyMail.Subject = Range("x9").Value
    MyMail.Body = Range("x10").Value & vbNewLine & vbNewLine & Range("x11").Value & vbNewLine & _
            Range("x12").Value & vbNewLine & Range("x13").Value & vbNewLine & Range("x14").Value & vbNewLine & _
            Range("x15").Value
    
    'Use For Each loop to loop through each file in the folder
    For Each FileToAttach In FSOFolder.Files
        MyMail.Attachments.Add FileToAttach.Path
    Next FileToAttach
    
    MyMail.Send
    
    'MoveAFile

End Sub
 
Upvote 0
Solution
Using a FileSystemObject should be able to do this:

VBA Code:
Sub maj_Rektangelafrundedehjørner1_Klik()
   
    'Skal bruges
    Dim MyOutlook As Object
    Set MyOutlook = CreateObject("Outlook.Application")
   
    ' File system variables
    Dim folderName As String
    Dim FSOLibrary As Object
    Dim FSOFolder As Object
    Dim FileToAttach As Object
   
    'Set the file name to a variable
    folderName = Range("X18").Value2
   
    'Set all the references to the FSO Library
    Set FSOLibrary = CreateObject("Scripting.FileSystemObject")
    Set FSOFolder = FSOLibrary.GetFolder(folderName)
   
    Dim MyMail As Object
    Set MyMail = MyOutlook.CreateItem(olMailItem)
   
    MyMail.To = Range("x2").Value ' & "; " & Range("y3").Value & "; " & Range("y4").Value & "; " & Range("y5").Value & "; " & Range("ay6").Value
   
    MyMail.Subject = Range("x9").Value
    MyMail.Body = Range("x10").Value & vbNewLine & vbNewLine & Range("x11").Value & vbNewLine & _
            Range("x12").Value & vbNewLine & Range("x13").Value & vbNewLine & Range("x14").Value & vbNewLine & _
            Range("x15").Value
   
    'Use For Each loop to loop through each file in the folder
    For Each FileToAttach In FSOFolder.Files
        MyMail.Attachments.Add FileToAttach.Path
    Next FileToAttach
   
    MyMail.Send
   
    'MoveAFile

End Sub
It is just as it should be, thank you very much, Best Regards from Denmark KW
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,205
Members
452,618
Latest member
Tam84

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