vba code to search in a txt document

D

dorinatarnovan

Hi!
I have to write some code in VBA, so that i can search a given string
in a txt document.
If I find that string in the txt document, I have to take the line of
code in which I found the searched string and put it in a variable in
VBA.

If you have some usefull ideas on how to do this, please answer.

Thank you
 
K

Karl E. Peterson

I have to write some code in VBA, so that i can search a given string
in a txt document.
If I find that string in the txt document, I have to take the line of
code in which I found the searched string and put it in a variable in
VBA.

If you have some usefull ideas on how to do this, please answer.

Read the file into a string, break the string into lines, scan through the lines
looking for your substring. What part(s) are proving difficult?
 
K

Karl E. Peterson

Karl said:
Read the file into a string, break the string into lines, scan through the lines
looking for your substring. What part(s) are proving difficult?

Just realized this was the "beginners" group. Okay, assuming the file isn't
gargantuan, read it in its entirety from file into a string, use this:

Public Function ReadFile(ByVal FileName As String) As String
Dim hFile As Long
On Error GoTo Hell
hFile = FreeFile
Open FileName For Binary As #hFile
ReadFile = Space$(LOF(hFile))
Get #hFile, , ReadFile
Close #hFile
Hell:
End Function

You could, for example, create an array of "lines" something like this:

Dim Lines() As String
Lines = Split$(ReadFile(TheFile$), vbCrLf)

Then just loop through the array, using Instr() to search for your desired
substring.
 
D

dorinatarnovan

Hi

I've used that function:

Public Function ReadFile(ByVal FileName As String) As String
Dim hFile As Long
On Error GoTo CodeErr
hFile = FreeFile
Open FileName For Binary As #hFile
ReadFile = Space$(LOF(hFile))
Get #hFile, , ReadFile
Close #hFile
CodeErr:
End Function

and I've created the array of "lines":
Dim TheLines() As String
TheLines = Split(ReadFile(strIniFile), vbCrLf)

But I have a problem with the InStr function.

If InStr(1, TheLines(i), sSignal, vbTextCompare) = 1 Then
ThisLine = Split(Trim$(TheLines(i)), " ")
sSignalFound = ThisLine(z)
sFinalCode = ThisLine(z + 1)
Else
MsgBox ("Signal not found in the txt document")
End If


The problem is with the following code:
If InStr(1, TheLines(i), sSignal, vbTextCompare) = 1
Then 'doesn't enter here
ThisLine = Split(Trim$(TheLines(i)), "
")

sSignalFound = ThisLine(z)
sFinalCode = "(" & ThisLine(z + 1) & " " &
ThisLine(z + 2) & " " & ThisLine(z + 3) & ")"
Else
MsgBox ("Signal not found in the txt document")
End If

It doesn't like "i", and if I put a number instead, for example 1, it
will work, but only for that array.
What condition should I put for i?
 
H

Helmut Weber

Hi,

not
If InStr(1, TheLines(i), sSignal, vbTextCompare) = 1 Then but
If InStr(1, TheLines(i), sSignal, vbTextCompare) > 0 Then

Don't know what the rest of the code is supposed to do.

--

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Vista Small Business, Office XP
 
K

Karl E. Peterson

Hi

I've used that function:

Public Function ReadFile(ByVal FileName As String) As String
Dim hFile As Long
On Error GoTo CodeErr
hFile = FreeFile
Open FileName For Binary As #hFile
ReadFile = Space$(LOF(hFile))
Get #hFile, , ReadFile
Close #hFile
CodeErr:
End Function

and I've created the array of "lines":
Dim TheLines() As String
TheLines = Split(ReadFile(strIniFile), vbCrLf)

But I have a problem with the InStr function.

If InStr(1, TheLines(i), sSignal, vbTextCompare) = 1 Then
ThisLine = Split(Trim$(TheLines(i)), " ")
sSignalFound = ThisLine(z)
sFinalCode = ThisLine(z + 1)
Else
MsgBox ("Signal not found in the txt document")
End If


The problem is with the following code:
If InStr(1, TheLines(i), sSignal, vbTextCompare) = 1
Then 'doesn't enter here
ThisLine = Split(Trim$(TheLines(i)), "
")

sSignalFound = ThisLine(z)
sFinalCode = "(" & ThisLine(z + 1) & " " &
ThisLine(z + 2) & " " & ThisLine(z + 3) & ")"
Else
MsgBox ("Signal not found in the txt document")
End If

It doesn't like "i", and if I put a number instead, for example 1, it
will work, but only for that array.
What condition should I put for i?

You'd want to use a For-Next loop for i...

For i = LBound(TheLines) To UBound(TheLines)
If InStr(1, TheLines(i), sSignal, vbTextCompare) = 1
' etc...
End If
Next i
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top