Reputation: 21
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
Upvotes: 0
Views: 1591
Reputation: 1713
Try this:
Sub Demo()
Dim wApp As Word.Application
Dim doc As Word.document
ActiveSheet.Shapes.Range(Array("Object 1")).Select
Selection.Verb Verb:=xlPrimary
Set wApp = GetObject(, "Word.Application")
Set doc = ActiveDocument
MsgBox doc.Paragraphs(1).Range.Text
End Sub
Upvotes: 2