VBA to Crop Image to 1:1 Aspect Ratio

Status
Not open for further replies.

ViperDriver35

New Member
Joined
Mar 18, 2018
Messages
7
Hello, I am attempting to use VBA to crop images to a 1:1 aspect ratio. Here is the code that I am using currently:

With shp
Width = .Width
Height = .Height
TCrop = Width - Height
Hcrop = TCrop / 2
End With
With .PictureFormat
.CropLeft = Hcrop
.CropTop = 0
.CropBottom = 0
.CropRight = Hcrop
End With

It is not working and more confusingly, it is not delivering consistent results among images with different initial aspect ratios (i.e. 16:9 and 4:3 do not end up with the same aspect ratio after the macro is run). Any help would be greatly appreciated. I have run out of troubleshooting ideas. Thanks!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Re: VBA Help to Crop Image to 1:1 Aspect Ratio

Try adding this line in this position:
Code:
With shp
.ShapeRange.LockAspectRatio = msoFalse     '<<< ADD
Width = .Width
'etc

And you should avoid using keywords as "variables" name (eg Width, Height)

Bye
 
Last edited:
Upvote 0
Re: VBA Help to Crop Image to 1:1 Aspect Ratio

Thank you for the reply! I attempted your solution and got a "Run-time error '438': Object doesn't support this property or method." I removed the ".ShapeRange" and the error goes away but the results are the same still (an 8 inch by 9 inch image). Thanks again for the help!
 
Upvote 0
Re: VBA Help to Crop Image to 1:1 Aspect Ratio

Could you share a sample workbook containing an Image, the macro, the image resulting from running the macro and the description of what is wrong (with respect to what you wish to obtain) in the resulting image?
For sharing a file you could use a file sharing service, eg dropbox or filedropper.com

Bye
 
Upvote 0
Re: VBA Help to Crop Image to 1:1 Aspect Ratio

Duplicate https://www.mrexcel.com/forum/excel...pping-image-1-1-aspect-ratio.html#post5031862

Please do not post the same question multiple times. All clarifications, follow-ups, and bumps should be posted back to the original thread.
Per forum rules, posts of a duplicate nature will be locked or deleted (rule 12 here: Forum Rules).

Note that sometimes posts from new users require Moderator approval before you can see them on the public forums. When this happens, you should see a message to that effect when you try to post it.
Please be patient and do not attempt to post the question again.
 
Upvote 0
Status
Not open for further replies.

Forum statistics

Threads
1,223,896
Messages
6,175,259
Members
452,626
Latest member
huntinghunter

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top