Hi there! I am very new to VBA so everything I have put in this code has been borrowed from other individuals questions, and I am having trouble making the last few adjustments for it to fit my needs. I am trying to send an email with a range of cells copied from an Excel workbook. I made the code and have verified I can send an email with just a general body message and I can make the correct selection of cells in my code, but when I try and change the body of my email to include the cells I have issues. I think this has to do with the mail body variable designation, but I get a "type mismatch" error when I try and change it to a range from string when I was just sending a practice email. Any help is appreciated! Thank you!
Sub SendEmail(what_address As String, subject_line As String, mail_body As Range)
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
olMail.To = what_address
olMail.Subject = subject_line
olMail.Body = mail_body
olMail.Send
End Sub
Sub CopyData()
Dim a As Long
Dim i As Long
Dim b As Long
Dim r As Long
Dim mail_body As Range
Dim what_address As String
Dim subject_line As String
Application.ScreenUpdating = False
a = Worksheets("Active").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Active").Cells(2, "B").Copy Worksheets("Sheet3").Cells(1, "A")
Worksheets("Active").Cells(2, "E").Copy Worksheets("Sheet3").Cells(1, "B")
Worksheets("Active").Cells(2, "F").Copy Worksheets("Sheet3").Cells(1, "C")
Worksheets("Active").Cells(2, "O").Copy Worksheets("Sheet3").Cells(1, "D")
Worksheets("Active").Cells(2, "Y").Copy Worksheets("Sheet3").Cells(1, "E")
For i = 2 To a
If Worksheets("Active").Cells(i, 3).Value = "Michael" Then
b = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Active").Cells(i, "B").Copy Worksheets("Sheet3").Cells(b + 1, "A")
Worksheets("Active").Cells(i, "E").Copy Worksheets("Sheet3").Cells(b + 1, "B")
Worksheets("Active").Cells(i, "F").Copy Worksheets("Sheet3").Cells(b + 1, "C")
Worksheets("Active").Cells(i, "O").Copy Worksheets("Sheet3").Cells(b + 1, "D")
Worksheets("Active").Cells(i, "Y").Copy Worksheets("Sheet3").Cells(b + 1, "E")
End If
Next i
r = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Activate
ActiveSheet.Range(Cells(1, 1), Cells(r, 5)).Select
Set mail_body = Selection
what_address = "example@gmail.com"
subject_line = "Test"
Call SendEmail(what_address, subject_line, mail_body)
'Sheets("Sheet3").UsedRange.ClearContents
Application.ScreenUpdating = True
End Sub
Sub SendEmail(what_address As String, subject_line As String, mail_body As Range)
Dim olApp As Outlook.Application
Set olApp = CreateObject("Outlook.Application")
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
olMail.To = what_address
olMail.Subject = subject_line
olMail.Body = mail_body
olMail.Send
End Sub
Sub CopyData()
Dim a As Long
Dim i As Long
Dim b As Long
Dim r As Long
Dim mail_body As Range
Dim what_address As String
Dim subject_line As String
Application.ScreenUpdating = False
a = Worksheets("Active").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Active").Cells(2, "B").Copy Worksheets("Sheet3").Cells(1, "A")
Worksheets("Active").Cells(2, "E").Copy Worksheets("Sheet3").Cells(1, "B")
Worksheets("Active").Cells(2, "F").Copy Worksheets("Sheet3").Cells(1, "C")
Worksheets("Active").Cells(2, "O").Copy Worksheets("Sheet3").Cells(1, "D")
Worksheets("Active").Cells(2, "Y").Copy Worksheets("Sheet3").Cells(1, "E")
For i = 2 To a
If Worksheets("Active").Cells(i, 3).Value = "Michael" Then
b = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Active").Cells(i, "B").Copy Worksheets("Sheet3").Cells(b + 1, "A")
Worksheets("Active").Cells(i, "E").Copy Worksheets("Sheet3").Cells(b + 1, "B")
Worksheets("Active").Cells(i, "F").Copy Worksheets("Sheet3").Cells(b + 1, "C")
Worksheets("Active").Cells(i, "O").Copy Worksheets("Sheet3").Cells(b + 1, "D")
Worksheets("Active").Cells(i, "Y").Copy Worksheets("Sheet3").Cells(b + 1, "E")
End If
Next i
r = Worksheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
Worksheets("Sheet3").Activate
ActiveSheet.Range(Cells(1, 1), Cells(r, 5)).Select
Set mail_body = Selection
what_address = "example@gmail.com"
subject_line = "Test"
Call SendEmail(what_address, subject_line, mail_body)
'Sheets("Sheet3").UsedRange.ClearContents
Application.ScreenUpdating = True
End Sub