Get Dimensions of Image File Outside Excel

L

Lazzaroni

Does anyone know how to retrieve the image size (width and height) in pixels
of an image that is not inside a spreadsheet?

When I hover my mouse pointer over images on my desktop the image size pops
up in Windows Vista, but I don't know how to retrieve this particular
attribute of the file from inside Excel 2007.

Thank you.
 
S

Steve Yandl

If you check out the link below, I have a script that uses the
"Shell.Application" object to retrieve the dimensions of various types of
picture files as part of the process. You will have to wade through the
other stuff it's doing but it should be easy enough to incorporate. The key
is going to be that you will first need to retrieve the complete file name
including the path (I use WScript arguments as the script is designed so the
user can drag the picture into the file and have it do its thing). I'm
still running WinXP and Excel 2003 so I won't presume to be able to share
exactly how you will need to approach file name retrieval.
http://www.microsoft.com/technet/scriptcenter/csc/scripts/media/graphics/cscme009.mspx


Steve Yandl
 
L

Lazzaroni

Steve:

That's what I was looking for. The FileSystemObject and the GetDetailsOf
method.

I had to change the GetDetailsOf method constant from 26 to 31 to return the
image dimensions. 26 returned nothing. I checked the FSO documentation but
could not find anything on this. Perhaps it is because I am using Windows
Vista.

Using GetDetailsOf constant 31 returns the image dimensions, but enclosed in
mystery characters that appear as question marks on my computer. That's the
reason for the additional string manipulation.

Perhaps someone can explain the change in the constant, or improve upon my
code.

Thank you for your help.

Function GetImageDimensions(ByVal strPath As String, Optional ByRef lngWidth
As Long, Optional ByRef lngHeight As Long) As Boolean

Dim objFSO As New FileSystemObject
Dim objShell As Object
Dim objFolder As Object
Dim varFileName As Variant
Dim strDimensions() As String

ReDim strDimensions(0)

Const filePropName = 0
Const filePropType = 2
Const filePropDimensions = 31 '26?

If objFSO.FileExists(strPath) Then

GetImageDimensions = True

Set objShell = CreateObject("Shell.Application")
Set objFolder =
objShell.Namespace(objFSO.GetParentFolderName(strPath))

For Each varFileName In objFolder.Items

If objFolder.GetDetailsOf(varFileName, filePropName) =
objFSO.GetFileName(strPath) Then
strDimensions(0) = objFolder.GetDetailsOf(varFileName,
filePropDimensions)
If InStr(strDimensions(0), " x ") > 0 Then
strDimensions = Split(strDimensions(0), "x")
lngWidth = CLng(Mid(Trim(strDimensions(0)), 2))
lngHeight = CLng(Mid(Trim(strDimensions(1)), 1,
Len(Trim(strDimensions(1))) - 1))
Exit For
Debug.Print lngWidth
Debug.Print lngHeight
End If
End If
Next

End If

Set objFSO = Nothing
Set objFolder = Nothing
Set objShell = Nothing

End Function
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top