Compile error: Variable not defined

lharr28

New Member
Joined
May 22, 2024
Messages
25
Office Version
  1. 365
Platform
  1. Windows
The VBA was created to send bulk emails with a word template as the outlook body. When I run the macros I get compile error: variable not defined message. It is referring to the portion with the Editor. How would I go about defining the variable? The code is listed below.


1724188111695.png


Rich (BB code):
Sub sendMail()

Dim ol As Outlook.Application
Dim olm As Outlook.MailItem

Dim wd As Word.Application
Dim doc As Word.Document

Set ol = New Outlook.Application

'start from row 11 and go to the last row with data
Dim r As Integer

For r = 11 To Sheet4.Cells(Rows.Count, 1).End(xlUp).Row
    Set olm = ol.CreateItem(olMailItem)
    
    Set wd = New Word.Application
    Set doc = wd.documents.Open(Cells(6, 2).Value)

    With wd.Selection.Find
    .Text = "<<first name>>"
    .Replacement.Text = Sheet4.Cells(r, 2).Value
    .Execute Replace:=wdReplaceAll
    End With
    
    doc.Content.Copy
    
    With wd.Selection.Find
    .Text = "<<vendor>>"
    .Replacement.Text = Sheet4.Cells(r, 3).Value
    .Execute Replace:=wdReplaceAll
    End With
    
    With wd.Selection.Find
    .Text = "<<amount>>"
    .Replacement.Text = Sheet4.Cells(r, 4).Value
    .Execute Replace:=wdReplaceAll
    End With
    
'Set the properties of the mail item, to, cc, subject, etc...
With olm
    .Display
    .To = Sheet4.Cells(r, 5).Value
    .Subject = Sheet4.Cells(r, 6).Value
    
    'Copying the information from the word document into the body of the email
    Set Editor = .GetInspector.WordEditor
    Editor.Content.Paste
    '.Send
End With

Set olm = Nothing

Application.DisplayAlerts = False
doc.Close SaveChanges:=False
Set doc = Nothing
wd.Quit
Set wd = Nothing
Application.DisplayAlerts = True

Next

End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
You'll need to declare the variable Editor as a Word document. So you'll need to add the following declaration...

VBA Code:
Dim Editor As Word.Document

Hope this helps!
 
Upvote 1
You'll need to declare the variable Editor as a Word document. So you'll need to add the following declaration...

VBA Code:
Dim Editor As Word.Document

Hope this helps!
Thanks! It worked! I've been working on this all day and my brain was on overload, lol.
 
Upvote 0

Forum statistics

Threads
1,223,881
Messages
6,175,159
Members
452,615
Latest member
bogeys2birdies

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