Add specific Document to specific Word.Application

浪尽此生 提交于 2019-12-25 01:54:14

问题


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

My code looks like this:

'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

  • 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.
  • But how can integrate two above advantages: 1) Working Invisible with the OLEObjectDocument and 2) Perform EndTask the WINWORD.EXE if no other word documents are opened?


回答1:


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).

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.

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.

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.



来源:https://stackoverflow.com/questions/48495929/add-specific-document-to-specific-word-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!