Solved

Get resolution of graphic file using VBA

Posted on 2014-03-04
5
2,432 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
 
LVL 1

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
 
LVL 1

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
 
LVL 1

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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

739 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