Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2008

Tip: Looking for answers? Try searching our database.

vba code to search in a txt document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
dorinatarnovan@googlemail.com - 27 Mar 2008 13:38 GMT
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
Karl E. Peterson - 27 Mar 2008 18:39 GMT
> I have to write some code in VBA, so that i can search a given string
> in a txt document.
[quoted text clipped - 3 lines]
>
> 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?
Signature

.NET: It's About Trust!
http://vfred.mvps.org

Karl E. Peterson - 27 Mar 2008 18:47 GMT
>> I have to write some code in VBA, so that i can search a given string
>> in a txt document.
[quoted text clipped - 6 lines]
> 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.
Signature

.NET: It's About Trust!
http://vfred.mvps.org

dorinatarnovan@googlemail.com - 28 Mar 2008 14:44 GMT
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?
Helmut Weber - 29 Mar 2008 13:35 GMT
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
Karl E. Peterson - 31 Mar 2008 20:34 GMT
> Hi
>
[quoted text clipped - 41 lines]
> 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

Signature

.NET: It's About Trust!
http://vfred.mvps.org

InAnyPlace - 28 Mar 2008 20:21 GMT
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
[quoted text clipped - 5 lines]
>
> Thank you

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.