object required error for range of cells in copy in outlook mail

sapxl

New Member
Joined
Jun 20, 2020
Messages
24
Office Version
  1. 2016
  2. 2013
  3. 2011
  4. 2010
  5. 2007
Platform
  1. Windows
Hello Friends,

I'm trying to send the outlook email, where i want to send the bodytext of email would be the value of cells range which needs to be copy into my email body. I tried to do so, but unable to figure out.

Is there anyone who can help me out with this issue... appreciate your help in this regard. Thanks,

code:

VBA Code:
Sub Outlookmail()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)
xMailBody = Activate.Sheet1.Range("A1:H24").Value

''xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"

On Error Resume Next
With xOutMail
.To = "abcxyz@gmail.com"
.CC = ""
.BCC = ""
.Subject = Cells(cell.Row, "B8").Value & Space(1) & "(" & Cells(cell.Row, "D8").Value & Space(1) & Cells(cell.Row, "F8").Value & ")" & Space(1) & "**DO NOT REPLY TO THIS E-MAIL**"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 

Attachments

  • outlook object reqd error.png
    outlook object reqd error.png
    51.3 KB · Views: 3

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
try something like this:
VBA Code:
mailar = Activate.Sheet1.Range("A1:H24").Value
xMailBody = ""
 For i = 1 To 24
  For j = 1 To 8
  xMailBody = xMailBody & mailar(i, j)
  Next j
  xMailBody = xMailBody & vbNewLine
 Next i
 
Upvote 0
Hi offthelip,

Thanks for the code.. but it still not working... I included as below:

VBA Code:
Sub Outlookmail()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Dim mailar As String
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)

mailar = Sheet1.Range("A1:H24").Value
''xMailBody = Activate.Sheet1.Range("A1:H24").Value
With xOutApp.CreateItem(0)
xMailBody = ""
 For i = 1 To 24
  For j = 1 To 8
  xMailBody = xMailBody & mailar(i, j)
  Next j
  xMailBody = xMailBody & vbNewLine
 Next i
 End With
''xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"

On Error Resume Next
With xOutMail
.To = "abcxyz@gmail.com"
.CC = ""
.BCC = ""
.Subject = Cells(cell.Row, "B8").Value & Space(1) & "(" & Cells(cell.Row, "D8").Value & Space(1) & Cells(cell.Row, "F8").Value & ")" & Space(1) & "**DO NOT REPLY TO THIS E-MAIL**"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 

Attachments

  • compile error -outlook email.PNG
    compile error -outlook email.PNG
    87.9 KB · Views: 2
Upvote 0
do you have option explicit set,? if so you need to add:
VBA Code:
dim mailar () as variant
 
Upvote 0
Excellent my dear friend.. superb... Thanks a ton... working very smooth...
 
Upvote 0
It was not working when i use option explicit, but when i removed, it is working smoothly.. Thanks once again..
 
Upvote 0
Hi offthelip,

I'm trying to automate the email sending process without clicking to send email.. if N5 cell has email id then it will automatically send email (N5 value) as soon as email id is entered in the cell N5. I'm trying the code, but no luck... could you pls help me on this..Appreciate your help in this regard.. Thanks,

VBA code:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Range("N5").Value Like "?*@?*.?*" And Cells.SpecialCells(xlCellTypeConstants) Then
    Sheets("Invoice").Range("N5").Value.RefreshAll
    Call Outlookmail
    If Target.Cells.Count > 1 Then Exit Sub
    If Not Application.Intersect(Range("N5"), Target) Is Nothing Then
        Exit Sub
        End If
    End If
End Sub

Sub Outlookmail()
Dim xOutApp As Object
Dim xOutMail As Object
Dim xMailBody As String
Dim mailar As Variant
Set xOutApp = CreateObject("Outlook.Application")
Set xOutMail = xOutApp.CreateItem(0)

''On Error GoTo 0

''If cell.Value Like "?*@?*.?*" And Cells.SpecialCells(xlCellTypeConstants) Then

''If Sheets("Invoice").Range("N5").Value <> "" Then
''If cell.Value Like "?*@?*.?*" And LCase(Cells(cell.Row, "N5").Value) <> "" Then

mailar = Sheet1.Range("A1:H24").Value
With xOutApp.CreateItem(0)
xMailBody = ""
 For i = 1 To 24
  For j = 1 To 8
  xMailBody = xMailBody & mailar(i, j)
  Next j
  xMailBody = xMailBody & vbNewLine
 Next i
 End With
''xMailBody = "Hi there" & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2"

On Error Resume Next
With xOutMail
''.To = "abcxyz@gmail.com"
.To = Sheets("Invoice").Range("N5").Value
.CC = ""
.BCC = ""
.Subject = "IMP. MESSAGE"
.Body = xMailBody
.Display 'or use .Send
End With
On Error GoTo 0
Set xOutMail = Nothing
Set xOutApp = Nothing
End Sub
 
Upvote 0
I think you need to change the the worksheet change routine to this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Cells.Count > 1 Then Exit Sub
    If Not (Intersect(Range("N5"), Target) Is Nothing) Then
    
    If Range("N5").Value Like "?*@?*.?*" Then
 '   Sheets("Invoice").Range("N5").Value.RefreshAll
        Call outlookmail
    End If
    End If
End Sub
I don't think your refreshall, statement is correct either see:
Workbook.RefreshAll method (Excel)
 
Upvote 0
Hi offthelip,

Thanks alot for your help, issue has been resolved with another method.. I have used the helper cell to do the task...

N3 cell in invoice sheet should have formula - IF(N5<>"","yes","no") & N5 ihas the email id cell... if the condition is yes, then the email is triggered automatically.... but it has disadvantage, whenever, i'm double-click on any part of sheet, it gets refreshed, and the email is triggering number of times on that email id which the cell contains... is there anything to stop running the code once successfully send to the email id...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim findText As Variant
findText = Application.Match("yes", Sheets("invoice").Range("N3"), 0)
If Not IsError(findText) Then
If Sheets("Invoice").Range("N3").Value = "yes" Then
Call Outlookmail
End If
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,634
Messages
6,120,659
Members
448,975
Latest member
sweeberry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top