+ Post New Thread
Page 1 of 2 12 LastLast
Results 1 to 15 of 20
Scripts Thread, Manipulating Office (Word/PowerPoint) via PowerShell in Coding and Web Development; Huzzah! It's time for my monthly "Stuff don't work like it should do" coding thread! Code: $MSword = New-Object -ComObject ...
  1. #1


    Join Date
    Jan 2012
    Posts
    2,626
    Thank Post
    938
    Thanked 351 Times in 267 Posts
    Rep Power
    213

    Manipulating Office (Word/PowerPoint) via PowerShell

    Huzzah! It's time for my monthly "Stuff don't work like it should do" coding thread!

    Code:
    $MSword = New-Object -ComObject word.application
    	$MSword.Visible = $False
    	$wdTypes = Add-Type -AssemblyName 'Microsoft.Office.Interop.Word' -Passthru
    	$wdSaveFormat = $wdTypes | Where {$_.Name -eq "wdSaveFormat"}
    	$MSword.documents.open(".DOC FILE")
    		$MSWord.ActiveDocument.saveas([ref]".HTM FILE", [ref]$wdSaveFormat::wdFormatHTML);
    		$MSWord.ActiveDocument.Close([ref]0);
    	$MSWord.Quit([ref]0);
    This works fine. It silently opens up a document in Word, saves it as a HTM file (We use it for digital signage), closes the file, doesn't save changes, and quits word. Huzzah. Now I want to do something similar in PowerPoint. Open up the file, save as JPG, quit. So I start to re-work my code. According to the internets, powerpoint.application ought to work. Good. Let's fiddle around some more.

    Code:
    $MSPPT = New-Object -ComObject powerpoint.application
    	$MSPPT.visible = $True
    	$MSPPT.presentations.open(".PPT FILE")
    Plan was to keep visible as TRUE until it works, then change to FALSE. Standard debugging.

    However, "Stuff don't work like it should do", you see.

    PPT.png

    However, crawling around the dregs on the internets I find that
    Code:
    $MSPPT.visible = [Microsoft.Office.Core.MsoTriState]::msoTrue
    works file, and running that opens up PowerPoint, void of any presentation, as it should. However, opening a presentation still fails and I'm stuck finding information on this error code because it appears to apply to a lot of different programs.

    Why does it 'just work' for Word but not for Powerpoint?
    Last edited by Garacesh; 3rd June 2014 at 12:01 PM.

  2. #2

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,440
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    OK, I'll start by saying I know almost nothing about Powershell, however I do have some experience with Office scripting.

    I believe the problem you're having is that Powerpoint won't allow you to open a presentation while it's hidden. However, there's a workaround: you can open the presentation hidden with an overloaded Open() method, like so:

    Code:
    $MSPPT = New-Object -ComObject powerpoint.application
        $MSPPT.presentations.open("C:\test.pptx",[Microsoft.Office.Core.MsoTriState]::msoFalse,[Microsoft.Office.Core.MsoTriState]::msoFalse,[Microsoft.Office.Core.MsoTriState]::msoFalse)
    All very backwards but that works (for me at least) and you'd never know Powerpoint was open.

    More info on that overloaded Open method: PPT97: How to Use the Presentations.Open Method

    EDIT: how do you do syntax highlighting on here BTW? Tried quoting your post to see it but it just had a load of 'color' tags - you didn't do it manually, did you?!
    Last edited by LosOjos; 3rd June 2014 at 11:52 AM.

  3. #3


    Join Date
    Jan 2012
    Posts
    2,626
    Thank Post
    938
    Thanked 351 Times in 267 Posts
    Rep Power
    213
    Yes, I did it manually. Syntax highlighting makes it a lot easier for me to read my own code. There's only 4 colours, it's not really difficult to do. Only took ≈60 seconds.

    That didn't work either.. Same error, but Exception calling "Open" with "4" argument(s) this time, instead of just "1" argument(s)
    Also, currently PowerPoint isn't hidden, Visible is True and it does actually open PowerPoint when I use the (...)::msoTrue line.

  4. #4

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,440
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    Bit of a long shot but have you tried it exactly as I posted it i.e. without the 'visible' line?
    Also, have you tried opening the file manually to check it's not corrupt?

  5. #5


    Join Date
    Jan 2012
    Posts
    2,626
    Thank Post
    938
    Thanked 351 Times in 267 Posts
    Rep Power
    213
    Yep, didn't work.

    BUT
    I have made progress.

    Faffing around with it (testing different paths, etc), I've discovered that the error is caused by the file currently being in use by another user (Which is actually extremely common, but doesn't actually matter because I just want to SAVE AS), so now I need to figure out how to make it open as Read Only (or, if prompted, reply with "Yes, open as Read-Only, similar to how my Word script automatically decides "No, don't save changes" when you exit).
    Edit:
    Code:
    $MSPPT.presentations.open(".PPT FILE", 2, $True)
    Last edited by Garacesh; 3rd June 2014 at 12:30 PM.

  6. #6

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,440
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    Quote Originally Posted by Garacesh View Post
    Yep, didn't work.

    BUT
    I have made progress.

    Faffing around with it (testing different paths, etc), I've discovered that the error is caused by the file currently being in use by another user (Which is actually extremely common, but doesn't actually matter because I just want to SAVE AS), so now I need to figure out how to make it open as Read Only (or, if prompted, reply with "Yes, open as Read-Only, similar to how my Word script automatically decides "No, don't save changes" when you exit).
    OK great, narrowing it down! If you change the second argument in the overloaded Open method I posted earlier to 'msoTrue', it'll open read-only - perhaps that'll be enough to overcome the problem?

  7. #7


    Join Date
    Jan 2012
    Posts
    2,626
    Thank Post
    938
    Thanked 351 Times in 267 Posts
    Rep Power
    213
    Using
    Code:
    $MSPPT.presentations.open(".PPT FILE", 2, $True)
    worked! (yay internet), though I suspect your lines may be doing the same thing, just the 'longhand' way?

    So, we've got the document open. Now to figure out how to save each slide as a JPG. Onwards and upwards!

    Edit: Getting a lot of "You cannot call a method on a null-valued expression.".. It still doesn't want to admit that $MSPPT is a real thing..
    Also $MSPPT.Quit([ref]0) doesn't work. "Cannot find an overload for "Quit" and the argument count: "1"."
    Last edited by Garacesh; 3rd June 2014 at 12:47 PM.

  8. #8

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,440
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    Slides have an Export method which is simply

    Code:
    Slide.Export(string filename, string filetype)
    
    E.G.
    
    Slide.Export("C:\slide.jpg", "JPG")
    You can get a collection fo slides from ActivePresentation.Slides or pick an individual one by specifying it's index (starting from 1) e.g. ActivePresentation.Slides(1)

    Just brain dumping - hope it helps!

  9. #9

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,440
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    You may have already cracked it, but just in case, this does it:

    Code:
    $MSPPT = New-Object -ComObject powerpoint.application
    $PRES = $MSPPT.presentations.open("C:\test.pptx", 2, $True, $False)
    foreach($Slide in $PRES.slides)
    {
        $Slide.Export("C:\" + $Slide.Name + ".jpg", "JPG")
    }
    $MSPPT.Quit()
    You were right RE the Open method being the same as I was using, didn't realise it'd accept bools (assumed it wouldn't because of earlier error!). The last argument is the one that sets whether it's shownor not, so change that to $True (or omit it entirely) if you want to see the presentation open

    Problem I'm having is that PowerPoint won't quit - according to the documentation, you don't/can't set an exit code, which would make your "no overload" error ring true, however if I check taskman PowerPoint continues running...
    Last edited by LosOjos; 3rd June 2014 at 01:20 PM.

  10. #10

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,440
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    Supposedly you're supposed to use ReleaseComObject to properly terminate some Office applications after calling Quit, but that doesn't seem to work for me either:

    Code:
    [System.Runtime.InteropServices.Marshal]::ReleaseComObject($MSPPT)
    Source: Windows PowerShell Tip: Getting Rid of a COM Object (Once and For All)

  11. #11


    Join Date
    Jan 2012
    Posts
    2,626
    Thank Post
    938
    Thanked 351 Times in 267 Posts
    Rep Power
    213
    Aha!
    Code:
    $MSPPT.ActivePresentation.SaveAs("LOCATION", [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsJPG);
    Saves each slide to the location as Slide1, Slide2, Slide3 etc. So that's the next step completed!
    (I did originally try saving just as a JPG, but that saved the entire file as one JPG file.. Not quite what I was after lol)

    Now to get it to close..
    Code:
    $MSPPT.ActivePresentation.Close
    $MSPPT.Quit
    doesn't seem to be working. The PowerPoint stays open (Both program and presentation) and the command line just prints out this:
    PPT2.png


    Edit: I could force it to terminate with Stop-Process, I guess. Kludgy.
    Last edited by Garacesh; 3rd June 2014 at 01:37 PM.

  12. #12

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,440
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    Have you tried with the ReleaseComObject method I posted/linked to above? Could be that I'm using an old version of PowerShell (I think) that it won't work for me

  13. #13


    Join Date
    Jan 2012
    Posts
    2,626
    Thank Post
    938
    Thanked 351 Times in 267 Posts
    Rep Power
    213
    Yeah I tried that. Bizarrely, it prints "1" in the command line and does nothing else.

    Looks like I'm going to have to settle for:
    Code:
    $MSPPT = New-Object -ComObject powerpoint.application
    	$ppTypes = Add-Type -AssemblyName 'Microsoft.Office.Interop.Powerpoint' -Passthru
    	$ppSaveFormat = $ppTypes | Where {$_.Name -eq "ppSaveFormat"}
    	$MSPPT.presentations.open(".PPT FILE", 2, $True)
    		$MSPPT.ActivePresentation.SaveAs("LOCATION", [Microsoft.Office.Interop.PowerPoint.PpSaveAsFileType]::ppSaveAsJPG);
    Stop-Process -name "POWERPNT" -force
    $MSPPT = $Null
    Annoyingly, changing $MSPPT.visible = [Microsoft.Office.Core.MsoTriState]::msoTrue to $MSPPT.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse errors, so no doing it silently for me
    Still. It is what it is. It's working, at least.

  14. #14
    ascott2's Avatar
    Join Date
    Nov 2007
    Posts
    181
    Thank Post
    18
    Thanked 37 Times in 29 Posts
    Rep Power
    20
    Try
    $MSPPT.ActivePresentation.Close()
    $MSPPT.Quit()

    Both are methods, without the parenthesis, you are just outputting details of the method.

    Also you may wasn't to use [System.Runtime.Interopservices.Marshal]::ReleaseComObject($MSPPT) after as that will dispose of the Comobject and terminate the processes correctly. Ref http://technet.microsoft.com/en-us/l.../ff730962.aspx

    Hope that helps.
    Last edited by ascott2; 3rd June 2014 at 02:06 PM.

  15. #15

    LosOjos's Avatar
    Join Date
    Dec 2009
    Location
    West Midlands
    Posts
    5,452
    Thank Post
    1,440
    Thanked 1,170 Times in 798 Posts
    Rep Power
    707
    Quote Originally Posted by Garacesh View Post
    Annoyingly, changing $MSPPT.visible = [Microsoft.Office.Core.MsoTriState]::msoTrue to $MSPPT.visible = [Microsoft.Office.Core.MsoTriState]::msoFalse errors, so no doing it silently for me
    Still. It is what it is. It's working, at least.
    ...I posted how to do that waaay back just remove the $MSPPT.Visible line entirely and tag another $False on the end of the Open line, it works, honest!

    EDIT: the RemoveComObject printing 1 is actually a good sign, there's a KB article I was reading earlier that stated it decrements references each time it runs, so should output 0 when all objects are released. The workaround (nasty though it was) was to put it in a loop like so:

    Code:
    while([System.Runtime.InteropServices.Marshal]::ReleaseComObject($MSPPT) -gt 0){}
    EDIT: here's the KB - http://support.microsoft.com/kb/317109
    Last edited by LosOjos; 3rd June 2014 at 02:10 PM.

SHARE:
+ Post New Thread
Page 1 of 2 12 LastLast

Similar Threads

  1. [MS Office - 2007] MAC Document saved as .doc but won't open in MS Office Word
    By speckytecky in forum Office Software
    Replies: 0
    Last Post: 28th February 2011, 11:09 AM
  2. [MS Office - 2010] Fonts crashing Word/PowerPoint/Publisher
    By jlucas in forum Office Software
    Replies: 1
    Last Post: 1st February 2011, 11:28 AM
  3. Deploying Open Office 3.2 via GPO Update 1
    By networkingNut in forum Windows Server 2000/2003
    Replies: 1
    Last Post: 6th May 2010, 05:46 PM
  4. Open Office & Word
    By Domino in forum Office Software
    Replies: 13
    Last Post: 11th February 2008, 01:14 PM
  5. Office Word 2007
    By wesleyw in forum How do you do....it?
    Replies: 1
    Last Post: 6th July 2007, 06:49 AM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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