Read A Binary File From Excel
In Excel it is possible to read a binary file to provide a method to look at the actual internals of any binary file, such as a Word document. When opened using the method illustrated below the user can view the contents of a file in Hexadecimal or the decimal equivalent of the ASCII internal characters.This example below scans the internals of a Word documents to locate and then copy a date and display it. Auto-update date fields in word can never be viewed with their previous values when the file is opened in Word. An "auto-update" date field always opens with today's date. I needed to see the previous date the file was saved. This example cycles through all the Word files in a given directory.
Program Code
Public Sub LoopReadBinaryFile() ' ********************************************************* ' This loops through the Word Files and Extracts the Date ' And The File Name ' ********************************************************* Dim strDirectoryPathToWordFiles As String Dim i As Long Dim j As Long Dim strArray(1 To 5) As String Dim strExtractedDate As String Dim strWordFileName As String Dim byteArr() As Byte Dim fileInt As Integer: fileInt = FreeFile strDirectoryPathToWordFiles = "C:\VIMFiles\" strWordFileName = Dir(strDirectoryPathToWordFiles, vbNormal) Do Until strWordFileName = "" Open strDirectoryPathToWordFiles & strWordFileName For Binary Access Read As #fileInt ReDim byteArr(0 To LOF(fileInt) - 1) Get #fileInt, , byteArr j = 0 For i = 1 To UBound(byteArr) If byteArr(i) > 64 And byteArr(i) < 91 Then strArray(1) = Chr(byteArr(i)) strArray(2) = Chr(byteArr(i + 1)) strArray(3) = Chr(byteArr(i + 2)) strArray(4) = Chr(byteArr(i + 3)) strArray(5) = Chr(byteArr(i + 4)) If strArray(1) = "M" And _ strArray(2) = "E" And _ strArray(3) = "R" And _ strArray(4) = "G" And _ strArray(5) = "E" Then strExtractedDate = "" j = i Do Until j > i + 30 If byteArr(j) > 46 And byteArr(j) < 58 Then strExtractedDate = strExtractedDate & Chr(byteArr(j)) End If j = j + 1 Loop End If End If Next i MsgBox ("File " & strWordFileName & " Is Dated " & strExtractedDate) Close #fileInt strWordFileName = Dir Loop End Sub