问题
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 theobjWord
object, which has been hidden withobjWord.Visible = False
(I can't useDim objDoc As objWord.Document
variable declaration). - I need the
objWord
to have been isolated because when usingobjWord.Quit
, it must not try to close other Word Documents. (Here I usedSet 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 withobjWord.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