Getting image size in VBA?
Question
In a word document that I've been building some VBA code for, I need to be able to display images on a userform, and also resize the userform to fit whatever image it's presenting.
I was very confused when I tried something like this in the Immediate Box (the image file is actually only 675 pixels in width):
? LoadPicture("C:\Users\arose\Desktop\Security Control Doc\Images\AC1.bmp").Width
17859
Eventually I discovered that this is due to VBA using some unit other than pixels. Twips, or something like that. But I really need to have image sizes in pixels, so I mucked around, found roughly what the conversion factor is, and I'm using this:
Function TwipsToPixels(Twips As Long) As Long
TwipsToPixels = Twips / 25.477
End Function
This has helped, but it is not particularly accurate. So my question is, does anyone have a more elegant and precise way of getting image sizes in VBA?
Solution
The default unit of measurement in VBA is twips: screen-independent unit of measurement that is equal to 1/20 of a point, or 1/1,440 of an inch. There are 567 twips in a centimeter.
Pixels are screen dependent units, so the two functions/properties TwipsPerPixelX
and TwipsPerPixelY
should do the trick.