Search code examples
ms-wordwindows-10windows-scripting

Getting the filenames of all open documents in Microsoft Word


I am trying to write a script that saves all the open the programs, documents, files and webpages as a workspace. Ideally, I would like to use the script to save the workspace and then for it to reopen once I click another script (ie. the saved workspace). I have several jobs that I am doing concurrently and I would like to be able to shuffle between these jobs with just a click. I am aware of multiple desktops and such but I wanted something different that saves me from constantly having to open all the files and pages each time I intend to work on a particular task. Also, I did not want to be installing any additional software and I just wanted it to operate from a script, if possible.

For the coding, I went with VBScript. I am able to open the files, but I can't figure out how to check which files are open and their filepaths. This is part of the code I have so far:

Set Word = GetObject("", "Word.Application")
Word. --- I'm trying to get documents to work here and to get 

Thank you in advance for any help!


Solution

  • This will show the current open documents:

    ' Note: Leaving the first parameter, the pathname, empty as it's not
    '       required, and passing an empty string will cause an isolated 
    '       instance of Word to be loaded
    Set Word = GetObject(, "Word.Application")
    For Each doc In Word.Documents
        WScript.Echo "Name: " + doc.Name + ", Full name: """ + doc.FullName + """"
    Next
    

    This outputs the following for me, obviously your list will differ depending on what's open:

    Name: Document1, Full name: "Document1"
    Name: Example Doc.docx, Full name: "C:\Files\Example Doc.docx"