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:

  1. Inline
  2. 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:

Word, VBA Image Model
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.


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:

Word, Image, Crop, Right
Result:
Word, Image, Crop, Right, Result


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:

Word, VBA, Crop, Bottom, Section

Result:
Word, VBA, Crop Bottom, Result


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:

  1. Move the picture inside its shape container to the left
  2. 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 Left, Before

Result:
Crop Left After


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:

  1. Move the picture inside its shape container to the top
  2. 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:

Crop Top Before

Result:

Crop Top After

You can download the files and code related to this article from the links below:

See also:

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

Leave a Reply

Your email address will not be published. Required fields are marked *

Search