MS Word VBA - Modify all Shapes (Pictures) in ActiveDocument

kennyrogersjr

Board Regular
Joined
Apr 30, 2008
Messages
100
Hey folks,

Been awhile since I've needed to consult my go-to forum for VBA questions. I found some hints stating that the code-junkies perusing Mr. E's interwebs like to go a few rounds with the other MS VBA dialects.

If someone can point out my flaw, I would appreciate it. I can't get past the "Invalid use of property" compile error on my first "si.ScaleHeight" line. See below:
Code:
Sub CenterPics()

Dim si As InlineShape
Dim s As Shape

Set docAD = ActiveDocument

    For Each si In docAD.Shapes
        Select Case si.Type
            Case msoEmbeddedOLEObject, msoLinkedOLEObject, msoOLEControlObject, msoLinkedPicture, msoPicture
                si.ScaleHeight 0.89, True
                si.ScaleWidth 0.89, True
                si.Left = wdShapeCenter
                si.Top = wdShapeCenter
                si.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
                si.RelativeVerticalPosition = wdRelativeVerticalPositionPage
            Case Else
                si.ScaleHeight 0.89, False
                si.ScaleWidth 0.89, False
                si.Left = wdShapeCenter
                si.Top = wdShapeCenter
                si.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
                si.RelativeVerticalPosition = wdRelativeVerticalPositionPage
        End Select
    Next

    For Each s In docAD.Shapes
        Select Case s.Type
            Case msoEmbeddedOLEObject, msoLinkedOLEObject, msoOLEControlObject, msoLinkedPicture, msoPicture
                s.ScaleHeight 0.89, True
                s.ScaleWidth 0.89, True
                s.Left = wdShapeCenter
                s.Top = wdShapeCenter
                s.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
                s.RelativeVerticalPosition = wdRelativeVerticalPositionPage
            Case Else
                s.ScaleHeight 0.89, False
                s.ScaleWidth 0.89, False
                s.Left = wdShapeCenter
                s.Top = wdShapeCenter
                s.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
                s.RelativeVerticalPosition = wdRelativeVerticalPositionPage
        End Select
    Next
End Sub

Been pouring over MSDN and various forums to find my error and I just can't see what I did wrong. A second pair of eyes reviewing my code would be helpful.

Ken
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Well, the InlineShape Object cannot support the ScaleHeight property... I ended up running with this simplified version and it worked for mostly all of my pictures. I had a few random ones that the original picture size was astronomical and the picture ended up around 400% larger than what it was scaled to, but the script handled the rest of the tedious work.

Code:
Sub CenterPics()

Dim si As InlineShape
Dim s As Shape

Set docAD = ActiveDocument

    For Each s In docAD.Shapes
                s.ScaleHeight 0.89, True 'Sets scale to 89% of original picture/shape size
                s.ScaleWidth 0.89, True 'Sets scale to 89% of original picture/shape size
                s.Left = wdShapeCenter 'Centers Horizontally
                s.Top = wdShapeCenter 'Centers Vertically
                s.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage 'Does it again
                s.RelativeVerticalPosition = wdRelativeVerticalPositionPage 'Does it again
    Next
End Sub

If anyone has any improvements or features to add, post away. I don't mind. :)
 
Upvote 0

Forum statistics

Threads
1,225,653
Messages
6,186,203
Members
453,340
Latest member
yearego021

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