Word VBA, Crop Images
In this article I will explain how you can use VBA for word to crop images.
The first step is to determine the type of image. There are 2 types of images:
- Inline
- Floating
I’ve previously explained about the 2 image types in the article below:
Inline images are referenced through the InlineShape
collection and the floating images are referenced through the Shape
collection.
Pictures are contained within a shape container:
In order to crop the image we need to modify the width and height of the shape container.
Note: Modifying the width and height of the picture itself will result in the image resizing rather than cropping.
Contents
Crop Right Side of Image:
Inline Image: Assuming the image we want to crop is the first inline shape in the document, the code below will crop the right side of the image by 20%:
InlineShapes.Item(1).PictureFormat.Crop.ShapeWidth _
= InlineShapes.Item(1).Width * 0.8
Floating Image: Assuming the image we want to crop is the first floating shape in the document, the code below will crop the right side of the image by 20%:
Shapes.Item(1).PictureFormat.Crop.ShapeWidth _
= Shapes.Item(1).Width * 0.8
Crop section:
Crop bottom side of Image:
Inline Image: Assuming the image we want to crop is the first inline shape in the document, the code below will crop the bottom side of the image by 20%:
InlineShapes.Item(1).PictureFormat.Crop.ShapeHeight _
= InlineShapes.Item(1).Height* 0.8
Floating Image: Assuming the image we want to crop is the first floating shape in the document, the code below will crop the bottom side of the image by 20%:
Shapes.Item(1).PictureFormat.Crop.ShapeHeight _
= Shapes.Item(1).Height* 0.8
Crop section:
Crop Left Side of Image:
Cropping the left side of the image is not as straight forward as cropping the right side. In order to crop the left side we will have to follow the steps below:
- Move the picture inside its shape container to the left
- Reduce the width of the shape container >> therefore creating the cropping effect
Inline Image: Assuming the image we want to crop is the first inline shape in the document, the code below will crop the left side of the image by 20%:
Sub main()
Dim dblPercent2Crop As Double
dblPercent2Crop = 20
'move the image
InlineShapes.Item(1).PictureFormat.Crop.PictureOffsetX _
= -InlineShapes.Item(1).Width * (dblPercent2Crop / 100)
'crop
InlineShapes.Item(1).PictureFormat.Crop.ShapeWidth _
= InlineShapes.Item(1).Width * (1 - dblPercent2Crop / 100)
End Sub
Floating Image: Assuming the image we want to crop is the first floating shape in the document, the code below will crop the left side of the image by 20%:
Sub main()
Dim dblPercent2Crop As Double
dblPercent2Crop = 20
'move the image
Shapes.Item(1).PictureFormat.Crop.PictureOffsetX _
= -Shapes.Item(1).Width * (dblPercent2Crop / 100)
'crop
Shapes.Item(1).PictureFormat.Crop.ShapeWidth _
= Shapes.Item(1).Width * (1 - dblPercent2Crop / 100)
End Sub
Crop section:
Crop Top Side of Image:
Cropping the top side of the image is not as straight forward as cropping the bottom side. In order to crop the top side we will have to follow the steps below:
- Move the picture inside its shape container to the top
- Reduce the height of the shape container >> therefore creating the cropping effect
Inline Image: Assuming the image we want to crop is the first inline shape in the document, the code below will crop the top side of the image by 20%:
Sub main()
Dim dblPercent2Crop As Double
dblPercent2Crop = 20
'move the image
InlineShapes.Item(1).PictureFormat.Crop.PictureOffsetY _
= -InlineShapes.Item(1).Height * (dblPercent2Crop / 100)
'crop
InlineShapes.Item(1).PictureFormat.Crop.ShapeHeight _
= InlineShapes.Item(1).Height * (1 - dblPercent2Crop / 100)
End Sub
Floating Image: Assuming the image we want to crop is the first floating shape in the document, the code below will crop the top side of the image by 20%:
Sub main()
Dim dblPercent2Crop As Double
dblPercent2Crop = 20
'move the image
Shapes.Item(1).PictureFormat.Crop.PictureOffsetY _
= -Shapes.Item(1).Height * (dblPercent2Crop / 100)
'crop
Shapes.Item(1).PictureFormat.Crop.ShapeHeight _
= Shapes.Item(1).Height * (1 - dblPercent2Crop / 100)
End Sub
Crop section:
Result:
You can download the files and code related to this article from the links below:
See also:
- Word VBA Insert Images
- Word VBA, Loop Through Images
- Word VBA, Picture Format CropBottom, CropLeft, CropRight and CropTop
- Microsoft MSDN Word 2010: Add Picture Shapes and Format Cropping Using Word.PictureFormat.Crop
If you need assistance with your code, or you are looking for a VBA programmer to hire feel free to contact me. Also please visit my website www.software-solutions-online.com