Forum Discussion
Create a macro to crop images
- Feb 20, 2022
seanmccole The information on using those properties is pretty sparse, but after a few attempts, I believe the applicable property is the .PictureFormat.Crop.PictureOffsetX and that the offset is from the center of the picture. For the result that you want, probably no offset is required.
Using the following code with an image that was distorted to the size of your image:
With ActiveDocument
For i = 1 To .InlineShapes.Count
With .InlineShapes(i)
.PictureFormat.Crop.ShapeWidth = .Height
.PictureFormat.Crop.ShapeHeight = .Height
.PictureFormat.Crop.PictureOffsetY = 0
.PictureFormat.Crop.PictureOffsetX = 0
End With
Next i
End Withthe result shown compared to an uncropped version of the image
Increasing the offset, by using
.PictureFormat.Crop.PictureOffsetX = 10
reduces the crop from the left
and reducing it by using
.PictureFormat.Crop.PictureOffsetX = -10
increases the crop from the left
Doug_Robbins_Word_MVP Ah, okay this is interesting.
All of my images are always a standard size so i should be able to use this command.
They are always 8.73cm x 16.51cm (247.4955 points x 468.0585 points)
to my calculations I'd need to come in 3.89cm (110.2815 points) from the left side. is that correct?
Apologies for my naivety when it comes to the code but would this be inserted before the code that you provided before?
seanmccole The information on using those properties is pretty sparse, but after a few attempts, I believe the applicable property is the .PictureFormat.Crop.PictureOffsetX and that the offset is from the center of the picture. For the result that you want, probably no offset is required.
Using the following code with an image that was distorted to the size of your image:
With ActiveDocument
For i = 1 To .InlineShapes.Count
With .InlineShapes(i)
.PictureFormat.Crop.ShapeWidth = .Height
.PictureFormat.Crop.ShapeHeight = .Height
.PictureFormat.Crop.PictureOffsetY = 0
.PictureFormat.Crop.PictureOffsetX = 0
End With
Next i
End With
the result shown compared to an uncropped version of the image
Increasing the offset, by using
.PictureFormat.Crop.PictureOffsetX = 10
reduces the crop from the left
and reducing it by using
.PictureFormat.Crop.PictureOffsetX = -10
increases the crop from the left
- Feb 21, 2022For that case, you should select the image and then use
With Selection.InlineShapes(1)
.PictureFormat.Crop.ShapeWidth = .Height
.PictureFormat.Crop.ShapeHeight = .Height
.PictureFormat.Crop.PictureOffsetY = 0
.PictureFormat.Crop.PictureOffsetX = 0
End With - seanmccoleFeb 21, 2022Copper Contributor
Doug, you are a legit genius!
Thanks very much for taking the time to look into this.
The information on this is pretty sparse yeah. I couldn't find an answer anywhere and even the support from the word developers was useless.This code obviously works for every image in the entire document, however, in the rare case that I might need to just crop and resize one image, do I just remove the "Next i" line of code?
Thanks again!