Consulting

Results 1 to 12 of 12

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

  1. #1

    VBA code to read the dimension(Height and width) of a TIFF file

    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!!

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Please post the snippet of code you have found so we can begin to assist from there
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    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

  4. #4
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    Works fine with Excel 2003:
    Try using:
             MsgBox "Width:" & lWidth & vbCrLf & "Height:" & lHeight
    instead of:
             'Debug.Print "Width:" & lWidth & vbCrLf & "Height:" & lHeight

  5. #5
    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

  6. #6
    Sorry! IT WORKS WELL now. I save the file in the wrong location! Thanks!!

  7. #7
    VBAX Newbie
    Joined
    Nov 2013
    Location
    Beautiful Seabrook Island in SC
    Posts
    3
    Location

    Cool Help - Tif dimensions

    Quote Originally Posted by rollis13 View Post
    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

  8. #8
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    Still works great with Excel 2K3.

  9. #9
    VBAX Newbie
    Joined
    Nov 2013
    Location
    Beautiful Seabrook Island in SC
    Posts
    3
    Location
    Quote Originally Posted by rollis13 View Post
    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?

  10. #10
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    W7 (64bit) isn't the problem.

  11. #11
    VBAX Contributor rollis13's Avatar
    Joined
    Jun 2013
    Location
    Cordenons
    Posts
    146
    Location
    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.

  12. #12
    VBAX Newbie
    Joined
    Nov 2013
    Location
    Beautiful Seabrook Island in SC
    Posts
    3
    Location
    Quote Originally Posted by rollis13 View Post
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •