Consulting

Results 1 to 8 of 8

Thread: Documents.Count returns 0 with documents still open

  1. #1

    Documents.Count returns 0 with documents still open

    Dearest helpers,

    I made a script in my normal.dotm file that should loop through all opened docs, executes some code on each, closes it and when the last one is closed it quits the grey window that's left over.

    In code :
    Do while Word.Application.Documents.Count <> 0
    
    Documents(1).Activate Call mycode() Active Document.close()
    Loop Application.quit It does do this, but it leaves documents open. I Guess I'm running multiple instances. How to actually loop all open documents?
    Cor

  2. #2
    your doc where you are running this code is not exempted.
    you need to Check If the Doc.Name is same as the Current document name
    and prevent it from closing.

    btw you will never get .Count of 0 because of same reason above.

  3. #3
    Well it kinda does all the time and that's the issue. Sorry for posting it in the CODE format.
    Note that the VBA code runs from a normal.dotm template, not the Word document I'm editing.

    I think new Word documents are opened in a new instance that isn't included in:
    Word.Application.Documents.Count
    When I have 3 Word files open the document count is 1. After closing that file the count is 0 and a completely grey window is left. That's closed with
    Application.Quit

  4. #4

    VBA - count all open documents from multiple instances / processes

    Hi,

    I've honestly been looking for an answer for months. Help is appreciated.

    When I select 3 Word files and hit [enter], they are opened in 3 separate processes. How to count them all?

    The following code doesn't work because it always returns 1 as each instance/process contains 1 of the documents.
    Word.Application.Documents.Count
    Actually I want to run a function on every opened document (like SaveAs PDF) and close them afterwards, one by one. Maybe I need to loop through every Word.Application?

    Is that possible?

    Cor

  5. #5
    VBAX Contributor
    Joined
    Jul 2020
    Location
    Sun Prairie
    Posts
    119
    Location
    Yes, you are opening multiple instances of Word this way. If you have Word open (even with no document open) and then do the same thing, all three should open in the same instance.

  6. #6
    Thanks! Good to know it's default behaviour.
    Now, how do I switch to another instance?

    This does not seem to do the trick:
    Dim wd As Object
    Set wd = GetObject(, "Word.Application")
    wd.Documents(1).Close

  7. #7
    it will always create new instance unlike other office apps.
    https://support.microsoft.com/en-us/...3-7f60b30ff49d

  8. #8
    Ok, but how to address it with VBA?

    This is obviously not the answer:
    Word.Application(2).Documents(1)
    ???

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •