Whats the best way to get a list of words from a text file, each on a seperate line, and then searching a string for them?
Thanx
Printable View
Whats the best way to get a list of words from a text file, each on a seperate line, and then searching a string for them?
Thanx
I'm very busy right now, but I'll try to provide a sample code which loads the contents of a *.txt and then searches for a specific string.
Quintonir
VB Code:
Dim strTemp$, StringToLookIn$ Dim arrTemp() Open "C:\text.txt" For Input As #1 strTemp = Input(LOF(1),1) Close #1 StringToLookIn = Text1.Text arrTemp = Split(strTemp,vbCrLf) For x = 0 To UBound(arrTemp) If InStr(1,StringToLookIn,arrTemp(x)) <> 0 Then MsgBox arrTemp(x) & " found in string at least once." End If Next x
is that kind of what you are looking for?
The code looks fine, but it produces an error when running the line:
The error is:Code:arrTemp = Split(strTemp,vbCrLf)
"Compile Error: array or user-defined type expected"
Any ideas?
not sure why...try changing "Dim arrTemp()" to "Dim arrTemp As Variant"
wait, no that's not it. You are using VB5, and I didn't realize that. There is no Split function in version 5. give me a minute and I will find the code that simulates the Split function
:)
Could the problem be that I'm not using the VB6 split function, as I'm on VB5, and am using this instead:
Code:Sub Split2(ByRef arr() As String, ByVal strInput As String, ByVal strDelimit As String)
Dim i, tmp
ReDim arr(0)
For i = 1 To Len(strInput)
tmp = Mid(strInput, i, 1)
If tmp = strDelimit Then
ReDim Preserve arr(UBound(arr) + 1)
arr(UBound(arr)) = ""
Else
arr(UBound(arr)) = arr(UBound(arr)) & tmp
End If
Next i
End Sub
VB Code:
Private Function Split2(ByVal sString As String, ByVal sSeparator As String) As Variant 'Author: Aaron Young 'Origin: - 'Purpose: Split Function 'Version: VB4+ Dim sParts() As String Dim lParts As Long Dim lPos As Long lPos = InStr(sString, sSeparator) While lPos ReDim Preserve sParts(lParts) sParts(lParts) = Left(sString, lPos - 1) sString = Mid(sString, lPos + Len(sSeparator)) lPos = InStr(sString, sSeparator) lParts = lParts + 1 Wend If Len(sString) Then ReDim Preserve sParts(lParts) sParts(lParts) = sString End If Split2 = IIf(lParts, sParts, Array()) End Function Private Sub Command1_Click() Dim vArray As Variant Dim sTemp As String, sTemp2 As String Dim strText As String Open "C:\Test.txt" For Input As #1 strText = Input(LOF(1), 1) Close #1 vArray = Split2(strText, vbCrLf) sTemp = vArray(0) sTemp2 = vArray(1) End Sub
its from Aaron Young, and it works ;)
Cool! :)
That works (well, if I use the first Command1_Click code that you posted instead of that last one :D )
Thanx
Or you can just use the InStr function.
VB Code:
Open "C:\text.txt" For Input As #1 strTemp = Input(LOF(1),1) Close #1 If InStr(1, strTemp, "MyWord") <> 0 Then MsgBox "String found"
How did you guys get the code to say "visual basic code" and the colouring instead of just "code"?
use [ vbcode ][ /vbcode ] (without the spaces) instead of [ code ][ /code ] (without the spaces)Quote:
Originally posted by adamcox
How did you guys get the code to say "visual basic code" and the colouring instead of just "code"?
I haven't fully tested this code, so if it runs in error or doesn't reach its goal, please notice me.
I know the function can be shortened. The function does much more than needed, but that's to show you some things. It loads the contents of the file into an array, and then scans the array for matches, listing the line numbers in another array.
The function:
Usage:Code:Private Function SearchFile(ByRef FilePath As String, ByRef SearchString As String, ByRef WholeStory As String) As Variant
Dim sLineStr() As String
Dim lFndLine() As Long
Dim iFFile As Integer
Dim lNumLine As Long
Dim lCount As Long
Dim lNumFnd As Long
Dim sWholeStory As String
On Error GoTo erSrcFile
If Dir(FilePath) = "" Then GoTo erSrcFile
iFFile = FreeFile
Open FilePath For Input As #iFFile
Do While Not EOF(iFFile)
lNumLine = lNumLine + 1
ReDim Preserve sLineStr(1 To lNumLine)
Line Input #iFFile, sLineStr(lNumLine)
Loop
Close #iFFile
For lCount = 1 To lNumLine
If InStr(sLineStr(lCount), SearchString) > 0 Then
lNumFnd = lNumFnd + 1
ReDim Preserve lFndLine(1 To lNumFnd)
lFndLine(lNumFnd) = lCount
End If
sWholeStory = sWholeStory & sLineStr(lCount) & vbNewLine
Next
sWholeStory = Left(sWholeStory, Len(sWholeStory) - Len(vbNewLine))
WholeStory = sWholeStory
SearchFile = lFndLine
Exit Function
erSrcFile:
SearchFile = Null
End Function
The code isn't very usefull, and again takes a long road to reach its goal. But in this way you can see how it is done. And I hope it solves your problem.Code:Private Sub Test()
Dim sFilePath As String
Dim sSearchStr As String
Dim lLineStr As Variant
Dim sWholeStory As String
sFilePath = "C:\Addresses.txt"
sSearchStr = "Avenue"
lLineStr = SearchFile(sFilePath, sSearchStr, sWholeStory)
If VarType(sLineStr) = 1 Then MsgBox "An error occured!", vbExclamation, "Error": Exit Sub
'lLineStr now contains an array with the lines containing sSearchStr
'sWholeStory now contains the complete content of sFilePath
End Sub
Quintonir