Solved

Get resolution of graphic file using VBA

Posted on 2014-03-04
5
2,307 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

Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

830 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