Managing Office documents with Powershell

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Andrea Gaviraghi Andrea Gaviraghi 8 months, 2 weeks ago.

  • Author
    Posts
  • #36798
    Profile photo of Andrea Gaviraghi
    Andrea Gaviraghi
    Participant

    Hi all,
    I'm very new to PowerShell, so I appreciate any help you guys can give me.
    I would like to get documents that are already open and then forcing them to save (or even better to save them into another directory, let's say Desktop\LastRecovered\).

    I'm searching since 2 days but no success.
    Any help would be greatly appreciated.

    Thanks,

    Andrea

  • #36808
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    You can handle this using the COM objects. Usually you would do this by creating a new COM object:

    $MSword = New-Object -ComObject Word.Application
    

    However, that will start a new instance of the application and you won't see any documents that are already open. What you'll need to do is bind to the existing application instance instead.

    Here's some example code for Word which should point you in the right direction:

    $MSword = [Runtime.Interopservices.Marshal]::GetActiveObject('Word.Application')
    
    $documents = $MSword.Documents
    
    foreach ($document in $documents) {
    
        $document.SaveAs("F:\__temp\a\$($document.name)")
        $document.Close()
    
    }
    

    Credit to this post on Stack Overflow for demonstrating how to bind to an existing instance of an application.

  • #36814
    Profile photo of Andrea Gaviraghi
    Andrea Gaviraghi
    Participant

    Hello,
    thank you very much.
    I'll start working on this.

    Best regards,

    Andrea

  • #36873
    Profile photo of Andrea Gaviraghi
    Andrea Gaviraghi
    Participant

    Hello,
    I'm playing around with Powershell, trying to achieve my goal...obviously I have some problems.
    First of all I really don't understand why if I open 4 Word 2010 docs, these code returns only 2 docs..

    $msword = [Runtime.Interopservices.Marshal]::GetActiveObject('Word.Application')
    $msword.documents | select Name

    Name
    —-
    TEST DOC – Copy.docx
    TEST DOC – Copy – Copy – Copy.docx

    I also open other 2 docs, "TEST DOC – Copy – Copy.docx" and "TEST DOC.docx".

    A similar issue occurs with PowerPoint.
    I open 3 .pptx:

    TEST DOC.pptx
    TEST DOC – Copy.pptx
    TEST DOC – Copy – Copy.pptx

    Now if I give these commands, PS correctly returns all 3 .pptx:

    $pp = [Runtime.Interopservices.Marshal]::GetActiveObject('Powerpoint.Application')
    $pp.presentations | select Name

    When I open 3 .pptx, these commands only returns 2 .pptx:

    Name
    —-
    TEST DOC.pptx
    TEST DOC – Copy – Copy.pptx
    TEST DOC – Copy.pptx

    But this only save 2 .pptx ("TEST DOC.pptx" and "TEST DOC – Copy – Copy.pptx")

    # SaveFolder
    $SAVEASDIR = "$ENV:USERPROFILE\Desktop\Security Saves\"
    if(!(Test-Path -Path $SAVEASDIR )){ New-Item -ItemType directory -Path $SAVEASDIR }

    #PowerPoint
    $MSPPOINT = [Runtime.Interopservices.Marshal]::GetActiveObject('Powerpoint.Application')
    $MSPPOINTDOCS = $MSPPOINT.Presentations
    $MSPPOINTDIRNAME = (Get-Date).tostring("dd-MM-yyyy_hh-mm-ss")
    $MSPPOINTSAVEASDIR = New-Item -itemType Directory -Path "$SAVEASDIR" -Name $MSPPOINTDIRNAME" Doc PowerPoint"
    foreach ($presentation in $MSPPOINTDOCS) {
    $presentation.SaveAs("$MSPPOINTSAVEASDIR\$($presentation.name)")
    $presentation.Close()
    }

    Excel seems to work as expected (to me :-)).
    Any help would be greatly appreciated.

    Thanks,

    Andrea

  • #36880
    Profile photo of Matt Bloomfield
    Matt Bloomfield
    Participant

    You'd have to check Task Manager but I'm guessing that you have multiple instances of the application open.

    How did you open the documents? If I click them one-by-one they all open in the same instance of Word and all five test documents are listed. However, if I select all five documents and hit Enter then two or more instances of the application get launched.

    I'm not sure how to handle multiple instances of the running application. I'd have to do some research to see if it's possible.

  • #36895
    Profile photo of Andrea Gaviraghi
    Andrea Gaviraghi
    Participant

    Hello Matt,
    thanks.
    are you talking about Running Object Table (ROT)?
    I would appreciate if you get back to me about this.

    From what I understand by reading https://support.microsoft.com/en-us/kb/316126, Excel and Word are defined Single Use (Multiple Instances), while PowerPoint is defined as Multiuse (Single Instance).

    I made some test but I'm confused...
    I have 3 documents for every app (Excel, PowerPoint and Word).
    Whether I open all documents together or one-by-one, both Excel and PowerPoint only create a single process of the application.
    In Word, if I open documents one-by-one, only 1 WINWORD.EXE process is created while if I open all documents together most of the time multiple WINWORD.EXE processes are created (rarely 1 process is created).

    So the "GetActiveObject" solution could be ok since no users open multiple docs together...

    But I'm really confused...

    Andrea

You must be logged in to reply to this topic.