Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
date: Thu, 27 Mar 2008 05:38:03 -0700 (PDT),    group: microsoft.public.word.vba.beginners        back       


vba code to search in a txt document   
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
date: Thu, 27 Mar 2008 05:38:03 -0700 (PDT)   author:   unknown

Re: vba code to search in a txt document   
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?
-- 
.NET: It's About Trust!
 http://vfred.mvps.org
date: Thu, 27 Mar 2008 10:39:20 -0700   author:   Karl E. Peterson

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   
Access my profile Details in:

                  http://al-bernardes.sites.uol.com.br/
                  http://www.linkedin.com/in/andrebernardes
                  http://andrebernardes.myplaxo.com/

André Luiz Bernardes

On 27 mar, 09:38, dorinatarno...@googlemail.com wrote:
> Hi!
> I have to write some code inVBA, 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 inVBA.
>
> If you have some usefull ideas on how to do this, please answer.
>
> Thank you
date: Fri, 28 Mar 2008 12:21:35 -0700 (PDT)   author:   InAnyPlace

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

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us