I would like to open and modify a Word document that is embedded in the Excel file.
I need to open the Word document, add the table from the Excel file from range A24:C & lastRow, copy-paste everything to an Outlook email and finally close the Word document.
I receive error 13.
Sub SendMail()
Dim ol As Outlook.Application
Dim olm As Outlook.MailItem
Dim wd As Word.Application
Dim doc As Word.Document
Dim rng As Range
Dim oleObject As Object
Dim wordDocument As Object
Set ol = New Outlook.Application
Set olm = ol.CreateItem(olMailItem)
Set wd = New Word.Application
wd.Visible = True
Set doc = wd.Documents.Open(ActiveWorkbook.Sheets("Webinars").OLEObjects(1))
doc.Verb Verb:=xlPrimary
lr = Sheet4.Range("A" & Application.Rows.Count).End(xlUp).Row
ThisWorkbook.Worksheets("Webinars").Range("A24:C" & lr).Copy
doc.Paragraphs(12).Range.PasteExcelTable _
LinkedToExcel:=False, _
WordFormatting:=False, _
RTF:=False
doc.Content.Copy
With olm
.Display
.To = ""
.Subject = "Test"
Set Editor = .GetInspector.WordEditor
Editor.Content.Paste
CutCopyMode = False
'.Send
End With
Set olm = Nothing
Application.DisplayAlerts = False
CutCopyMode = False
doc.Close SaveChanges:=False
Set doc = Nothing
wd.Quit
Set wd = Nothing
Application.DisplayAlerts = True
End Sub
Everything seems to work except
Set doc = wd.Documents.Open(ActiveWorkbook.Sheets("Webinars").OLEObjects(1))
doc.Verb Verb:=xlPrimary
Documents.Open
needs a file path. – Rorydoc.Verb Verb:=xlPrimary
as a Word document doesn't have a Verb property, and even if it did you can't assign an Excel enum (xlPrimary
) to a Word object. – Timothy RylattOLEObject
'sVerb
method (as mentioned that is not a method of a document) to activate the document, then try and get a reference to it withGetObject
– Rory