none
powershell word api compress pictures RRS feed

  • Question

  • Hello everyone,

    I have a PS 3.0 script that i use to process a folder of word documents and convert then to .docx format. I want to also compress images in the documents (picture tools -> format -> compress pictures, the tab that appears when you click on any picture) but i can't find the proper methods or properties in the word api to do so. My code is:

    "$docPath = "C:\Documents"
    $all_docs = Get-ChildItem $docPath -recurse -filter "*.doc*"
    $word = New-Object -comobject "Word.Application"
    $word.Visible = $false
    foreach ($doc in $all_docs){

    Write-Host "Processing: " $doc.FullName
    $doc = $word.Documents.Open($doc.FullName)
    $FileName = $doc.FullName
    if ($FileName.ToLower().EndsWith(".doc"))
    {
        $FileName = $FileName+"x"
    }
    $FileFormat=16
    $doc.convert()
    $doc.EmbedTrueTypeFonts = $false
    $doc.SaveAs($FileName,$FileFormat)
    $doc.Close()

    }

    $Word.Quit()
    $Word = ""

    Get-ChildItem $docPath -recurse -exclude "*.docx" | Remove-Item"

    I've looked at the document object or document.shapes and others but can't find any functions that has to to with compressing the pictures in the document.

    Any help is greatly appreciated.

    Thank you!

    Tuesday, May 14, 2013 6:51 AM

Answers

  • I want to also compress images in the documents (picture tools -> format -> compress pictures, the tab that appears when you click on any picture) but i can't find the proper methods or properties in the word api to do so.

    I am not familiar with PowerShell Script, but I can suggest you a way in VBA.

    You can use VBA code to automate the Compress Pictures button by CommandBar.FindControl Method.

    Sub CompressPics()
        With Application.CommandBars.FindControl(ID:=6382)
            SendKeys "%A%W{Enter}"
            .Execute
        End With
    End Sub

    To look for the control's ID, you can download the help files from Microsoft Download Center:

    Office 2010 Help Files: Office Fluent User Interface Control Identifiers

    Policy ID of "PicturesCompress" is 6382.

    Thanks,


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 16, 2013 12:40 PM
    Moderator
  • Thanks for the idea of sending keyboard commands to the application.

    The solution in powershell goes like:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")

    Start-Sleep -s 1
    $a = Get-Process | Where-Object {$_.Name -eq "WINWORD"}
    [Microsoft.VisualBasic.Interaction]::AppActivate($a.ID)
    [System.Windows.Forms.SendKeys]::SendWait("%4AN{ENTER}")
    Start-Sleep -s 1

    The sleep commands are there to ensure the word finishes opening the document before sending the keystrokes and converts everything before saving, otherwise occasionally doesn't work as expected .

    The keys are "%4AN{ENTER}" meaning "ALT+4 A N ENTER"

    I've inserted the compress picture shortcut into the Quick Access Toolbar (Word Options->Customize) and now all i have to do to open the Compress Pictures dialog is to press ALT+4.

    The nuisance now is that this is no longer a background process and you can't do other stuff... still it does the job.

    Thanks again!


    Thursday, May 30, 2013 11:23 AM

All replies

  • I want to also compress images in the documents (picture tools -> format -> compress pictures, the tab that appears when you click on any picture) but i can't find the proper methods or properties in the word api to do so.

    I am not familiar with PowerShell Script, but I can suggest you a way in VBA.

    You can use VBA code to automate the Compress Pictures button by CommandBar.FindControl Method.

    Sub CompressPics()
        With Application.CommandBars.FindControl(ID:=6382)
            SendKeys "%A%W{Enter}"
            .Execute
        End With
    End Sub

    To look for the control's ID, you can download the help files from Microsoft Download Center:

    Office 2010 Help Files: Office Fluent User Interface Control Identifiers

    Policy ID of "PicturesCompress" is 6382.

    Thanks,


    Damon Zheng
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, May 16, 2013 12:40 PM
    Moderator
  • Thanks for the idea of sending keyboard commands to the application.

    The solution in powershell goes like:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("'System.Windows.Forms")
    [void] [System.Reflection.Assembly]::LoadWithPartialName("'Microsoft.VisualBasic")

    Start-Sleep -s 1
    $a = Get-Process | Where-Object {$_.Name -eq "WINWORD"}
    [Microsoft.VisualBasic.Interaction]::AppActivate($a.ID)
    [System.Windows.Forms.SendKeys]::SendWait("%4AN{ENTER}")
    Start-Sleep -s 1

    The sleep commands are there to ensure the word finishes opening the document before sending the keystrokes and converts everything before saving, otherwise occasionally doesn't work as expected .

    The keys are "%4AN{ENTER}" meaning "ALT+4 A N ENTER"

    I've inserted the compress picture shortcut into the Quick Access Toolbar (Word Options->Customize) and now all i have to do to open the Compress Pictures dialog is to press ALT+4.

    The nuisance now is that this is no longer a background process and you can't do other stuff... still it does the job.

    Thanks again!


    Thursday, May 30, 2013 11:23 AM