Forum Discussion
Create a macro to crop images
- Feb 19, 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
Use
Dim myCrop As Crop
With ActiveDocument
For i = 1 To .InlineShapes.Count
With .InlineShapes(i)
Set myCrop = .PictureFormat.Crop
If .Height > .Width Then
myCrop.ShapeHeight = .Width
Else
myCrop.ShapeWidth = .Height
End If
End With
Next i
End With
Hi Doug.
This is great! however it crops to the left hand side of the image instead of cropping to the centre. Is there any way to achieve this?
This is what happens when I use the macro code that you have kindly supplied...
This is what happens when i crop an image to 1:1
- Feb 18, 2022
seanmccole The Crop>Aspect Ratio ability is not available in Visual Basic.
There are some other settings that can be applied to the Crop as detailed at
https://docs.microsoft.com/en-us/office/vba/api/overview/library-reference/crop-members-office
and I believe using the .ShapeLeft, .ShapeHeight and .ShapeWidth properties, you can probably achieve what you are after, BUT you would need to know the setting that needs to be used for .ShapeLeft. If that was likely to be constant for all of the images that you want to crop, you could find it by trial and error and then incorporate the setting into the code. If however it is likely to change from one image to the next, you are "up the creek without a paddle"
I believe those properties function in the same way as clicking on the grey area when using the Crop>Aspect Ratio>1:1 and moving the image in relation to the 1:1 shape, before clicking outside of the image to execute the crop.
- seanmccoleFeb 19, 2022Copper Contributor
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?- Feb 19, 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