And here is the code.
Sub ExtractMessage_Click()
Dim wkst_home As Worksheet
Dim FSO As FileSystemObject
Dim Rs As Recordset
Dim MC As Connection
Dim blnFlag_NotFound As Boolean
Set MC = New ADODB.Connection
Set Rs = New Recordset
Set wkst_home = ThisWorkbook.Worksheets("Home")
Set FSO = CreateObject("Scripting.FileSystemObject")
Myworkbook = Application.ThisWorkbook.FullName
MC.Open "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & Myworkbook & ";" & _
"Extended Properties=Excel 12.0;" & _
"Persist Security Info=False"
If wkst_home.ListObjects.Count = 0 Then
Exit Sub
End If
With wkst_home.ListObjects(1)
Set oLo = wkst_home.ListObjects(1)
arrAddress = Split(Trim(oLo.Range.Address), "$")
straddress = arrAddress(1) & arrAddress(2) & arrAddress(3) & arrAddress(4)
rngtestcasenum = "Home" & "$" & straddress
strSQL = "Select * from [" & rngtestcasenum & "]"
End With
Rs.Open strSQL, MC, adOpenStatic, adUseClient
' this will allow the user to select which text file to search
Call Browse
gff_location = objfiledirectory
arrFil = Split(gff_location, "\")
sFileName = arrFil(UBound(arrFil))
Application.ScreenUpdating = False
If FSO.FileExists(gff_location) Then
If LCase(Right(sFileName, 4)) = ".txt" Then
blnFlag_NotFound = False
With Rs
Do While Not (.EOF)
i = 1
Worksheets.Add(after:=Worksheets("Home")).Name = "Extracted Messages"
Set wkst_Out = ThisWorkbook.Worksheets("Extracted Messages")
' will write all the values of a particular row into one string
' if there be any empty values in the table then those will not be read
' this string is then made into an array
strColVals = ""
For j = 0 To intTblFlds - 1
If IsNull(Rs.Fields(j).Value) = False Then
If strColVals = "" Then
strColVals = Rs.Fields(j).Value
Else
strColVals = strColVals & ";" & Rs.Fields(j).Value
End If
End If
Next
arrstrColVals = Split(Trim(strColVals), ";")
Set ts = FSO.OpenTextFile(gff_location)
Do While Not ts.AtEndOfStream
strLine = ts.ReadLine
If strLine = "" Then
If Not ts.AtEndOfStream Then
Do
If Not ts.AtEndOfStream Then
strLine = ts.ReadLine
Else
Exit Sub
End If
Loop Until strLine <> ""
End If
End If
blnFlag_NotFound = False
If InStr(1, Trim(strLine), "{") > 0 Then
Sum = Sum + 1
wkst_Out.Cells(5, i).Value = strLine
Do
If Not ts.AtEndOfStream Then
a = ts.ReadLine
wkst_Out.Cells(5, i).Value = wkst_Out.Cells(5, i).Value & Chr(10) & a
End If
Loop Until a = "-}"
k = 0
Do
If InStr(1, Trim(wkst_Out.Cells(5, i).Value), Trim(arrstrColVals(k))) < 1 Then
intInvalidCount = intInvalidCount + 1
wkst_Out.Cells(5, i).Value = ""
blnFlag_NotFound = True
Exit Do
End If
k = k + 1
Loop Until UBound(arrstrColVals) < k ' - 1
If blnFlag_NotFound = False Then
intValidCount = intValidCount + 1
i = i + 1
End If
Else
Do
If Not ts.AtEndOfStream Then
a = ts.ReadLine
End If
Loop Until a = "-}"
End If
Loop
Rs.MoveNext
Application.ScreenUpdating = True
End If
Loop
Rs.Close
End With
Else
MsgBox "Please Select a Text File to Extract"
End If
End If
MsgBox "Message Extraction Operation completed successfully."
MC.Close
End Sub
I hope now its a bit more clearer...
Bookmarks