Use excel to copy word file to outlook as body of email

tomhow

New Member
Joined
Sep 24, 2017
Messages
7
I am trying to createa macro in excel which will send an email to a large number of people bycopying the contents of a word file as the body of the email. The list of emailaddresses is listed in column A of sheet1. I modified the code below from a post I found earlier. My problem istwofold; firstly when I run it, the macro hangs and I get a message that thedocument is locked for editing by XXX and do I want to open it as read only. This is despite the fact that the document isnot open nor is Word running. Secondly, Iget a run time 91 Error “Object variable or with block variable not set” Theproblem line is:
SetoMailWordDoc = oOutApp.ActiveInspector.WordEditor
Could someone pleasegive me the benefit of their expertise? I am running excel, word & outlook2010.

Code:
Sub CopyBodyFromWord()

Dim oOutApp As Object
Dim oMailItem As Object
Dim oWordApp As Object
Dim oWordDoc As Object
Dim oMailWordDoc As Object
Dim WDocName As String
For j = 1 To 3 'use cells 1 to 3 in column "A" as a test run
    If Sheets("SHEET1").Range("A1").Value <> "" Then
        myadd = myadd & ";" & Sheets("SHEET1").Range("A" & j).Value
    End If
Next
On Error GoTo CleanUp
WDocName = InputBox("What is the name of the word file to be copied to the email?")
Set oWordApp = CreateObject("Word.Application")
Set oWordDoc = oWordApp.Documents.Open("C:\Users\Janet & Peter\Documents\" & WDocName)
oWordDoc.Content.Copy
Set oOutApp = CreateObject("Outlook.Application")
Set oMailItem = oOutApp.CreateItem(0)
With oMailItem
    .To = ""
    .Bcc = myadd
    .Subject = "Test Email"
End With
Set oMailWordDoc = oOutApp.ActiveInspector.WordEditor
oMailWordDoc.Application.Selection.Paste
CleanUp:
oWordApp.Quit
Set oMailWordDoc = Nothing
Set oMailItem = Nothing
Set oOutApp = Nothing
Set oWordDoc = Nothing
Set oWordApp = Nothing
End Sub
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
When you enter the document name in the inputbox are you including the file extension?

WDocName = InputBox("What is the name of the word file to be copied to the email?")

As this would effects this line

Set oWordDoc = oWordApp.Documents.Open("C:\Users\Janet & Peter\Documents" & WDocName)

If you recorded a macro in the Word Doucment to show how to select all the document content it would provide you with

Sub Macro2()
'
' Macro2 Macro
'
'
Selection.WholeStory
Selection.Copy
End Sub
 
Upvote 0
Yes I include the file extension. Also my macro copies the word file to the clipboard ok. The problem line is:

Set oMailWordDoc = oOutApp.ActiveInspector.WordEditor
When I hover over it says that oMailWordDoc = Nothing
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

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