Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过Excel修改打开的word文档_Excel_Vba_Ms Word - Fatal编程技术网

通过Excel修改打开的word文档

通过Excel修改打开的word文档,excel,vba,ms-word,Excel,Vba,Ms Word,如何使用VBA通过Excel修改打开的word文档? 这里有一些我正在写的代码,但有些地方我不明白 Dim WordDoc As Word.Document Dim WordApp As Word.Application 'ThisWorkbook is the opened Excel Workbook through which I control Word documents. If Len(Dir(ThisWorkbook.path & "\Report.docx")) <

如何使用VBA通过Excel修改打开的word文档? 这里有一些我正在写的代码,但有些地方我不明白

Dim WordDoc As Word.Document
Dim WordApp As Word.Application
'ThisWorkbook is the opened Excel Workbook through which I control Word documents.

If Len(Dir(ThisWorkbook.path & "\Report.docx")) <> 0 then 
         'if the document exists in the folder where ThisWorkbook is saved, I check
         'if the document is already opened.

   If IsFileOpened(ThisWorkbook.path & "\Report.docx") 
         'if during the run-time I get here, it means that the document exists and 
         'it's already opened.
         'Now I want to get the reference to the opened document "Report.docx",
         'so I do a thing like this.

         Set WordDoc= Word.Application.Documents(ThisWorkbook.path & "\Report.docx")

         'When it tries to excute the instruction over, it gives me a message in which
         'it is written that the name is bad or inexistent, even if the document
         'is already opened. Is the instruction correct?

         Set WordApp= WordDoc.Application
         '...other code

   Else
         'if the document isn't opened, I open it.
      Set WordApp = CreateObject("Word.Application")
      Set WordDoc = WordApp.Documents.Open(ThisWorkbook.path & "\Report.docx") 
         '..other code
   End If

Else     'I create a new document called "Report.docx" if it doesn't exist 
         'in the folder where ThisWorkbook is saved.
   Set WordApp = CreateObject("Word.Application")
   Set WordDoc = WordApp.Documents.Add("Report.docx")
   '.... other code 
End If
Dim WordDoc作为Word.Document
Dim WordApp作为Word.Application
'此工作簿是打开的Excel工作簿,通过它我可以控制Word文档。
如果Len(Dir(ThisWorkbook.path&“\Report.docx”))为0,则
'如果保存此工作簿的文件夹中存在该文档,我将选中
'如果文档已打开。
如果IsFileOpened(thiswoolk.path&“\Report.docx”)
'如果在运行时我到达这里,这意味着该文档存在并且
“已经打开了。
'现在我想获取对已打开文档“Report.docx”的引用,
“所以我做了这样一件事。
设置WordDoc=Word.Application.Documents(thishworkbook.path&“\Report.docx”)
“当它试图执行指令时,它给了我一个信息,其中
“有人写道,该名称不好或不存在,即使该文件
'已打开。说明正确吗?
设置WordApp=WordDoc.Application
“……其他代码
其他的
'如果文档未打开,我将打开它。
设置WordApp=CreateObject(“Word.Application”)
设置WordDoc=WordApp.Documents.Open(thishworkbook.path&“\Report.docx”)
“……其他代码
如果结束
否则,我将创建一个名为“Report.docx”的新文档(如果它不存在)
'在保存此工作簿的文件夹中。
设置WordApp=CreateObject(“Word.Application”)
设置WordDoc=WordApp.Documents.Add(“Report.docx”)
'.... 其他代码
如果结束

提前感谢…

我用excel工作簿尝试了一下,效果很好

 Set WordDoc= Word.Application.Documents(ThisWorkbook.path & "\Report.docx")
应该是

 Set WordDoc= Word.Documents("Report")
当我尝试使用文件路径时,运行时错误“9”下标超出范围。当我只使用文件名时,它是成功的

编辑:使用word文档尝试此操作后,您不需要应用程序对象,也不应使用文件扩展名。我可以确认这是有效的。

我试过这个版本

path = ThisWorkbook.path & "\Report.docx"
Set WordApp = GetObject(path).Application
代替

Set WordDoc= Word.Application.Documents(ThisWorkbook.path & "\Report.docx")

它能工作。

它在哪一行断开?在这个。。。设置WordDoc=Word.Application.Documents(thishworkbook.path&“\Report.docx”),我想您已经将Microsoft Word 14.0对象库添加到引用中了?您是否尝试过从Set WordDoc=Word.Application.Documents(thishworkbook.path&“\Report.docx”)中删除文件路径?我想是的,否则您在尝试运行“Dim WordDoc As Word.Document”时会遇到问题。我不知道它为什么会在那里断裂。如果你想的话,我有另一种方法可以使用……你的子文件IsFileOpened实际上可以正常工作吗?这意味着当文件到达中断行时,它确实是打开的?我尝试了你的代码,但效果不一样。。我得到这个错误:运行时错误4160-错误的文件名。