Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
417 views
in Technique[技术] by (71.8m points)

VBA open embedded Word document directly from Excel

I would like to embed a Word document into my Excel file and have the VBA to open it, add a table I have from the Excel file, and copy-paste all in an email. Is there a way to open an embedded Word document with VBA instead of giving a folder path? Here is my VBA (it works fine). I guess the way is to add as an object (?). Thanks!

Private Sub ToggleButton1_Click()

Dim ol As Outlook.Application
Dim olm As Outlook.MailItem
Dim wd As Word.Application
Dim doc As Word.Document
Dim rng As Range
Set ol = New Outlook.Application
Set olm = ol.CreateItem(olMailItem)
Set wd = New Word.Application
wd.Visible = True
Set doc = wd.Documents.Open(Environ$("USERPROFILE") & "DesktopUS - Dec.docx")
lr = Sheet4.Range("A" & Application.Rows.Count).End(xlUp).Row
ThisWorkbook.Worksheets("Webinars").Range("A27:D" & lr).Copy
doc.Paragraphs(17).Range.PasteExcelTable _
                        LinkedToExcel:=False, _
                        WordFormatting:=False, _
                        RTF:=False
doc.Content.Copy
With olm
.Display
.To = ""
.Subject = ""
Set Editor = .GetInspector.WordEditor
Editor.Content.Paste
Application.CutCopyMode = False
'.Send
 End With
 Set olm = Nothing
 Application.DisplayAlerts = False
doc.Close SaveChanges:=False
 Set doc = Nothing
wd.Quit
 Set wd = Nothing
 Application.DisplayAlerts = True

End Sub
question from:https://stackoverflow.com/questions/65915100/vba-open-embedded-word-document-directly-from-excel

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I provided you the answer with the working code in your previous posting of this same question.

VBA Excel open Word Object


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...