Solved

Get resolution of graphic file using VBA

Posted on 2014-03-04
5
2,244 Views
Last Modified: 2014-03-10
I want VBA (Visual Basic for applications) within MS Access to get the resolution of a graphic file (i.e., png, gif, bmp, jpeg). By the way, I need the width and height of the graphic because I am generating html code that rescales the graphic to the appropriate size.

Is there a VBA function or windows call that returns this information?
0
Comment
Question by:gordonwwaugh
  • 3
  • 2
5 Comments
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 500 total points
ID: 39905428
Here is a code snippet that you should be able to customize to get the extended property of a file.  Of course, you need to change the Namespace to your directory and your file name to the image file you are looking for.
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace("c:\users\mark\downloads")
set objFile = objFolder.Items.Item("image001 (2).png")
Debug.Print objFile.ExtendedProperty("Dimensions")

Open in new window

0
 

Author Comment

by:gordonwwaugh
ID: 39907924
It almost worked. I had to replace "Items.Item" with "ParseName".

However, something weird is happening. It fails if I replace the "c:\users\mark\downloads" with a string variable name. For example, this doesn't work:

strFoldername = "c:\users\mark\downloads"
set objFolder = objShell.NameSpace(strFoldername)

When I use this code, I get the following error on the "set objFile = ..." line: "Object variable not set." I have verified that the strFoldername value is correct.

Any ideas? Surely I am allowed to use a variable name in the "set objFolder" statement rather than a quoted string.
0
 

Accepted Solution

by:
gordonwwaugh earned 0 total points
ID: 39907992
Okay. I figured out the reason for the error. I had declared strFoldername as a string variable but NameSpace is a variant value. It works now that I am storing the folder name in variant variable varFoldername.

Here is the final code that works:

Private Sub GetGraphicDimensions()

    Dim lngImageWidth As Long
    Dim lngImageHeight As Long
    Dim strFilePath As String
    Dim objShell As Object
    Dim objFolder As Object
    Dim objFile As Object
    Dim strFilename As String
    Dim varFoldername As Variant
    Dim strImageDimensions As String
    
    strFilePath = "C:\Documents and Settings\graphicFile.png"
        
    '------ Use my custom functions to parse the path into folder and filename -----
    strFilename = FileNameOnly(strFilePath)
    varFoldername = PathNameOnly(strFilePath)
    
    Set objShell = CreateObject("Shell.Application")
    Set objFolder = objShell.NameSpace(varFoldername)
    Set objFile = objFolder.ParseName(strFilename)
    
    strImageDimensions = objFile.ExtendedProperty("Dimensions")
                                            
    lngImageWidth = CLng(Left(strImageDimensions, InStr(strImageDimensions, "x") - 2))
    lngImageHeight = CLng(Mid(strImageDimensions, InStr(strImageDimensions, "x") + 2, 99))
      
    Debug.Print "width = " & lngImageWidth & ", height = " & lngImageHeight

End Sub

Open in new window

0
 
LVL 45

Expert Comment

by:aikimark
ID: 39908169
The expert's solution has one error.
I wouldn't assert that my original code post had errors, since it worked for me.  Like you, I didn't know that the Namespace() parameter had to be a Variant data type.  The string literal I used was being automatically cast by the compiler/interpreter.  When working to quickly answer EE questions, I'm often in the Immediate Window where all dynamic variables are variant.
0
 

Author Closing Comment

by:gordonwwaugh
ID: 39917027
The expert's solution has one error. My solution fixes the error and adds additional supporting code such as variable declarations.
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question