VBA to create emails with an attachment based on path (folder)

Jonnny

New Member
Joined
Dec 19, 2022
Messages
14
Office Version
  1. 365
Platform
  1. Windows
ABCDEF
Row 1To:ccSubjectBody TextPath of Attachment folderAttachment name
Row 2abc@email1.comabc@email1.comtest email 1For your information...C:\Users\ABC\Desktop\SavedFolder\Folder112192022Invoice12.pdf

Hi,
I want to display emails with an attachment from a certain folder. It will be used by multiple users so the path is different and the users must enter their path in row 2, column E.
In the folder, there will be multiple documents with all different name. If users enter the file name in row 2, column F, VBA create an email with the attachment.

Below is the code I currently use.

Code:
Private Sub CommandButton1_Click()

Dim strbody As String
    Dim xStrFile As String
    Dim xFilePath As String
    Dim xFileDlg As FileDialog
    Dim xFileDlgItem As Variant
    Dim xOutApp As Outlook.Application
    Dim xMailOut As Outlook.MailItem
    Application.ScreenUpdating = False

    Set xOutApp = CreateObject("Outlook.Application")
    Set xMailOut = xOutApp.CreateItem(olMailItem)
    Set xFileDlg = Application.FileDialog(msoFileDialogFilePicker)

 If xFileDlg.Show = -1 Then
        With xMailOut
        .To = ""
        .CC = Rows(2).Columns(4).Value
        .BCC = Rows(2).Columns(3).Value
        .Subject = Rows(2).Columns(5).Value
        .Body = Rows(2).Columns(6).Value
        For Each xFileDlgItem In xFileDlg.SelectedItems
                .Attachments.Add xFileDlgItem
            Next xFileDlgItem
            .Display
    End With
    End If
    Set xMailOut = Nothing
    Set xOutApp = Nothing
    Application.ScreenUpdating = True
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Assuming you have a way to call the code (either in a button or manually running), the below should work for you...

VBA Code:
Private Sub CommandButton1_Click()

Dim strbody As String
Dim xStrFile As String
Dim xFilePath As String
Dim xFileDlg As FileDialog
Dim xFileDlgItem As Variant
Dim xOutApp As Outlook.Application
Dim xMailOut As Outlook.MailItem
Application.ScreenUpdating = False

Set xOutApp = CreateObject("Outlook.Application")
Set xMailOut = xOutApp.CreateItem(olMailItem)

'add path and file to variables
Dim aPath As String: aPath = Cells(2, 7).Value & "/"
Dim aFile As String: aFile = Cells(2, 8).Value

With xMailOut
    .To = ""
    .CC = Cells(2, 4).Value
    .BCC = Cells(2, 3).Value
    .Subject = Cells(2, 5).Value
    .Body = Cells(2, 6).Value
    'attaches file using path and file variables
    .attachments.Add aPath & aFile
    .Display
End With
Set xMailOut = Nothing
Set xOutApp = Nothing
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you so much. It works perfectly.!

Is it also possible to attach multiple attachments?


Below is my current macro.

VBA Code:
Private Sub CommandButton1_Click()

Dim strbody As String
Dim xStrFile As String
Dim xFilePath As String
Dim xFileDlg As FileDialog
Dim xFileDlgItem As Variant
Set xOutApp = CreateObject("Outlook.Application")
Set xMailOut = xOutApp.CreateItem(olMailItem)
Application.ScreenUpdating = False


'add path and file to variables
Dim aPath As String: aPath = Cells(2, 6).Value & "/"
Dim aFile As String: aFile = Cells(2, 7).Value

With xMailOut
    .To = Cells(2, 1).Value
    .CC = Cells(2, 2).Value
    .BCC = Cells(2, 3).Value
    .Subject = Cells(2, 4).Value
    .Body = Cells(2, 5).Value
    'attaches file using path and file variables
    .attachments.Add aPath & aFile
    .Display
End With
Set xMailOut = Nothing
Set xOutApp = Nothing
Application.ScreenUpdating = True
End Sub
 
Upvote 0
It's possible to loop through the specified folder and attach multiple files from it. Would you want all files in the folder attached to the email? Or are there specific ones?
 
Upvote 0
I would like to attach multiple selected files from the specified folder.
E.g) I have several check boxes and only checked files to be attached from the specified folder. Would it be possible?
Thank you so much in advance your advice.
 
Upvote 0
Will there be a set number of checkboxes for each row? Or will all rows use the same checkbox collection?

If you can provide a visual of how it will look, that would be helpful.
 
Upvote 0
I would like to have multiple attachments in the email when it is selected.
e.g) Row 6, 7 boxes are checked, then the attachment for Row6 and Row7 needs to be attached in the email.
I can do it for one single attachment but struggling to for multiple attachments..
It doesn't have to be with checkboxes, if there are other ways to select.
1673960786666.png
 

Attachments

  • 1673960579380.png
    1673960579380.png
    21.7 KB · Views: 10
  • 1673960601943.png
    1673960601943.png
    21.7 KB · Views: 9
  • 1673961312630.png
    1673961312630.png
    24.9 KB · Views: 11
Upvote 0
You could set up checkboxes, sure. Not sure if that would be efficient as I'm not sure how much your data would change. The other thing you could do would be just to include a "Attach?" column with "YES" or "NO" values. You could also set up all the form checkboxes you need and set the cell link to the adjacent cell under the "Attach?" column, where the values would be "TRUE" or "FALSE".

The below is how you'd loop through the Attach? column and skip or add the needed attachment.

NOTE: in your screenshots, I didn't see where the To, CC, or BCC values went and I'm not sure how you'd list those in the new format you have.

VBA Code:
Private Sub CommandButton1_Click()

Dim xOutApp As Object: Set xOutApp = CreateObject("Outlook.Application")
Dim xMailOut As Object: Set xMailOut = xOutApp.CreateItem(olMailItem)

'set range for loop to look through - column F is the Attach? column
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
Dim rng As Range: Set rng = ws.Range("F5:F" & ws.Cells(ws.Rows.Count, 6).End(xlUp).Row)
Dim c As Range

Application.ScreenUpdating = False

'add path and file to variables
Dim aPath As String: aPath = ws.Cells(3, 3).Value & "\"

With xMailOut
    .To = ""
    .CC = ""
    .BCC = ""
    .Subject = ws.Cells(1, 3).Value
    .Body = ws.Cells(2, 3).Value
    'loops through responses in column F to determine if corresponding attachment should be added
    For Each c In rng.Cells
        If ws.Cells(c.Row, 6).Value = "YES" Then 'or use "TRUE" if using form checkbox
            .attachments.Add aPath & ws.Cells(c.Row, 5).Value & ".pdf"
        End If
    Next c
    .Display
End With

Set xMailOut = Nothing
Set xOutApp = Nothing

Application.ScreenUpdating = True

End Sub
 
Upvote 0
I meant to share this image with you yesterday, which gives you a visual on column F and the "Attach?" column.

1674079449989.png
 
Upvote 0
Thanks a lot.! It works perfectly.
Do you know if it is possible to have excel table in body?
e.g) B4:E8 table in the screenshot to be in the mail body. I am not sure if this even works with VBA.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

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