|
|
|
date: Thu, 27 Mar 2008 05:38:03 -0700 (PDT),
group: microsoft.public.word.vba.beginners
back
Re: vba code to search in a txt document
Karl E. Peterson wrote:
> dorinatarnovan@googlemail.com wrote:
>> 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?
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.
--
.NET: It's About Trust!
http://vfred.mvps.org
date: Thu, 27 Mar 2008 10:47:45 -0700
author: Karl E. Peterson
Re: vba code to search in a txt document
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?
date: Fri, 28 Mar 2008 06:44:15 -0700 (PDT)
author: unknown
Re: vba code to search in a txt document
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
date: Sat, 29 Mar 2008 13:35:49 +0100
author: Helmut Weber
Re: vba code to search in a txt document
dorinatarnovan@googlemail.com wrote:
> 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
--
.NET: It's About Trust!
http://vfred.mvps.org
date: Mon, 31 Mar 2008 12:34:48 -0700
author: Karl E. Peterson
|
|