PDA

View Full Version : Crop & Resize numerous images using VBA



arcade005
08-02-2020, 09:12 PM
Hi all,

I have to capture many computer screen, and put it into one word file for my boss checking daily.
I used to printscreen, paste to the Word, and manually crop and resize the pictures.

I am self-studying VBA and just found the below VBA code from Microsoft which might be useful for me.
https://docs.microsoft.com/zh-tw/office/vba/api/word.pictureformat.crop

To fit my situation, i have made some adjustment as below.

---
Sub CropNResize()


Dim myInlineShape As InlineShape
Dim myCrop As Crop

Set myInlineShape = ActiveDocument.InlineShapes(1)
Set myCorp = myInlineShape


With myCorp
.PictureFormat.CropLeft = 0
.PictureFormat.CropTop = 60
.PictureFormat.CropRight = 870
.PictureFormat.CropBottom = 370
.LockAspectRatio = True
.Height = 293
End With


Set myInlineShape = ActiveDocument.InlineShapes(2)
Set myCorp = myInlineShape


With myCorp
.PictureFormat.CropLeft = 0
.PictureFormat.CropTop = 60
.PictureFormat.CropRight = 870
.PictureFormat.CropBottom = 370
.LockAspectRatio = True
.Height = 293

End With
End Sub


Assuming if i have 100 pictures today, is there any method to cropNresize the images in few lines of command instead of repeating shape(1) shape(2)... shape(100)?
Thanks a lot.

arcade005
08-02-2020, 09:45 PM
Thanks all i have figured it out... though i still dont understand the logic and function of Dim....


Sub CropNResize()


Dim i As Long


With ActiveDocument
For i = 1 To .InlineShapes.Count
With .InlineShapes(i)
.PictureFormat.CropLeft = 0
.PictureFormat.CropTop = 60
.PictureFormat.CropRight = 870
.PictureFormat.CropBottom = 370
.LockAspectRatio = True
.Height = 293
End With
Next i


End With
End Sub

Paul_Hossler
08-03-2020, 07:00 AM
1. Welcome to the forum. Please take a minute and read the FAQs in the link in my sis=gnature

2. I added CODE tags around your macro. You can use the [#] icon button next time. It sets it off and formats it

3. 'Dim' (short for 'Dimension') is used to define a variable and give it a Type (Long in your example). Online help is pretty good for things like this. Using Dim is required if you use Option Explicit in your module