Log in

View Full Version : VBA code to read the dimension(Height and width) of a TIFF file



rickyfong
09-07-2013, 08:04 AM
I have located the whole net and only could find out some coding for C#, VBscrpit, VBproject etc but not any in VBA and could work. (In fact, I got one VBA coding but not working indeed). Please help!!

Aussiebear
09-09-2013, 12:14 AM
Please post the snippet of code you have found so we can begin to assist from there

rickyfong
09-09-2013, 01:35 AM
Here comes the coding ! Thanks!

Sub command1__Click()
Dim strFile As String 'filename
Dim strFileType As String 'TIF Mark
Dim iffsetIFD As Long ' first IFD deviation value
Dim iCountDE As Integer 'DE value
Dim lWidth As Long 'width of the TIF
Dim lHeight As Long 'Heigh of the TIF
Dim Temp1 As Integer, Temp2 As Long, i As Integer, k As Integer

strFile = "D:\1.tif"
Open strFile For Binary As #1
strFileType = String(2, 0)
Get #1, , strFileType ' get TIF mark
If strFileType <> "II" And strFileType <> "MM" Then Close #1: Exit Sub
Get #1, , Temp1 ' get version
Get #1, , iffsetIFD ' get first IFD deviation value
Seek #1, iffsetIFD + 1 ' set the next position as first IDF deviation value + 1
Get #1, , iCountDE ' get DE value from the first IDF
k = 0
For i = 1 To iCountDE
Get #1, , Temp1 ' get property marked number
If Temp1 = 256 Then ' if width
Get #1, , Temp1 'abandon
Get #1, , Temp2 'abandon
Get #1, , lWidth
k = k + 1
ElseIf Temp1 = 257 Then ' if height
Get #1, , Temp1 'abandon
Get #1, , Temp2 'abandon
Get #1, , lHeight
k = k + 1
Else ' else abandon those data
Get #1, , Temp1
Get #1, , Temp2
Get #1, , Temp2
End If
If k = 2 Then Exit For
Next i
Close #1
Debug.Print "Width:" & lWidth & vbCrLf & "Height:" & lHeight
End Sub

rollis13
09-09-2013, 02:53 AM
Works fine with Excel 2003:
Try using:
MsgBox "Width:" & lWidth & vbCrLf & "Height:" & lHeight
instead of:
'Debug.Print "Width:" & lWidth & vbCrLf & "Height:" & lHeight

rickyfong
09-09-2013, 03:55 AM
The problem seems not due to that, I have tried and the program exit when it firstly gone through this statement located at the 12th row of the function!!

If strFileType <> "II" And strFileType <> "MM" Then Close #1: Exit Sub

rickyfong
09-09-2013, 04:18 AM
Sorry! IT WORKS WELL now. I save the file in the wrong location! Thanks!!

my911
11-17-2013, 08:41 AM
Works fine with Excel 2003:
Try using:
MsgBox "Width:" & lWidth & vbCrLf & "Height:" & lHeight
instead of:
'Debug.Print "Width:" & lWidth & vbCrLf & "Height:" & lHeight

Hi,
I'm new.
I tried the code with MsgBox substitution.
The report was both Height and width = 0
But if I "hover" over the file name it reports dimensions = 4223x2885.

The test tif file is valid in the sense that Photoshop Elements will open and display it.

Is there an update to tif specs since this code was posted?
How else can I help you to help me?
Thanks

rollis13
11-17-2013, 04:39 PM
Still works great with Excel 2K3.

my911
11-17-2013, 06:42 PM
Still works great with Excel 2K3.
R,
Thanks for the reply.
It seems that the code executes on my W7 machine in Excel 2010.
Did I read somewhere that Tif tags/names/numbers depend on the OS?

Could that be the problem?
Anybody?

rollis13
11-18-2013, 04:03 PM
W7 (64bit) isn't the problem.

rollis13
11-19-2013, 08:50 AM
No problem with Excel 2K10, the macro works.

You can open the TIF file with NotePad and look at the first two characters, if they are II the macro will show the size, if they are MM the macro shows 0 x 0.
I think richyfrog's code needs some other amending.

my911
11-21-2013, 08:22 PM
No problem with Excel 2K10, the macro works.

You can open the TIF file with NotePad and look at the first two characters, if they are II the macro will show the size, if they are MM the macro shows 0 x 0.
I think richyfrog's code needs some other amending.

I tried several tif files. Unfortunately they all reported strFileType = MM
Here are typical results
strFile matches my selected file path/name
strFileType = MM
Version = 10752 ( whatever that means)
1stIFDDevValue = 134217728 (ditto)
iCountDE = 0 ( which jumps out

Apparently all who reported it works had a file with strFileType = II

Can anyone help with mods for a Type MM file?
Or point to a tutorial where I can learn about Version, 1stIFDDevValue,and iCountDE ?

PS.. Notepad shows it clearly........MM.....and Width and Height.

HELP?