Page 1 of 1
Send mail
Posted: Mon Sep 28, 2020 7:51 am
by sna
Code: Select all
Dim olApp As Outlook.Application
Dim olMail As MailItem
Dim flname As String
Dim wkb As Workbook
flname = VBA.Environ("temp") & "\" & VBA.Format(VBA.Now, "dd_mm_yyy_hh_mm_ss") & ".xlsx"
Set olApp = New Outlook.Application
Set olMail = olApp.CreateItem(olMailItem)
Set wkb = Workbooks.Add ' add new workbook
' change sheet name and range here
ThisWorkbook.Sheets(1).Range("A1:g25").Copy Destination:=wkb.Sheets(1).Range("a1")
wkb.SaveAs flname ' save workbook with temp name
wkb.Close 'close it
With olMail
.To = "abc@gmail.com"
.Subject = "Hello"
' vbNewLine is used to insert a row
.Body = "Dear " & vbNewLine & "Please find the attachment" & vbNewLine & vbNewLine & vbNewLine & "Regards" & vbNewLine & "email"
.Attachments.Add flname ' attach the workbook
.Display ' or use .send
End With
Set wkb = Nothing
Set olMail = Nothing
Set olApp = Nothing
End Sub
I need to tweak to loop mail Id and sent to them
Best Regards,
Re: Send mail
Posted: Tue Sep 29, 2020 9:24 pm
by snasui

Please attach the macro file that have your code inside.
Macro file extension is '.xlsm' or '.xlsb' not '.xlsx' in post #1.
Re: Send mail
Posted: Tue Sep 29, 2020 9:59 pm
by sna
See attached
Re: Send mail
Posted: Wed Sep 30, 2020 8:25 am
by snasui
sna wrote: Mon Sep 28, 2020 7:51 am
I need to tweak to loop mail Id and sent to them

The example code for looping as below:
Code: Select all
Dim r As Range
'Other code
With thisworksheet
For Each r In .Range("o3", .Range("o" & .Rows.Count).End(xlUp))
With olMail
'.To = "abc@gmail.com"
.To = r.Value
.Subject = "Hello"
' vbNewLine is used to insert a row
.Body = "Dear " & vbNewLine & "Please find the attachment" & vbNewLine & vbNewLine & vbNewLine & "Regards" & vbNewLine & "email"
.Attachments.Add flname ' attach the workbook
.Display ' or use .send
End With
Next r
'Other code
End With
Re: Send mail
Posted: Wed Sep 30, 2020 10:00 pm
by sna
How can we send only PDF?
Re: Send mail
Posted: Wed Sep 30, 2020 10:53 pm
by snasui

You can send PDF file with this code, just make this statement
flname = VBA.Environ("temp") & "\" & VBA.Format(VBA.Now, "dd_mm_yyy_hh_mm_ss") & ".xlsx" refer to PDF file instead.
Re: Send mail
Posted: Thu Oct 01, 2020 12:14 pm
by sna
Thank you