You could try this:
(
explaination below)
Code:
Private Sub Form_Load()
MsgBox Search_File("C:\YourFileToSearch.txt", "SearchText")
End Sub
Function Search_File(strFile As String, strSearch As String) As String
Dim strData As String
Open strFile For Input As #1
Do Until EOF(1)
Line Input #1, strData
If InStr(strData, strSearch) Then
Search_File = Right(strData, Len(strData) - InStr(strData, strSearch) - Len(strSearch) + 1)
Exit Function
End If
DoEvents
Loop
Search_File = "Not Found"
Close #1
End Function
What this does is it makes a function, which is probably best for this kind of thing. It uses
strFile to hold the path to the file,
strSearch for the string to search for,
strData holds the current line gathered from the file.
So when the function is called it opens the file path held in
strFile, and makes it so that it will input (other settings are output and append) as
#1, that is just a tag so you can refer to it.
It then sets a loop until
EOF (end of file), using 1 (the file we have opened). It then inputs a single line from the file #1 and puts that into
strData for later processing.
It then uses the
instr function to check if the search string is in the inputted line. The
instr function works as both a
boolean (to detect if a string is inside another) and an
integer (to give you the point at which the search string is in the input string provided).
Then the
Right function is used to grab all the characters from the end search string onwards. Unfortunately, I always have great difficulty trying to explain the Right function, but here goes.
The
Right function works by you supplying it a string to work with, and a position from which it will take. Take this for example:
We search for "
hello" in a text file that contains "
testahello=test". The purpose of the
right function is to get the right hand side of that, so we want the "
=test". We have to get the length of the entire thing, which is
15. We then work out the position of "
hello" in the string, which is
6 (
instr finds the position of the first character). Then it collects the length of the search string, takes that from the current position and plus one to that position.
That now returns everything after the "
hello".
I hope this is of help.