PDA

View Full Version : Word 2007 Change Shape Fill Picture and maintain Aspect Ratio



gmaxey
03-09-2016, 07:06 AM
In Word 2010 and above I can change the picture filling a shape using two simple lines of code. I select the shape and run:

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
CommandBars.ExecuteMso ("ObjectPictureFill")
CommandBars.ExecuteMso ("PictureFillCrop")
lbl_Exit:
Exit Sub
End Sub

If the replacement image ha a different aspect ratio then it I automatically cropped to fit the shape.

Unfortunately Word 2007 does not have the "PictureFillCrop" feature. Revising the code as follows will run in Word 2007 but the result is usually a distorted image (i.e., a fat man may appear like a thin man) as the entire picture is distorted to fit into the shape.

Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
CommandBars.ExecuteMso ("ObjectPictureFill")
If Application.Verion > 12.0 Then CommandBars.ExecuteMso ("PictureFillCrop")
lbl_Exit:
Exit Sub
End Sub

Now after the fact, I can:


Right-click the selected shape, and then click Format AutoShape.
On the Colors and Lines tab, click Fill Effects.
Click the Picture tab.
Click Select Picture.
Click the picture that you want as the picture fill for the shape, and then click Insert.
On the Picture tab, click to select the Lock picture aspect ratio check box.
Click OK to close the Fill Effects dialog box.
Click OK to close the Format AutoShape dialog box.


This results in the image returning to the correct aspect ration and cropped right and bottom to fit the shape. The fat man is still fat even if I can only see 3/4 of him (that is ok)

I can even record a macro of performing those steps.
However, attempts to run that recorded macro results in run-time errors or has no effect on a shape with a distorted image.

Does anyone know how to overcome this issue and replace a shape fill picture with another picture and maintain the aspect ratio?

Thanks