How to select an executable instance of Word and open a document in

So, here's what I want to do:

I have a VBA application that uses information from several open Word 2010 documents. This works great as long as all open documents are opened in the same process. But I would like to open these Docs not manually, but through Powershell, and so far I could do this by opening a new instance of Word. Let's take a look at the code:

$isRunning = (Get-Process | Where-Object { $_.Name -eq "WINWORD" }).Count -gt 0

if ($isRunning) {

    #Select open Word Process
    **$wrd = # ???** 

    # Make Word Visible 
    $wrd.visible = $true  

    # Open a document  
    $doc = $wrd.documents.add($latest.Fullname)  
}
else {

    # Create Word Object  
    $wrd = New-object -com word.application 

    # Make Word Visible 
    $wrd.visible = $true  

    # Open a document  
    $doc = $wrd.documents.add($latest.Fullname)  
}

      

??? mark the place where I would like to select an executable instance of Word where I can open my documents. But all the examples I could find always call a new object by starting a separate instance of the word.

Another workaround would be to change my VBA application to access documents from different processes, but I don't know if this is possible or how to do it.

Any help would be much appreciated anyway.

Thank.

+3


source to share


1 answer


You can get an executable instance like:



$word = [System.Runtime.InteropServices.Marshal]::GetActiveObject('Word.Application')

      

+1


source







All Articles