locked
Compress pictures using macro in Word RRS feed

  • Question

  • Hi,

    I am using MS Word 2010 and have a piece of code that changes pictures attributes:

    Sub ChangeImageBorder()

        Dim iShape As InlineShape

        For Each iShape In ActiveDocument.InlineShapes

            With iShape

                With .Borders(wdBorderLeft)

                    .LineStyle = wdLineStyleSingle

                    .LineWidth = wdLineWidth025pt

                    .Color = wdColorBlack

                End With

            End With

        Next iShape

    End Sub

     

    This works fine.

    I am trying to use the macro that reduces the size of the PPI to 96 and when I record this process nothing gets recorded.

    Please can you help?

    Many thanks

    Peter

    • Moved by Reed KimbleMVP Monday, April 21, 2014 2:42 PM Word macro/vba question posted to VB.Net
    Monday, April 21, 2014 8:30 AM

Answers

  • AFAIK, the only way to compress pictures programmatically is via code like:

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

    Note that the above compresses all pictures in the document; there doesn't seem to be a way to compress just one. Depending on Word's settings, when you run the above code you may also get a pop-up warning you about the consequences of continuing.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Knowledge Gaps Wednesday, April 23, 2014 3:13 AM
    Tuesday, April 22, 2014 12:33 AM

All replies

  • Hi Peter,

    You've posted to the VB.Net forum; we won't be able to help you with Word Macros or VBA from here.

    I'll move this to the Word Developers forum.  You should be able to get help there, or a moderator of that forum may be able to point you to an even more appropriate place.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Monday, April 21, 2014 2:41 PM
  • AFAIK, the only way to compress pictures programmatically is via code like:

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

    Note that the above compresses all pictures in the document; there doesn't seem to be a way to compress just one. Depending on Word's settings, when you run the above code you may also get a pop-up warning you about the consequences of continuing.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Marked as answer by Knowledge Gaps Wednesday, April 23, 2014 3:13 AM
    Tuesday, April 22, 2014 12:33 AM
  • Hi Paul,

    Thank you for this, but for some reason it does not do anything. Please can you be so kind as to explain what each step should do?

    Should I be doing anything else before running this macro?

    Thanks, Peter

    Wednesday, April 23, 2014 3:16 AM
  • You wouldn't necessarily expect to see any difference in the document; only in the file size and then only if the compression materially changed the picture's resolution. Compression doesn't change the picture's dimensions and won't have any effect on vector images (e.g. Word autoshapes).

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Wednesday, April 23, 2014 4:58 AM
  • This solutions works fine. And there IS a way to do it only to a selected picture by using:

    SendKeys "%S%W{Enter}"

    And you can also compress to print format (200dpi) instead of web format:

    SendKeys "%S%P{Enter}"

    _________

    Sylvain

    Sunday, March 19, 2017 3:54 PM
  • Is there a way to compress images programatically on Word for Mac?  The above solution fails to compile :

    SendKeys "%A%W{Enter}" - Invalid Procedure call or arguement

    Thanks

    Friday, July 31, 2020 8:05 AM
  • I had to do this for a Word addin.  The compression Word provides wasn't good enough since doc could have 80-100 images.  I used ImageMagick (free).  It is a very powerful image manipulation program.   You can call it from the command line but also has a .COM interface so you can call it directly from VBA
    • Edited by mogulman52 Thursday, October 1, 2020 1:38 PM
    Thursday, October 1, 2020 12:54 AM