Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get resolution of graphic file using VBA

Posted on 2014-03-04
5
Medium Priority
?
2,852 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 46

Assisted Solution

by:aikimark
aikimark earned 2000 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 46

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you’re using QODBC to update QuickBooks data from Microsoft® Access but Access is not showing the updated data, you could have set up QODBC incorrectly.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

972 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