Results 1 to 4 of 4
  1. #1
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code: Function strTypeOfDocument(strFile As String

    Here is a skeleton for a function that returns the type of a file. The little TEST procedure serves as a shell.

    The skeleton is just that; it serves to show how you might write such a function with full criteria.


    <pre>Public Function strTypeOfDocument(strFile As String) As String
    ' Procedure : cmd_TypeOfDocument
    ' Description: Determine what sort of document we have.
    ' Copyright: Chris Greaves Inc.
    ' Inputs: File name.
    ' Returns: STRING.
    ' Assumes: File exists, is of required minium length.
    ' Side Effects: None.
    ' Tested: By a call from the user.

    Dim intFile As Integer
    intFile = FreeFile
    Open strFile For Binary As intFile
    If boolChar(intFile, "WPC", 1) Then
    strTypeOfDocument = "WPC" ' matches on 3 known criteria
    ElseIf boolChar(intFile, "GIF", 0) Then
    strTypeOfDocument = "GIF"
    ElseIf boolHex(intFile, "D0CF11E0A1B11AE1", 0) Then
    If boolHex(intFile, "FEFFFFFF", "3C") Then
    strTypeOfDocument = "Excel"
    Else
    End If
    ElseIf boolChar(intFile, "[ver]", 0) Then
    strTypeOfDocument = "AmiPro"
    ElseIf boolChar(intFile, "PK", 0) Then
    strTypeOfDocument = "PKZip"
    Else
    strTypeOfDocument = strFile & " ; Unknown type"
    End If
    Close intFile
    End Function


    Sub TESTstrTypeOfDocument()
    Dim strName As String ' Retain filename/extent from dialogue box.
    With Dialogs(wdDialogFileOpen)
    .Name = "*.*" ' Specifically interested in templates, since we're opeing VBE
    If .Display = -1 Then
    strName = .Name ' Grab the Name; user will have navigated to DefaultFilePath.
    MsgBox strTypeOfDocument(strName)
    Else
    End If
    End With
    End Sub
    </pre>


  2. #2
    2 Star Lounger
    Join Date
    Mar 2001
    Location
    Oregon, USA
    Posts
    129
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strTypeOfDocument(strFile As String

    Hi Chris,

    Rather than hard coding file types, what about returning them from the registry? For example:

    Dim strTest As String
    Dim strResult As String

    strTest = "whatever.zip" 'run whatever you need to return the file name

    strTest = Right(strTest, 4)
    strTest = "HKEY_CLASSES_ROOT" & strTest
    strResult = System.PrivateProfileString("", strTest, "")
    MsgBox "strResult"

    My humble <img src=/S/2cents.gif border=0 alt=2cents width=15 height=15>.

    Regards,

    Chris

  3. #3
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strTypeOfDocument(strFile As String

    Too humble by far! Your comment is valid, and I have yet ANOTHER chunk of code that obtains converter types/names from somewhere (I didn't know it was the registry).

    The types in the registry may not account for all the types I have on my system.

    My main consideration in showing literal string-types in the sample code was to simplify the code to show the mechanism of determining the type of file, and THAT code was a vehicle for showing a use of the get-hex-from-file code.

  4. #4
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Yilgarn region of Toronto, Ontario
    Posts
    5,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Code: Function strTypeOfDocument(strFile As String

    > Rather than hard coding file types, what about returning them from the registry?


    I found my code to list all converters (on MY computer, but not on yours). I realise now that it's the code that gave me a problem with On Error a few days back.

    If I am correct, the registry uses file extents to associate file types. This has to fail when we are faced with WP51 DOS files, because those users commonly employed all 8+3 characters of a DOS filename, so the extent became meaningless. That was the thrust behind my efforts to determine a file type by looking at it.

    Now I'm sure that many desktop applications do this; Word checks the files for those it might understand. I wanted a more general method, so that when a client says "Find all my Act! files and do THIS with them", I could work out the Act! signature, drop it into a table, and let my macro do the work.

    Consider too a malicious Word virus that disguised its host template by giving it a weird extent. Windows wouldnt find it, but I would.





    <pre>Sub ListAllFormatConverters()

    ''OpenFormat|Name|FormatName|ClassName|Extensions
    ''6|WWPAB.CNV|Schedule+ Contacts|SPLUS|scd
    ''7|WWPAB.CNV|Personal Address Book|MSPAB|pab
    ''8|WWPAB.CNV|Outlook Address Book|OUTLOOK|olk
    ''9|TXTLYT32.CNV|MS-DOS Text with Layout|MS-DOS Text with Layout|asc
    ''10|TXTLYT32.CNV|Text with Layout|Text with Layout|ans
    ''11|html32.cnv|All Word Documents|WordFormats|DOC DOT HTM* URL RTF
    ''21|html32.cnv|HTML Document|HTML|html htm htx asp
    ''12|mswrd632.cnv|Word (Asian Versions) 6.0/95|MSWordJ6|doc dot
    ''None|WNWRD232.CNV|Word 2.x for Windows|MSWordWin2|doc
    ''13|MACWRD32.CNV|Word 4.0 - 5.1 for Macintosh|MSWordMac|mcw
    ''None|MACWRD32.CNV|Word 4.0 for Macintosh|MSWordMac4|mcw
    ''None|MACWRD32.CNV|Word 5.0 for Macintosh|MSWordMac5|mcw
    ''None|MACWRD32.CNV|Word 5.1 for Macintosh|MSWordMac51|mcw
    ''14|recovr32.cnv|Recover Text from Any File|Recover|*
    ''15|LOTUS32.CNV|Lotus 1-2-3|Lotus123|wk1 wk3 wk4
    ''16|WPFT532.CNV|WordPerfect 5.x|WrdPrfctDOS|doc
    ''None|WPFT532.CNV|WordPerfect 5.0|WrdPrfctDOS50|doc
    ''None|WPFT532.CNV|WordPerfect 5.1 for DOS|WrdPrfctDOS51|doc
    ''None|WPFT532.CNV|WordPerfect 5.x for Windows|WrdPrfctWin|doc
    ''None|WPFT532.CNV|WordPerfect 5.1 or 5.2 Secondary File|WrdPrfctDat|doc
    ''None|WPFT532.CNV|WordPerfect 5.0 Secondary File|WrdPrfctDat50|doc
    ''17|WPFT632.CNV|WordPerfect 6.x|WordPerfect6x|doc wpd
    ''18|EXCEL32.CNV|Microsoft Excel Worksheet|MSBiff|xls xlw
    ''19|works332.cnv|Works ||




    Dim fc
    Open "d:temperase.txt" For Output As 1
    For Each fc In FileConverters
    On Error GoTo Dummy
    Debug.Print fc.OpenFormat; fc.Name; fc.FormatName; fc.ClassName; fc.Extensions
    GoTo NextFc
    Dummy:
    Debug.Print "No registered converter supports reading files in this format."_
    ; fc.Name; fc.FormatName; fc.ClassName; fc.Extensions
    ' On Error Resume Next
    Resume Next
    NextFc:
    Next fc
    On Error GoTo 0

    End Sub
    </pre>


Posting Permissions

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