MS Word 2013 VBA - Image Compression Fix

kennyrogersjr

Board Regular
Joined
Apr 30, 2008
Messages
100
I figured out a way to eliminate that frustrating Compress Pictures bug that won't allow you to really compress that image.

Scenario: You re-size an image and you also crop out some white space. You save your document, which is set to compress images on save, and continue editing. Well, you go to email that document you created and realize that the file size is grossly out of proportion to what it should be. So you go back to check on the culprit and, low and behold, it's that image you compressed. You take a look at it and the scale ratio is still set to 33%, in some cases the cropped area wasn't deleted, and it didn't even compress.

Fix: You can manually Ctrl + X and Ctrl + Shift + V (Paste Special) the image to allow MS Word to recognize the image as an image, or you can run the below code:

Code:
Public Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function EmptyClipboard Lib "user32" () As Long
Public Declare Function CloseClipboard Lib "user32" () As Long
Public Function ClearClipboard()
    OpenClipboard (0&)
    EmptyClipboard
    CloseClipboard
End Function
Sub CompressImageFix()

Dim s As Shape

Set docAD = ActiveDocument

For Each s In docAD.Shapes
    s.Select
    Selection.Cut
    Selection.PasteSpecial Link:=False, DataType:=15, Placement:=wdTightAll, DisplayAsIcon:=False
    Selection.Collapse    'IMPORTANT - Allows deselection of current object
    ClearClipboard          'IMPORTANT - Function stops unintentional pasting of prior copied objects over next object
Next s

End Sub

This code is really intended for those of us that need a couple hundred images to process. Feel free to criticize, it won't hurt my feelings. Plus, it helps others, too.

Ken
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
I figured out a way to eliminate that frustrating Compress Pictures bug that won't allow you to really compress that image.

This code is really intended for those of us that need a couple hundred images to process. Feel free to criticize, it won't hurt my feelings. Plus, it helps others, too.

Ken

:laugh:"Feel free to criticize, it won't hurt my feelings."Thank you for sharing this for us. And I will try and give the feedback to you.:)
 
Upvote 0

Forum statistics

Threads
1,225,656
Messages
6,186,244
Members
453,343
Latest member
hacigultekin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top