将特定的文档添加到特定的Word.Application

问题描述:

我可以将objDoc分配为ObjWord的文档(在Set objDoc = oDoc.Object之后)吗?

May I assign the objDoc as a document of the ObjWord (after Set objDoc = oDoc.Object)?

我的代码如下:

'Declaration
Dim oDoc As OLEObject
Dim objWord As Word.Application
Dim objDoc As Word.Document

Dim WS as Worksheet
Dim strOdoc as String

'Initialization
Set WS = Whorksheets("Sheet1")
strOdoc = "WordDoc"

Set objWord = New Word.Application

'Set objWord = GetObject(, "Word.Application")
'I need using GetObject to be able to made the activated OLEDocument, invisible.
'And Need Set objWord = New Word.Application to be able
'to EndTask the WINWORD.EXE with objWord.Quit.

objWord.Visible = False
Set oDoc = WS.OLEObjects(strOdoc)
oDoc.Activate
Set objDoc = oDoc.Object
'I need here Add the objDoc to the objWord


  • 我需要objDocobjWord对象的文档,该文档已被objWord.Visible = False隐藏(我不能使用Dim objDoc As objWord.Document变量声明).
  • 我需要将objWord隔离开,因为在使用时 objWord.Quit,它一定不要尝试关闭其他Word文档. (在这里我使用了Set objWord = New Word.Application)
  • 我需要使用GetObject语句才能使激活的OLEDocument 不可见.
  • 并且需要设置objWord = New Word.Application才能结束任务 带有objWord.Quit的WINWORD.EXE.

    • I need objDoc to have been a document of the objWord object, which has been hidden with objWord.Visible = False (I can't use Dim objDoc As objWord.Document variable declaration).
    • I need the objWord to have been isolated because when using objWord.Quit, it must not try to close other Word Documents. (Here I used Set objWord = New Word.Application)
    • I need using the GetObject statement to be able to made the activated OLEDocument invisible.
    • And Need Set objWord = New Word.Application to be able to EndTask the WINWORD.EXE with objWord.Quit.
    • 但是如何整合上述两个优点: 1)使用OLEObjectDocument不可见,以及2)如果没有打开其他Word文档,则执行EndTask WINWORD.EXE ?

请采取更合乎逻辑的逐步方法.请记住,您需要引用Microsoft Word对象库(VBE>工具>引用).

Please take a more logical, step-by-step approach. Bear in mind that you need the Microsoft Word Object Library to be referenced (VBE > Tools > References).

现在,您可以创建Word应用程序的实例.默认情况下它是不可见的.您可以使用它并退出它,而不会影响可能同时运行的Word的任何其他实例.您的代码的那部分在逻辑上是正确的,但是推荐的代码是Set objWord = CreateObject("Word.Application").这将创建一个新实例.我不确定Set objWord = New Word.Application也许是否做同样的事情,但是您不需要两者都使用.

Now you can create an instance of the Word application. It is invisible by default. You can use it and quit it without affecting any other instances of the Word that might be running concurrently. That part of your code is logically correct but the recommended code is Set objWord = CreateObject("Word.Application"). This creates a new instance. I'm not sure if Set objWord = New Word.Application perhaps does the same thing but you shouldn't need both.

现在您可以打开一个文档.使用Documents.Open方法执行此操作.该文档将是Word.Document数据类型,而不是OLEObject.该文档将在一个窗口中打开,您可以使该窗口不可见.将其命名为objWord.Windows(1)objWord.ActiveWindow.也许屏幕上会出现闪烁,您可以通过objWord.ScreenUpdating = False进行消除.

Now you can open a document. Use the Documents.Open method to do that. The document will be of Word.Document data type, not an OLEObject. This document will open in a window and it is that window which you can make invisible. Address it as objWord.Windows(1) or objWord.ActiveWindow. Perhaps there will be a flicker on the screen which you might combat with objWord.ScreenUpdating = False.

完成这些步骤之后,您将在没有打开其他文档的Word实例中打开一个普通的Word文档,该文档在其自己的窗口中不可见.您可以操作该文档,将其关闭,然后退出objWord.

After these steps you would have a normal Word document, invisible in its own Window, open in an instance of Word which has no other document open in it. You can manipulate that document, close it and then quit objWord.