snasui.com ยินดีต้อนรับ ยินดีต้อนรับสู่กระดานถามตอบ Excel and VBA และอื่น ๆ ที่เป็นมิตรกับทุกท่าน มีไฟล์แนบมหาศาล ช่วยให้ท่านค้นหาและติดตามศึกษาได้โดยง่าย สมาชิกท่านใดที่ยังไม่ได้ระบุ Version ของ Excel ที่ใช้งานจริง สามารถทำตาม Link นี้เพื่อจะได้รับคำตอบที่ตรงกับ Version ของท่านครับ ระบุ Version ของ Excel
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,
You do not have the required permissions to view the files attached to this post.
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
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.