简体   繁体   English

打开多个Word文档VBA Excel

[英]Opening multiple word documents vba excel

What's the best way to open multiple work documents? 打开多个工作文档的最佳方法是什么? I want to be copying/pasting from different word documents through VBA. 我想通过VBA从不同的Word文档复制/粘贴。 Should I open a new instance for each one? 我应该为每个实例打开一个新实例吗? I really need two workbooks active at a time, and will close after the pasting has been done. 我确实确实需要同时激活两个工作簿,并且将在粘贴完成后关闭。

Is

Set objWord1 = CreateObject("Word.Application")
Set objWord2 = CreateObject("Word.Application")
objWord1.Document.open("maindocument.docx")
obj2Word2.Document.open("seconddoc.docx")

is this the most efficient way? 这是最有效的方法吗?

You just need one instance of the application open. 您只需要打开一个应用程序实例。 Then you can make two separate declarations for your documents: 然后,您可以为文档做两个单独的声明:

Dim objWord As Object
Dim doc1 As Object, doc2 As Object

Set objWord = CreateObject("Word.Application")
Set doc1 = objWord.Documents.Open("maindocument.docx")
Set doc2 = objWord.Documents.Open("seconddoc.docx")

I don't generally recommend this, but if you want to get crafty: 我一般不建议这样做,但是如果您想提高技巧:

Dim doc1 As Object, doc2 As Object

With CreateObject("Word.Application")
    Set doc1 = .Documents.Open("maindocument.docx")
    Set doc2 = .Documents.Open("seconddoc.docx")
End With

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

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