Attaching multiple PDFs from selected folder to email - excel

I am trying to allow users to select the folder where they have kept multiple PDFs and extract them.
Unless I put the specific pathname, I am unable to get the macro to run correctly.
This works, if instead of referencing the function I reference the specific pathname:
StrPath = "C:\Users\Mark\OneDrive - Corporation\Desktop\Work file\RemA\Canada\"
My full code, which does not attach any PDFs:
Option Explicit
Private Function selectfolder()
'Defining the Variables
Dim user_name As String 'sequence of characters: alphabets, numbers, special characters
user_name = Environ("username") 'to pick up the username from work environment
'Prompt for folder creation
With Application.FileDialog(msoFileDialogFolderPicker) 'standard wording
.Title = "Select the folder to save"
.InitialFileName = "C:\Users\" & user_name & "\OneDrive - Corporation\Desktop\Work file" 'base directory to open
.AllowMultiSelect = False
If .Show <> -1 Then Exit Function 'if user does not press OK, end the function'
selectfolder = .SelectedItems(1)
End With
End Function
Sub sendremindermail()
'Defining the Variables
Dim outlookapp As Object
Dim outlookmailitem As Object
Dim myattachments As Object
Dim StrPath As String
Dim StrFile As String
Dim network, Fldr As String
'If user does not choose a folder
StrPath = selectfolder()
If StrPath = "" Then
Exit Sub
End If
Set outlookapp = CreateObject("outlook.application")
Set outlookmailitem = outlookapp.createitem(0)
Set myattachments = outlookmailitem.Attachments
'Creating the email and adding attachment
With outlookmailitem
'Title of the Email
.Subject = "Test Run"
'To be sent to recipients
.To = "ABCD#gmail.com"
'Body of the email
.Body = "Dear " & ActiveSheet.Range("C6").Value & ","
.Body = .Body & "Please find attached your Finance Report for the month of " & Format(Range("C8"), "mmmm yyyy") & "."
'Attach your files
StrFile = Dir(StrPath & "*.pdf")
Do While Len(StrFile) > 0
myattachments.Add StrPath & StrFile
StrFile = Dir
Loop
'Displaying only the email. Not sending
.Display
End With
End Sub

The function selectfolder returns the path without the '' at the end, so please add '' at the end, so selectfolder has to return like "c:\temp\pdffolder', currently it is returning 'c:\temp\pdffolder', so Dir line is not returning the files.

Related

VBA FilePicker no longer opening from ThisWork.Path

I have a workbook (located on our network) with a macro enabled so I can email exported PDF worksheets via Outlook every Friday. The exported worksheet PDF gets saved to the same location as the Workbook. Outlook then opens the FilePicker and lets me select the file that I want to attach to the email. For some reason the Filepicker is now not opening to ThisWork.path anymore; it opens to my default MyDocuments located on my native computer. The exported file still saves in the proper spot (located on the network) but the FilePicker just wont open to that location. Now, if I move this Workbook to my direct computer, the FilePicker works as it should. I have not changed anything in the VBA so I don't know why this is all the sudden not working. It worked just last week. Below is the code I believe to be relevant to the issue.
'Creates workpath string
Dim mypath As String, fname As String
mypath = ThisWorkbook.Path
fname = CreateObject("Scripting.FileSystemObject").GetBaseName(ThisWorkbook.Name)
'********************************************************
'Saves PDF with Template Text and Date based on Order Date
Sheets(1).ExportAsFixedFormat 0, mypath & "\" & "TEXT Order Sheet " & Format(Range("D7").Value, "mm-dd-yy")
ActiveSheet.Name = Format(Range("D7").Value, "mm-dd-yy")
'********************************************************
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)
xFileDlg.Filters.Clear
xFileDlg.Filters.Add "pdf files", "*.pdf"
xFileDlg.AllowMultiSelect = True
xFileDlg.InitialFileName = ThisWorkbook.Path
If xFileDlg.Show = -1 Then
'********************************************************
With xMailOut
.Display
.To = "fake#email.com"
.Subject = "TEXT" & Range("D7").Value
.HTMLBody = "<p style='font-family:calibri;font-size:12.0pt'>" & "Here is our TEXT order for the week of " & Range("D7").Value & "." & " Please respond to this email to confirm that you have received the order." & .HTMLBody
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
'********************************************************

