问题
So I have a VBA and I would like to open and modify a Word file that is embedded in the Excel instead of opening it from a folder path. I am using this VBA and it is not working properly (basically I need the VBA to open the Word document, add the table from the Excel from ranges A24:C & lastRow, copy-paste everything to an Outlook email and finally close the Word document). I receive error 13, if that helps. Please assist!!!! Thanks a lot
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 the part
Set doc = wd.Documents.Open(ActiveWorkbook.Sheets("Webinars").OLEObjects(1))
doc.Verb Verb:=xlPrimary
回答1:
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
来源:https://stackoverflow.com/questions/65899857/vba-excel-open-word-object