Solved

Get resolution of graphic file using VBA

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now