Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    49
    Thanks
    4
    Thanked 0 Times in 0 Posts
    How can you get to all the running Word instances?

    I use to the following code for other reasons, so could it be applied here? If not, what are some other options?

    sub ListRunningProcesses()
    Dim winmgmtsObject As Object
    Set winmgmtsObject = GetObject("winmgmts:")

    Dim myQuery As String
    myQuery = "SELECT * FROM win32_process WHERE Name = 'WinWord.EXE'"

    Dim allProcesses As Object
    Set allProcesses = winmgmtsObject.execquery(myQuery)

    Dim myWordInstance As Word.Application
    Dim excelApplication As Excel.Application
    Dim myProcess As Object
    For Each myProcess In allProcesses
    Debug.Print myProcess.Name
    'set myWordInstance = ???
    'Do want I need to with a valid myWordInstance reference
    Next
    'object clean up goes here
    End Sub

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by ForEachLoop View Post
    How can you get to all the running Word instances?
    What do you want to do with them? If you want to automate an instance of Word as a COM object, you can use the GetObject() method to create a reference to it ("Word.Application" class). However, I don't know how GetObject() chooses which instance of Word to automate, if there is more than one. Note that multiple documents usually open in a single instance of Word.

    Regarding your code --

    Quote Originally Posted by ForEachLoop View Post
    Code:
    sub ListRunningProcesses()
        Dim winmgmtsObject As Object
        Set winmgmtsObject = GetObject("winmgmts:")
        
        Dim myQuery As String
        myQuery = "SELECT * FROM win32_process WHERE Name = 'WinWord.EXE'"
        
        Dim allProcesses As Object
        Set allProcesses = winmgmtsObject.execquery(myQuery)
    -- I'm not familiar with the library you're using, so I don't know whether you can get a useful object that way.

  3. #3
    Lounger
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    49
    Thanks
    4
    Thanked 0 Times in 0 Posts
    Even though Word does usually open in a single instance, the fact is you can still have multiple instances, so going through the documents list isn't always going to catch all the documents.

    Has anyone done this before, getting a specific instance from among all the running ones?

Posting Permissions

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