0

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?

Community
  • 1
  • 1
Tuberose
  • 434
  • 6
  • 24
  • Why not use `objDoc.Close`? Are you working with multiple documents at once? If you draw data from each of them it might be easier to quit and open new document in a loop. – AntiDrondert Jan 29 '18 at 07:01
  • Because the `objWord` object remains open. – Tuberose Jan 29 '18 at 07:03
  • So I Need `objWord = GetObject(, "Word.Application")` for be able to made the activated Ole document invisible. – Tuberose Jan 29 '18 at 07:06
  • 1
    It might be easier if you try to explain what you're trying to achieve instead of asking how to do something using specific method you're using. There might be a more simple solution. – AntiDrondert Jan 29 '18 at 08:04
  • 1) I have an embedded Word Document which contains linked fields. 2) I should copy updated document's fields to a new one and Export pdf a single pdf file. 3) I want this act doing invisible and quietly. 4) Working with Word documents in excel macro, has low performance. 5) I need the WINWORD.EXE been EndTask if there is no other word documents are opened, when the procedure ends. – Tuberose Jan 29 '18 at 08:21
  • @AntiDrondert, This question is in continue of this: [stackoverflow.com/q](https://stackoverflow.com/q/48059723/9075944) – Tuberose Jan 29 '18 at 08:28
  • 1
    Use Documents.Open to open a specific document in Word.Application and Document.Close to close that document after working with it. But it's not possible to activate an OLE object "invisibly". If you need to work with a document that's been embedded it should be LINKED, not just embedded. Then you can work with the source of the link.That's the only possibility you have with Excel as long as the document is open in the workbook. If you can work with the document when the workbook is closed, then you can use the Open XML SDK, but that requires the .NET Framework, not VBA. – Cindy Meister Jan 29 '18 at 14:37
  • @CindyMeister, I can do `objWord.Documents.Add`. How can I add `objWord` Document to the objWord Word.application instance? (against adding new blank Document) – Tuberose Jan 29 '18 at 18:46

1 Answers1

0

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.

Variatus
  • 14,293
  • 2
  • 14
  • 30
  • I had a feeling that he needs multiple instances of Word application, each for every OLEObject in his worksheet (from what I understood anyway). – AntiDrondert Jan 29 '18 at 07:48
  • That is entirely possible by the way I have described. If he wants to edit those documents he will need to open them in Word. I did some research on that a few weeks ago in response to another question. If it is relevant I will look for the answer to that question. – Variatus Jan 29 '18 at 07:52
  • @AntiDrondert, Yes, I have a new instance of the Word.application for OLEObject Document is embedded in my worksheet. While The activated OLEObject been invisible, after assigning to the new instance Word.application document. – Tuberose Jan 29 '18 at 08:03
  • @variatus, I need create new instance (`Set objWord = New Word.Application`) because of been separated from other Word documents, to been able perform `objWord.Quit` (Because the WINWORD.EXE EndTask at the end of the procedure, if not another Word documents are opened.) **And** I need `Set objWord = GetObject(, "Word.Application")` for made invisible the activated OLEOject document. When I using the `Set objDoc = oDoc.Object`, The activated embedded OLEObject remains visible. – Tuberose Jan 29 '18 at 08:55
  • @Variatus, As above comment, your answer not solved my problem. – Tuberose Jan 29 '18 at 08:56
  • What, exactly, are you trying to do? Do you wish to edit the documents? Do you wish to extract information from them? It's impossible to suggest code to you while you neither say what you want nor how you tried to implement my suggestion. – Variatus Jan 29 '18 at 10:28
  • Here is the link to a similar question which I mentioned in my earlier comment. Same OP, opposite requirement. That time it were Excel documents embedded in a Word document. https://stackoverflow.com/questions/48145123/referring-excel-objects-which-embedded-in-a-ms-word-document/48149125#48149125 – Variatus Jan 29 '18 at 10:36
  • 1
    We've been going around on this question for over a week now. See https://stackoverflow.com/questions/48378458/is-winword-exe-running for some background, where I suggested the same approach you have, as well as https://stackoverflow.com/questions/48475673/quit-word-application-which-opened-in-the-child-subrotine – Cindy Meister Jan 29 '18 at 14:33