filmov
tv
Excel VBA Macro: Send Email with All Files (in a Specific Folder) Attached
![preview_player](https://i.ytimg.com/vi/CKrRCH4Tymk/maxresdefault.jpg)
Показать описание
#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 (YouTube 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\Youtube 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
.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
Code (YouTube 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\Youtube 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
.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
Комментарии