Object Variable or With block variable not set

Doedtman

Board Regular
Joined
May 21, 2010
Messages
92
Hello!
I'm having some problems with a macro that I'm trying to set up. I'm trying to send out emails to a list of addresses in an excel spreadsheet, but I keep getting this error: "Object variable or With block variable not set" I'm not sure what this means, or how I can go about fixing this. Any help would be greatly appreciated!

Sub SendEmailWithAttachment()
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim strEmail As String, strName As String
Dim lRowCount As Long

Set objOutlook = CreateObject("outlook.application") ' Start Outlook

lRowCount = 2 'get email address from column B

Do Until ActiveSheet.Cells(lRowCount, 2) = "" ' check for email
strEmail = ActiveSheet.Cells(lRowCount, 2).Value ' get email Address
strName = ActiveSheet.Cells(lRowCount, 1).Value ' get name of file
Set objOutlookMsg = objOutlook.CreateItem(olMailItem) ' create new email msg
With objOutlookMsg ' Fill email
.Subject = ActiveSheet.Cells(lRowCount, 6).Value 'Get subject from column F
.Body = ActiveSheet.Cells(lRowCount, 7).Value ' Get body from column G
.To = strEmail
.Attachments.Add ("C:\Documents and Settings\" & strName & ".pdf")
.Send
End With
lRowCount = lRowCount + 1
Loop

objOutlook.Quit
Set objOutlook = Nothing
Set objOutlookMsg = Nothing
End Sub


Thanks,
Jodi
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Jodi

Where in the code do you get the error?
 
Upvote 0
Strange.:eek:

Anyway, try replacing ActiveSheet with an reference to the worksheet the data is on.
Code:
Dim wsData As Worksheet
Set wsData = Worksheets("EmailData")
 
Do Until wsData.Cells(lRowCount, 2) = "" ' check for email
      strEmail = wsData.Cells(lRowCount, 2).Value ' get email Address
 
....
 
Upvote 0
Did you change the worksheet name from the one I used 'EmailData' to the name of the worksheet your data is on?

By the way, where are you actually running the code?
 
Upvote 0
I'm running the code in excel. I did change the worksheet name. Here's what the new code is:

Code:
Sub SendEmailWithAttachment()
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim strEmail As String, strName As String
Dim lRowCount As Long

Set objOutlook = CreateObject("outlook.application") ' Start Outlook

lRowCount = 2 'get email address from column B
 
Dim wsData As Worksheet
Set wsData = Worksheets("Email")
 
Do Until wsData.Cells(lRowCount, 2) = "" ' check for email
    
strEmail = wsData.Cells(lRowCount, 2).Value ' get email Address
strName = ActiveSheet.Cells(lRowCount, 1).Value ' get name of file
Set objOutlookMsg = objOutlook.CreateItem(olMailItem) ' create new email msg
With objOutlookMsg ' Fill email
.Subject = ActiveSheet.Cells(lRowCount, 6).Value 'Get subject from column F
.Body = ActiveSheet.Cells(lRowCount, 7).Value ' Get body from column G
.To = strEmail
.Attachments.Add ("C:\Documents and Settings\" & strName & ".pdf")
.Send
End With
lRowCount = lRowCount + 1 
Loop

objOutlook.Quit
Set objOutlook = Nothing
Set objOutlookMsg = Nothing
End Sub
 
Upvote 0
It's working now! I was going line by line in VB and it kept showing an error. But when I click on the command button in excel, it works perfectly. Not sure why. Thank you very much for your help!

Jodi
 
Upvote 0
Jodi

You should replace ActiveSheet with wsData throughout the code, I only posted an example of how to change a couple of lines.
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,259
Members
452,901
Latest member
LisaGo

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