简体   繁体   English

从 VBA Excel 打开和关闭 Word 文档

[英]Open and Close Word Document from VBA Excel

I developed a small program in VBA Excel.我在 VBA Excel 中开发了一个小程序。 It was a good start however I adjusted some presets -- a website recommended this as would be pulling text from MS Word.这是一个好的开始,但是我调整了一些预设——一个网站推荐了这个,就像从 MS Word 中提取文本一样。 I stripped out all of the code I was working on to the following:我删除了我正在处理的所有代码,如下所示:

Function FnOpeneWordDoc()

   Dim objWord

   Dim objDoc

   Set objWord = CreateObject("Word.Application")

   Set objDoc = objWord.Documents.Open("C:\Users\Filepath\Example1.docx")

   objWord.Visible = True

End Function

It opens a Word document referenced in the task manager, but not from the interface.它打开任务管理器中引用的 Word 文档,但不是从界面中引用的。

The program above hangs for a while and returns a the following:上面的程序挂了一段时间并返回以下内容:

Microsoft Office Excel is waiting for another application to complete an OLE action. Microsoft Office Excel 正在等待另一个应用程序完成 OLE 操作。

Finally the run stops and I end up with Run-Time Error '-2147023170 (800706be)': Automation The Remote call failed.最后运行停止,我最终得到运行时错误“-2147023170 (800706be)”:自动化远程调用失败。

*I've tried all the basic tricks such as shutting down all instances of Excel and Word. *我已经尝试了所有基本技巧,例如关闭所有 Excel 和 Word 实例。

您应该在Set objWord = CreateObject("Word.Application")之后立即添加objword.Visible = True

I tested this Procedure and it opens up the "Example1.docx" Document within 2.3 seconds.我测试了这个程序,它在 2.3 秒内打开了“Example1.docx”文档。

Public Sub FnOpeneWordDoc()    
Dim WordApp As Word.Application, WordDoc As Word.Document

Set WordApp = CreateObject("Word.Application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open("C:\Users\Filepath\Example1.docx")

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM