0
votes

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
1
You can't open an embedded word document like that. Documents.Open needs a file path.Rory
Does it always need a file path??? My idea is to include it kn an Excel file and open it from there instead of using a folder pathAlber
Which line does the error occur on? I would expect an error at doc.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 Rylatt
Yes, it always needs a file path. Only other thing I can think of would be using the OLEObject's Verb method (as mentioned that is not a method of a document) to activate the document, then try and get a reference to it with GetObjectRory

1 Answers

1
votes

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