[英]How to recognize an embedded Word document as “open”?
I need to export the contents of Excel cells to specific bookmarks in a Word document embedded in the same Excel file. 我需要将Excel单元格的内容导出到嵌入在同一Excel文件中的Word文档中的特定书签中。
Sub Provareport()
Dim ws As Worksheet
Dim wd As Object
Dim oEmbFile As Object
Set ws = ThisWorkbook.Worksheets("Modello")
Set wd = CreateObject("Word.application")
'opening the embedded document
Application.DisplayAlerts = False
Set oEmbFile = ThisWorkbook.Sheets("BANCHE").OLEObjects("Reword")
oEmbFile.Verb Verb:=xlPrimary
Set oEmbFile = Nothing
Application.DisplayAlerts = True
With wd.ActiveDocument
.Bookmarks("Denominazione").Range.Text = ws.Range("G13").Value
.Bookmarks("SNDG").Range.Text = ws.Range("F13").Value
.Bookmarks("Organo_deliberante").Range.Text = ws.Range("I13").Value
.Bookmarks("Headline").Range.Text = ws.Range("B80").Value
.Bookmarks("Attivo").Range.Text = ws.Range("B81").Value
.Bookmarks("Passivo").Range.Text = ws.Range("B90").Value
.Bookmarks("LCRNSFR").Range.Text = ws.Range("B93").Value
.Bookmarks("Patrimonializzazione").Range.Text = ws.Range("B94").Value
.Bookmarks("Patrimonio2").Range.Text = ws.Range("B95").Value
.Bookmarks("Conto_economico").Range.Text = ws.Range("B98").Value
.Bookmarks("Conto_economico2").Range.Text = ws.Range("B100").Value
.Bookmarks("Conto_economico3").Range.Text = ws.Range("B105").Value
.Bookmarks("Conto_economico4").Range.Text = ws.Range("B108").Value
End With
Set doc = Nothing
Set wd = Nothing
End Sub
The code stops at the line: 代码在以下行停止:
With wd.ActiveDocument
The following error appears: 出现以下错误:
"run time error '4248'
“运行时错误'4248'
This command is not available because no document is open"该命令不可用,因为没有打开任何文档”
However, the Word document is open. 但是,Word文档是打开的。
as correctly pointed out by Gareth you are working with different instances of word. 正如Gareth正确指出的那样,您正在使用不同的单词实例。 It is assumed reference to Microsoft Word Object library has been already added.
假定已经添加了对Microsoft Word对象库的引用。
Then try in declare section 然后尝试在声明部分
Dim wd As Word.Document
and finally 最后
Set oEmbFile = ThisWorkbook.Sheets("BANCHE").OLEObjects("Reword")
oEmbFile.Verb Verb:=xlPrimary
Set wd = oEmbFile.Object
With wd
'
'
'
'
End With
above code work for editing in the location and seems sufficient for present objective. 上面的代码可以在该位置进行编辑,对于当前目标而言似乎足够了。 However for opening with full word window and ribbon etc may try
但是对于全字窗口和功能区等打开可能会尝试
oEmbFile.Verb Verb:=xlVerbOpen
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.