Excel VBA Macro: Send Email with All Files (in a Specific Folder) Attached

  Рет қаралды 6,277

greggowaffles

greggowaffles

2 жыл бұрын

#ExcelVBA #ExcelMacroExcel VBA Macro: Attach All Files (in a Specific Folder) to an Email and Send. In this video, we write code in VBA to add all files in a folder to an email using a Do While Loop.
Code (KZfaq doesn't allow brackets; so LT and GT are used for less than and greater than, respectively):
Sub email_all_files_in_folder()
Dim OutApp As Object
Dim OutMail As Object
Dim strbody As String
Dim myFldr As String
Dim myFile As String
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
myFldr = "C:\Users\greggowaffles\Documents\KZfaq Videos\Test\Sample Data Files\"
myFile = Dir(myFldr)
strbody = "LT BODY style = font-size:11pt; font-family:Arial GT" & _
"Hi Team, LT p GT Please see file(s) attached. LT p GT" & _
"Thanks, LT br GT Greg"
On Error Resume Next
With OutMail
.To = "123@abc.com"
.CC = ""
.BCC = ""
.Subject = "Daily File(s) " & Format(Date, "mm/dd/yyyy")
.Display
.HTMLBody = strbody & .HTMLBody
Do While myFile LT GT ""
.Attachments.Add myFldr & myFile
myFile = Dir
Loop
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
#ExcelVBA #ExcelMacro

Пікірлер: 25
@drewgossage8842
@drewgossage8842 2 жыл бұрын
Another great little piece of code! Thanks for sharing man👍👍
@greggowaffles
@greggowaffles 2 жыл бұрын
No prob! Thanks for watching!!
@sachinkariappa4922
@sachinkariappa4922 Жыл бұрын
Thank you so much, it’s a great video 👍🏻 Is there way to attach files based on a specified modified date instead of attaching all the files in the folder?
@SongDictionary
@SongDictionary Жыл бұрын
could you please let me know how to make this file path dynamic.
@israeljosiah2427
@israeljosiah2427 2 жыл бұрын
Awesomely done greg
@greggowaffles
@greggowaffles 2 жыл бұрын
Appreciate that!! Thanks for watching
@greggowaffles
@greggowaffles 2 жыл бұрын
So myFile = Dir(myFldr) returns nothing for you?
@israeljosiah2427
@israeljosiah2427 2 жыл бұрын
@@greggowaffles yes but that's because I'm getting the folder path from a textbox value ... so I needed add \ at the end of the path before dir could pick it .. Thanks for your response
@greggowaffles
@greggowaffles 2 жыл бұрын
Nice. No prob
@nareshchakowry7687
@nareshchakowry7687 Жыл бұрын
Hi Gregg can you please do a video showing how to display all worksheets' names( beside first two sheets; sheet1 and sheet2) on sheet1 😎
@kamalthelegendkiller
@kamalthelegendkiller Жыл бұрын
Thanks
@greggowaffles
@greggowaffles Жыл бұрын
No prob! Thanks for watching!!
@carloscostafonte
@carloscostafonte Жыл бұрын
Excelent Video . Can you do it for two folders at same time ?
@isommallory9473
@isommallory9473 11 ай бұрын
Great stuff. Is there a way to have the email auto send instead of waiting on user input?
@ngoisaolon1982
@ngoisaolon1982 Жыл бұрын
Thank you very much, Is it can show files names in body of email?
@kevshen15
@kevshen15 Жыл бұрын
Great video, very helpful....is it possible to add a cell value to the string body? for example I try to add Range("C3").Value. I can add it as part of the email subject , but cant seem to integrate info from cells into the email body/strbody. Sorry I'm a noob, but I've looked everywhere and tried all sorts with no success.
@tokatemika
@tokatemika Жыл бұрын
How can you use the subpath, which contains the files as the subject line instead?
@BrianaTowery
@BrianaTowery Жыл бұрын
How could I modify it to send all files but send them individually (one file per email)
@austinshofner1614
@austinshofner1614 2 жыл бұрын
At 6:40 you change the folder path to "Some Files"... Is there not away to just copy the above code and keep the macro running for ALL folders. That way you can send out all the attachments across all folders without having to stop and change the path? Im picturing an End result that allows me to send multiple emails going out to different recipients containing all attachments for their specific folder. Is this possible? I feel like i would just copy the above code 15x. With each section the code is listed i would give a a new "myfldr" path along with a different recipients address on the "to." section? Im super new to coding and this will save me SO much time at my job. Your videos are some of the best ive seen.
@greggowaffles
@greggowaffles 2 жыл бұрын
Thanks! You could list all of your folder paths in an excel workbook, and use a loop in the code to go through each one
@tanyaaggarwal4659
@tanyaaggarwal4659 Жыл бұрын
Hello, I would like to know how to make folder dynamic since i have list of folder names in excel
@greggowaffles
@greggowaffles Жыл бұрын
Hi! Can you provide a specific example?
@tanyaaggarwal4659
@tanyaaggarwal4659 Жыл бұрын
@@greggowaffles in your code myfldr is fixed but in my code i have an excel where column d has different folders. So i have to make a loop so that every-time it picks a different folder and attach the files present in that particular folder
@greggowaffles
@greggowaffles Жыл бұрын
@@tanyaaggarwal4659 Ok. I’ll make a video on that!
Каха заблудился в горах
00:57
К-Media
Рет қаралды 9 МЛН
小宇宙竟然尿裤子!#小丑#家庭#搞笑
00:26
家庭搞笑日记
Рет қаралды 8 МЛН
I'm Excited To see If Kelly Can Meet This Challenge!
00:16
Mini Katana
Рет қаралды 19 МЛН
Send bulk emails using Outlook (with Signatures) - Excel VBA macro tutorial
5:23
Extreme Automation - Kamal Girdher
Рет қаралды 38 М.
Excel VBA Macro: Send Multiple Emails (with Multiple Attachments)
12:09
Filter data, save and send results as an email with VBA in Excel
6:29
Excel VBA Macro: Send Email (with Attachment)
8:55
greggowaffles
Рет қаралды 58 М.
Excel VBA: Loop Through All Files in a Folder
5:05
Computergaga
Рет қаралды 62 М.
Excel macro to extract/read outlook emails | Lecture 6
6:18
Extreme Automation - Kamal Girdher
Рет қаралды 22 М.
Automated Multiple Emails with Multiple Attachments and "From" option
11:20
PK: An Excel Expert
Рет қаралды 70 М.
Excel VBA Macro: Send Email With Table In Body (Dynamic Range)
13:51
greggowaffles
Рет қаралды 106 М.