none
powershell: Remove Pictures from Word Document RRS feed

  • Question

  • Hi,

    i want to remove all Pictures from my word document via powershell.

    With this code i can see, that there are 2 pictures in the Word Document. But how to remove them?

    $MSWord = New-Object -com word.application
    $MSWord.Visible = $false
    $WorkFile = "C:\XY.docx"
    $doc = $MSWord.Documents.Open($WorkFile)
    $doc.InlineShapes.Count

    Thank you

    • Moved by Bill_Stewart Monday, June 10, 2013 2:02 PM Move to more appropriate forum
    Monday, June 10, 2013 1:54 PM

Answers

  • Thank you very much. with a loop i am able to delete it as expected.
    My mistake!

    $MSWord = New-Object -com word.application
    $MSWord.Visible = $false
    $WorkFile = "C:\XY.docx"
    $doc = $MSWord.Documents.Open($WorkFile)
    foreach($obj in $doc.InlineShapes){$obj.Delete()}
    $MSWord.ActiveDocument.Save()
    $MSWord.ActiveDocument.Close()
    $MSWord.Quit()

    • Marked as answer by MC-FSP Tuesday, June 11, 2013 8:41 AM
    Tuesday, June 11, 2013 8:40 AM

All replies

  • I'm not familiar with Power Shell so please forgive me... but if you want to remove something can't you just delete it??

    Kind Regards, Rich ... http://greatcirclelearning.com

    Monday, June 10, 2013 11:49 PM
  • You should be able to loop the InlineShapes collection and delete one item at a time. I can't tell you the proper syntax of the loop, though (because I'm not familiar with Powershell either).

    Stefan Blom, Microsoft Word MVP
    Tuesday, June 11, 2013 12:00 AM
  • Thank you very much. with a loop i am able to delete it as expected.
    My mistake!

    $MSWord = New-Object -com word.application
    $MSWord.Visible = $false
    $WorkFile = "C:\XY.docx"
    $doc = $MSWord.Documents.Open($WorkFile)
    foreach($obj in $doc.InlineShapes){$obj.Delete()}
    $MSWord.ActiveDocument.Save()
    $MSWord.ActiveDocument.Close()
    $MSWord.Quit()

    • Marked as answer by MC-FSP Tuesday, June 11, 2013 8:41 AM
    Tuesday, June 11, 2013 8:40 AM