Excel VBA attach multiple PDFs to an email - my do loop skips first PDF for second and subsequent emails generated?

I have attempted to write a macro that scans through a folder to pick relevant PDFs belonging to a person (such as AAA) and attach them to an email to be sent to AAA, then move on to pick up PDFs belonging to BBB and attach them to an email to be sent to BBB so on and so forth. My folder containing the PDFs looks like this:
AAA_111111.pdf
AAA_222222.pdf
AAA_333333.pdf
BBB_111111.pdf
BBB_222222.pdf
BBB_333333.pdf
CCC_777777.pdf
CCC_888888.pdf
CCC_999999.pdf
CCC_444444.pdf
The person is identified by the letters before the underscore (initials) and there is a list on another Excel tab that the initials are looked up against to return their email address.
I have written the code below and it works fairly well except it has an irritating flaw that I cannot solve. It will successfully generate the email for person AAA and attach all three files listed above for them. On the next pass of the main (outer) "do while" loop it comes to person BBB but the inner "do while mfe=" loop attaches the second and third file listed for them (BBB_222222.pdf & BBB_333333.pdf) and completely ignores BBB_111111.pdf (doesn't attach it) though it seems to be able to see it on. Ditto for the third loop, the "do while mfe=" loop will attach the latter three files for CCC to an email but won't attach CCC_777777.pdf?!
Sub emailreports()
Dim OutApp As Object
Dim OutMail As Object
Dim OMail As Object, signature, mfe, sto As String
Dim emaillastrow, x, a As Long
Dim fso As Scripting.FileSystemObject
Set fso = New FileSystemObject
Dim folder, strfile As String
Dim rundate As Date
Application.ScreenUpdating = False
Application.Calculation = xlManual
Application.AutoRecover.Enabled = False
folder = Worksheets("START").Range("A14")
strfile = Dir(folder)
rundate = Worksheets("TEMPLATE").Range("E7")
b = Worksheets("START").Range("H25")
Sheets("EMAIL").Select
emaillastrow = Worksheets("EMAIL").Range("A1000000").End(xlUp).Row
If Dir(folder, vbDirectory) = "" Then
MsgBox "PDF destination file path doesn't exist.", vbcritial, "Path error"
Exit Sub
End If
Do While Len(strfile) > 0
Filename = fso.GetBaseName(folder & strfile)
mfe = Left(Filename, InStr(Filename, "_") - 1)
For x = 2 To emaillastrow
If mfe = Worksheets("EMAIL").Range("A" & x) Then
sto = sto & ";" & Worksheets("EMAIL").Range("B" & x)
End If
Next
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.Display
End With
With OutMail
.To = LCase(sto)
.CC = ""
.BCC = ""
.Subject = "Test subject text"
Do While mfe = Left(Filename, InStr(Filename, "_") - 1)
.Attachments.Add (folder & Filename)
Filename = Dir
If Filename = "" Then
Exit Do
End If
Loop
.signature.Delete
.HTMLBody = "<font face=""arial"" style=""font-size:10pt;"">" & "Test email body text" & .HTMLBody
.Display
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set OutMail = Nothing
Set OutApp = Nothing
Set OutAccount = Nothing
Skip:
sto = ""
strfile = Filename
Loop
Application.StatusBar = False
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
Application.AutoRecover.Enabled = True
End Sub
I thought about trying to make it somehow at the end of generating the email to take a step back but being a Do loop this is not possible. My code seems to ignore the PDF that it stopped at as part of the previous email generation and when generating the next email starts from that PDF file but only picks up and attaches subsequent PDFs. Any help would be gratefully received as I've tried all sort of things but can't make it work. This is my first post to Stackoverflow so apologies if my question and/or format is not correct or appropriate.
You could use a dictionary object to group together the filenames by prefix with one pass of the directory and then iterate the dictionary keys to create the emails with corresponding attachments. For example (outlook methods untested)
Option Explicit
Sub emailreports()
Dim dict As Scripting.Dictionary, key
Set dict = New Scripting.Dictionary
Dim folder As String, strfile As String, mfe As String
Dim sTo As String, arPDF, arAddr, f
Dim ws As Worksheet, r As Long, emaillastrow As Long
folder = Worksheets("START").Range("A14")
strfile = Dir(folder & "*.pdf")
If strfile = "" Then
MsgBox "PDF destination file path doesn't exist.", vbCritical, "Path error " & folder
Exit Sub
Else
' group files by prefix
Do While strfile <> ""
mfe = Left(strfile, InStr(strfile, "_") - 1)
If dict.Exists(mfe) Then
dict(mfe) = dict(mfe) & vbTab & strfile
Else
dict.Add mfe, strfile
End If
strfile = Dir ' get next pdf
Loop
End If
Set ws = Worksheets("EMAIL")
emaillastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
' read email address lookup into array
arAddr = ws.Range("A2:B" & emaillastrow)
' prepare one email per key
Dim OutApp As Object, OutMail As Object, OMail As Object
'Set OutApp = CreateObject("Outlook.Application")
For Each key In dict.Keys
' build array of file names for one key
mfe = Trim(key)
arPDF = Split(dict(mfe), vbTab)
' get email addresses
sTo = ""
For r = 1 To UBound(arAddr)
If mfe = arAddr(r, 1) Then
sTo = sTo & arAddr(r, 2) & ";"
End If
Next
Debug.Print key, sTo
'Set OutMail = OutApp.CreateItem(0)
'With OutMail
'.To = LCase(sTo)
'.cc = ""
'.BCC = ""
'.Subject = "Test subject text"
' attach pdfs
For Each f In arPDF
'.Attachments.Add folder & f
Debug.Print , folder & f
Next
'.signature.Delete
'.HTMLBody = "<font face=""arial"" style=""font-size:10pt;"">" & "Test email body text" & .HTMLBody
'.Display
'End With
Next
'OutApp.quit
End Sub
On Error Resume Next seems to mask errors and hide skip reason. Try to use more specialized filename mask:
...
folder = Worksheets("START").Range("A14")
If Dir(folder, vbDirectory) = "" Then
MsgBox "PDF destination file path doesn't exist.", vbcritial, "Path error"
Exit Sub
End If
strfile = Dir(fso.BuildPath(folder, "*_*.pdf")
rundate = Worksheets("TEMPLATE").Range("E7")
b = Worksheets("START").Range("H25")
'Sheets("EMAIL").Select 'no need to select a sheet
emaillastrow = Worksheets("EMAIL").Range("A1000000").End(xlUp).Row
...

Pick file names stored in an Array and attach those files over email VBA

I am trying to pick files with names stored in a cell with comma separated and attached those files over email. Below is the code which I am working on and I keep on getting RunTime error 438.
I am struggling to figure out what is going wrong, your expertise would help me to resolve the issue
Private Sub test3()
Dim mess_body As String, StrFile As String, StrPath As String
Dim appOutLook As Object
Dim MailOutLook As Object
Dim i As Long
Dim Elem As Variant
Dim myArr As Variant
Set appOutLook = CreateObject("Outlook.Application")
Set MailOutLook = appOutLook.CreateItem(olMailItem)
StrPath = Sheets("Input").Range("E3").Value
With MailOutLook
.To = "manoj.sahoo#gmail.com"
.Subject = "test"
.HTMLBody = "test"
With Worksheets("DL")
For i = 2 To .Cells(.Rows.Count, "B").End(xlUp).Row
myArr = Split(.Range("B" & i).Value, ",")
For Each Elem In myArr
StrFile = Dir(StrPath & "\" & Elem & ".xlsx")
'
Do While Len(StrFile) > 0
.attachments.Add StrPath & "\" & StrFile
StrFile = Dir
Loop
Next Elem
Next i
End With
'
.Display
End With
End Sub
You have two nested With operators in the code:
With MailOutLook
.To = "manoj.sahoo#gmail.com"
.Subject = "test"
.HTMLBody = "test"
With Worksheets("DL")
The Worksheet doesn't have the Attachments property. You need to explicitly use the source object in the nested With operator.
MailOutLook.attachments.Add StrPath & "\" & StrFile
Finally, make sure that you pass a valid file path to the Add method of the Attachments class. The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.

Browse folders to attach files in Outlook mail using VBA Excel [duplicate]

This question already has answers here:
How to add an attachment to an email using VBA in Excel
(2 answers)
Closed 1 year ago.
I am working on an userform in VBA Excel that allows a user to submit their request. The users complete the form then click on the Send button. An Outlook mail will be opened and the completed form is automatically attached.
The users usually have data or/and documents related to the request. I want to add functionality to my userform, which allows them to browse their PC and import the files. When they click on the Send button, these files will be attached to the same mail as the original Excel form.
Below are the codes for my Send button.
Function CreationMail(criticité As String)
Dim xFile As String
Dim xFormat As Long
Dim Wb As Workbook
Dim Wb2 As Workbook
Dim FilePath As String
Dim FileName As String
Dim OutlookApp As Object
Dim OutlookMail As Object
Dim rng As Range
Set Sheet1 = ThisWorkbook.Sheets("Formulaire")
Set rng = Sheets("Formulaire").Range("C6:D11").SpecialCells(xlCellTypeVisible)
Application.ScreenUpdating = False
Set Wb = Application.ActiveWorkbook
ActiveSheet.Copy
Set Wb2 = Application.ActiveWorkbook
Select Case Wb.FileFormat
Case xlOpenXMLWorkbook:
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
Case xlOpenXMLWorkbookMacroEnabled:
If Wb2.HasVBProject Then
xFile = ".xlsm"
xFormat = xlOpenXMLWorkbookMacroEnabled
Else
xFile = ".xlsx"
xFormat = xlOpenXMLWorkbook
End If
Case Excel8:
xFile = ".xls"
xFormat = Excel8
Case xlExcel12:
xFile = ".xlsb"
xFormat = xlExcel12
End Select
FilePath = Environ$("temp") & "\"
FileName = "STATSAE" & "_" & Format(Now, "yymmdd") & "_" & Format(Now, "hhnnss")
Set OutlookApp = CreateObject("Outlook.Application")
Set OutlookMail = OutlookApp.CreateItem(0)
Wb2.SaveAs FilePath & FileName & xFile, FileFormat:=xFormat
With OutlookMail
.To = ";" & ";"
.CC = ""
If criticité = "Haute" Then
.Importance = olImportanceHigh
End If
If criticité = "" Then
.Importance = olImportanceNormal
End If
If criticité = "Faible" Then
.Importance = olImportanceNormal
End If
.Subject = "Request" & Space(1) & FileName
.Attachments.Add Wb2.FullName
.Body = "Please find the requested information" & vbCrLf & "Best Regards"
.HTMLBody = RangetoHTML(rng)
.Display
End With
Wb2.Close
Kill FilePath & FileName & xFile
Set OutlookMail = Nothing
Set OutlookApp = Nothing
Application.ScreenUpdating = True
End Function
First of all, you need to do customizations to your form to pick up files that requires to be attached to the email. Then in the code you can repeat the Add method of the Attachments class for each entry the user has chosen. The source of the attachment can be a file (represented by the full file system path with a file name) or an Outlook item that constitutes the attachment.
.Attachments.Add Wb2.FullName
.Attachments.Add your_chosen_file

Using FileDialog to attach a file to an email

I want to eliminate human error when it comes to selecting a file to send in an attachment in an email. Basically eliminating this code Filename = Application.InputBox("Enter File Name:", "", "File Name")'Type in File Name And replace it using FileDialog which has come to my attention as a good way of accomplishing this. I am very confused on how to use it correctly. Every time I have tried, I am able to use the application and see the file but I don't understand how it gets attached. My email coding is below.
Sub Mail_workbook_Test()
Dim OutApp As Object
Dim OutMail As Object
Dim Date1 As Date
Dim Recipient As Variant
Date1 = Format(Now, "yyyy-mm-dd")
'Date and format
UserName = Application.InputBox("Enter your name:", "", "FirstLast")
Filename = Application.InputBox("Enter File Name:", "", "File Name")
'Type in File Name
List = Application.InputBox("Enter Email List Name:", "", "ListName")
'Type in Email List
If List = "gold" Then
List = "example#mail.com; example1#mail.com; example2#mail.com"
ElseIf List = "silver" Then
List = "example#mail.com; example#mail.com"
Else
MsgBox ("Sorry, your list selection was not recognised.")
Exit Sub
End If
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
For Each Recipient In Split(List, ";")
.Recipients.Add Trim(Recipient)
Next
.CC = ""
.BCC = ""
.Subject = "" + Filename + "" & " " & Date1
.Body = "Hi Everyone," & Chr(10) & Chr(10) & "Please let me know if you get this!" & Chr(10) & Chr(10) & "Thanks!"""
.Attachments.Add ("C:\Users\" + UserName + "\Desktop\" + Filename + ".xlsx")
.Send '.Display
End With
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
How do I get Filename = to equal the file I pick with the code below and properly attach into an email? Any advice on my coding would be great too, thank you!
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
.AllowMultiSelect = False
End with
Replace
Filename = Application.InputBox("Enter File Name:", "", "File Name")
with:
With Application.FileDialog(msoFileDialogOpen)
.AllowMultiSelect = False
.Show
Filename = .SelectedItems(1)
End With
Here is an excerpt from a similar subroutine of mine, hope you'll find it helpful. Place this somewhere between creating MailItem object and sending the message:
'Ask which files to open (using FileDialog)
Dim fdInputFile As FileDialog
Set fdInputFile = Application.FileDialog(msoFileDialogOpen)
With fdInputFile
.Filters.Clear
.AllowMultiSelect = True
If .Show = False Then Exit Function 'you might want to handle "Cancel" button differently
End With
'Attach all files
Dim sInputFile As Variant
For Each sInputFile In fdInputFile.SelectedItems
OutMail.Attachments.Add sInputFile, 1
Next sInputFile
PS: I think it's easier to reuse the code above when it's separated from user input, so I use a separate function for creating e-mails whereever I need to. Just feed all the input as the parameters and call .Send method when you're ready
Public Function CreateEmailMsg(cRecipients, _
Optional sSubject As String = "", _
Optional sBody As String = "", _
Optional cAttachments = Nothing) _
As Object
'
' Generate new e-mail message
'
' Parameters:
' cRecipients: String (or a Collection of Strings) containing
' e-mail addresses of recipients
' sSubject: String containing message subject line
' sBody: String containing message body (HTML or plain text)
' cAttachments: String (or a Collection of Strings) containing
' path(s) to attachments
'
' Returns MailItem object referring to the created message
' Most common methods for MailItem object are .Display and .Send
'
Dim appOL As Object
Set appOL = CreateObject("Outlook.Application")
Dim msgNew As Object
Set msgNew = appOL.CreateItem(0) 'olMailItem
Dim sItem
With msgNew
'Message body
.BodyFormat = 2 'olFormatHTML
.HTMLBody = sBody
'Recipients
If TypeName(cRecipients) = "String" Then
.Recipients.Add cRecipients
ElseIf Not cRecipients Is Nothing Then
For Each sItem In cRecipients
.Recipients.Add sItem
Next sItem
End If
'Subject
.Subject = sSubject
'Attachments
If TypeName(cAttachments) = "String" Then
.Attachments.Add cAttachments, 1
ElseIf Not cAttachments Is Nothing Then
For Each sItem In cAttachments
.Attachments.Add sItem, 1
Next sItem
End If
End With
Set CreateEmailMsg = msgNew
End Function

Resources