Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Getting recognition of file type for Office 2007 documen
Message
From
15/06/2012 18:04:21
 
 
To
15/06/2012 16:10:57
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01546122
Message ID:
01546132
Views:
40
I have enhanced my FileType method to take care of the Office 2007 recognition for specific files such as Word, Excel and PowerPoint. This is the code. Some of it contains some framework logic. However, the flow chart should be pretty easy to understand. If anyone has a better approach to resolve the issue, let me know.
        ' Return the file type
        ' expC1 File name with the full path
        Public Function FileType(ByVal tcFile As String) As String
            Dim lcDirectory As String = ""
            Dim lcFile As String = ""
            Dim lcString As String = ""
            Dim lcValue As String = ""
            Dim lnLocation As Integer = 0
            Dim lnLocation2 As Integer = 0
            Dim loDirectoryFunction As Framework.DirectoryFunction = Nothing
            Dim loFileDirectory As Framework.FileDirectory = Nothing
            Dim loFileFunction As Framework.FileFunction = Nothing
            Dim loXml As Framework.XML = Nothing
            Dim loZip As Framework.Zip = Nothing

            ' Get the proper definition as per the current scope
            If oProcess Is Nothing Then
                loDirectoryFunction = New Framework.DirectoryFunction(oApp)
                loFileDirectory = New Framework.FileDirectory(oApp)
                loFileFunction = New Framework.FileFunction(oApp)
                loXml = New Framework.XML(oApp)
                loZip = New Framework.Zip(oApp)
            Else
                loDirectoryFunction = New Framework.DirectoryFunction(oProcess)
                loFileDirectory = New Framework.FileDirectory(oProcess)
                loFileFunction = New Framework.FileFunction(oProcess)
                loXml = New Framework.XML(oProcess)
                loZip = New Framework.Zip(oProcess)
            End If

            ' Read it as a binary
            lcString = FileToStrBinary(tcFile)

            ' If we have less than four characters
            If lcString.Length < 4 Then
                Return ""
            End If

            ' If this is a JPG
            If Left(lcString, 3) = Chr(255) + Chr(216) + Chr(255) Then
                Return "JPG"
            End If

            ' If this is a GIF
            If Left(lcString, 3) = "GIF" Then
                Return "GIF"
            End If

            ' If this is a EMF
            If Mid(lcString, 42, 3) = "EMF" Then
                Return "EMF"
            End If

            ' If this is a WMF
            If Left(lcString, 4) = Chr(215) + Chr(205) + Chr(198) + Chr(154) Then
                Return "WMF"
            End If

            ' If this is a TIF
            If Left(lcString, 4) = Chr(77) + Chr(77) + Chr(0) + Chr(42) Then
                Return "TIF"
            End If

            ' If this is a PNG
            If Left(lcString, 4) = Chr(137) + "PNG" Then
                Return "PNG"
            End If

            ' If this is a BMP
            If Left(lcString, 2) = "BM" Then
                Return "BMP"
            End If

            ' If this is a SWF
            If Left(lcString, 3) = "CWS" And Asc(Mid(lcString, 4, 1)) < 16 Then
                Return "SWF"
            End If

            ' If this is a SWF
            If Left(lcString, 3) = "FWS" And Asc(Mid(lcString, 4, 1)) < 16 Then
                Return "SWF"
            End If

            ' If this is a XML
            If UCase(Left(lcString, 6)) = "<?XML " Then
                Return "XML"
            End If

            ' If this is a DOC
            If Left(lcString, 8) = Chr(208) + Chr(207) + Chr(17) + Chr(224) + Chr(161) + Chr(177) + Chr(26) + Chr(225) Then
                Return "DOC"
            End If

            ' If this is a PDF
            If UCase(Left(lcString, 5)) = "%PDF-" Then
                Return "PDF"
            End If

            ' If this is a Excel
            If Left(lcString, 8) = Chr(208) + Chr(207) + Chr(17) + Chr(224) + Chr(161) + Chr(177) + Chr(26) + Chr(225) Then
                Return "XLS"
            End If

            ' If this is a Microsoft Office 2007 document
            If Left(lcString, 14) = Chr(80) + Chr(75) + Chr(3) + Chr(4) + Chr(20) + Chr(0) + Chr(6) + Chr(0) + _
             Chr(8) + Chr(0) + Chr(0) + Chr(0) + Chr(33) + Chr(0) Then

                ' If we have a temporary directory
                If oApp.cTempDir.Length > 0 Then

                    ' Get a temporary file name
                    lcDirectory = oApp.GenerateFileName()

                    ' Get the full long directory name
                    lcDirectory = oApp.cTempDir + lcDirectory

                    ' If we can create a temporary directory under Temp
                    If oApp.CreateDirectory(lcDirectory) Then

                        ' Zip class needs an extension, otherwise, it will try to zip a file name ending with .zip
                        loFileFunction.cSource = tcFile
                        loFileFunction.cDestination = tcFile + ".zip"
                        loFileFunction.lLogError = False
                        If loFileFunction.CopyFile() Then

                            ' If we can unzip the file
                            loZip.cFile = tcFile + ".zip"
                            loZip.cDirectory = lcDirectory
                            loZip.lUseCommandPrompt = True
                            If loZip.Unzip() Then

                                ' Delete the temporary file
                                If Not loFileFunction.DeleteFile(tcFile + ".zip", False) Then
                                End If

                                ' If we can load the Xml
                                If loXml.LoadXmlFromFile(lcDirectory + "\[Content_Types].xml") Then

                                    ' Delete the temporary files which were created in the temporary directory
                                    loFileDirectory.cDirectory = lcDirectory
                                    loFileDirectory.GetFile()
                                    If Not loFileDirectory.DeleteFiles() Then
                                    End If

                                    ' Delete the temporary directory
                                    If Not loDirectoryFunction.DeleteDirectory(lcDirectory) Then
                                    End If

                                    ' If we can add the namespace
                                    If loXml.AddNamespace("http://schemas.openxmlformats.org/package/2006/content-types", "ns") Then

                                        ' If the attribute exists
                                        If loXml.IsXMLNodeAttribute("//ns:Types/ns:Override[1]", "PartName") Then

                                            ' Get the value of the attribute
                                            If loXml.GetXMLNodeAttributeValue("//ns:Types/ns:Override[1]", "PartName") Then
                                                lcValue = loXml.cNodeAttribute

                                                ' Based on the type
                                                Select Case lcValue

                                                    ' Word
                                                    Case "/word/document.xml"
                                                        Return "DOCX"

                                                        ' Excel
                                                    Case "/xl/workbook.xml"
                                                        Return "XLSX"

                                                        ' PowerPoint
                                                    Case "/ppt/presentation.xml"
                                                        Return "PPTX"

                                                    Case Else
                                                        Return ""

                                                End Select

                                            Else
                                                Return ""
                                            End If

                                        Else
                                            Return ""
                                        End If

                                    Else
                                        Return ""
                                    End If

                                Else
                                    Return ""
                                End If

                            Else
                                Return ""
                            End If

                        Else
                            Return ""
                        End If

                    Else
                        Return ""
                    End If

                Else
                    Return ""
                End If

                Return ""
            End If

            ' If this is a ZIP
            If Left(lcString, 4) = "PK" + Chr(3) + Chr(4) Then
                Return "ZIP"
            End If

            Return ""
        End Function
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform