none
Macro to print all open documents fails in Word 2010 RRS feed

  • Question

  • My company built a macro for our clients to print out all open documents that were opened by our system.  This macro simply loops through all the documents in the documents collection to accomplish this.    It has been used for many years without any problems until Word 2010 which apparently opens a separate instance of Word for each (or every few?) document(s).  

    Can anyone tell me how to loop through all the open documents of all the Word Applications in Word 2010 (using a VBA macro). Another solution would be to be able to get Word 2010 to behave the same as before.

     


    Monday, December 19, 2011 8:44 PM

Answers

  • Hi Scott

    Indeed, for some unknown reason, Microsoft changed the behavior when opening Word documents from within Windows Explorer to always start a new instance of the application, rather than re-using a running one, as used to be the case.

    Probably, you could change this behavior using a group policy, if your app is running within a company (rather than something that's sold to "anyone"). If you're interested in that approach, I recommend the TechNet forums where you'll find specialists for network and configuration issues.

    A .NET language could handle this by looping all the Processes (think of the list you see in the Windows "Task Manager"). But VBA on its own has no way to do this. It would probably require using the Windows API.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Wednesday, January 4, 2012 7:50 AM
    Tuesday, December 20, 2011 2:50 PM
    Moderator

All replies

  • Hello,

    In what way does the macro fail? Does it produce exceptions?

    I suppose it fails if you open a document in a ProtectedView window,e.g. from an Outlook attachment. Is this the case?


    Regards from Belarus (GMT + 3),

    Andrei Smolin
    Add-in Express Team Leader
    Tuesday, December 20, 2011 7:14 AM
  • Hi Andrei

    Thanks.  No exceptions, it just fails to make it through all the open docs - only the ones in application.documents of that documents application.

    The problem is that unlike the previous versions of Word, 2010 often times opens separate applications for documents so the loop we execute from VBA only can print the Documents it sees.   These are not in protected view or anything, just open documents.  

    Here is what the loop looks like: 

    For i = 1 to documents.count

    PrintOut(Documents(i))

    next i

    To witness this behavior, make a folder with 5 empty documents, select all five, right mouse and choose "OPEN".  Then alt-f11 and in the immediate window type ?documents.count.  

    In 2007 and earlier this would result in 5, but in 2010 for some reason it will be 1 for each document.   So what I need is a way to reach the other document collections from one of those vba project spaces.

    Thank you for your time,

    Scott

     

    Tuesday, December 20, 2011 2:11 PM
  • Hi Scott

    Indeed, for some unknown reason, Microsoft changed the behavior when opening Word documents from within Windows Explorer to always start a new instance of the application, rather than re-using a running one, as used to be the case.

    Probably, you could change this behavior using a group policy, if your app is running within a company (rather than something that's sold to "anyone"). If you're interested in that approach, I recommend the TechNet forums where you'll find specialists for network and configuration issues.

    A .NET language could handle this by looping all the Processes (think of the list you see in the Windows "Task Manager"). But VBA on its own has no way to do this. It would probably require using the Windows API.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bruce Song Wednesday, January 4, 2012 7:50 AM
    Tuesday, December 20, 2011 2:50 PM
    Moderator
  • Hi Bruce Song,

    Doesn't this change the Word Application object model such that the documents collection is now meaningless from within a given document?    Can you point me to some MS documentation of this 'changed behavior'?   Because now when I hit F1 for help on "Documents" collection in Word, the answer is: "Returns a Documents collection that represents all the open documents . . .".   Nothing in there saying"except for some or most of the open documents". 

    Or is this post itself now the 'Official document' on the change now that you have marked Cindy's post as "Answer"?

    By the way, this change appears to be beyond Word documents opened within Windows Explorer, as the documents I am interested in printing are always opened by our application (and are in the same "Documents" collection in 2003 and 2007).

    Thank you,

    Scott

    Thursday, January 5, 2012 4:59 PM