VBA Excel打开Word对象 [英] VBA Excel open Word Object

查看:173
本文介绍了VBA Excel打开Word对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个VBA,我想打开和修改Excel中嵌入的Word文件,而不是从文件夹路径中打开它.我正在使用此VBA,但无法正常工作(基本上,我需要VBA打开Word文档,从范围A24:C& lastRow的Excel中添加表格,将所有内容复制并粘贴到Outlook电子邮件中,最后关闭Word文档).如果有帮助,我会收到错误13.请协助!!!!非常感谢

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

推荐答案

尝试一下:

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

这篇关于VBA Excel打开Word对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