Hi. I would like to know how to count the number of occurances of a word in a text file.
I am using Microsoft Access VBA.
Any ideas?
I am using Microsoft Access VBA.
Any ideas?
'Set a Reference to the Microsoft Scripting Runtime Library
Dim objFSO As FileSystemObject
Dim ts As TextStream
Dim strText As String
Dim strSearchText As String
Dim strFileName As String
Dim lngPos As Long
Dim lngStringCount As Long
strSearchText = "cb: TRINGLE"
strFileName = CurrentProject.Path & "\Test.txt"
'Create instance of FileSystemObject.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ts = objFSO.OpenTextFile(strFileName, ForReading)
'Read entire contents of file, save to strText variable
strText = ts.ReadAll
lngPos = 1
Do
lngPos = InStr(lngPos, strText, strSearchText)
If lngPos > 0 Then
lngStringCount = lngStringCount + 1
lngPos = lngPos + Len(strSearchText)
End If
Loop Until lngPos = 0
MsgBox "The String [" & strSearchText & "] appears " & lngStringCount & " time(s) in the " & _
"File " & strFileName & "!"
Function FindAndReplace()
Dim sSearchText As String
Dim sReplaceText As String
Dim sFileName As String
Dim lngPosition As Long
Dim intNumOfMatches As Integer
Dim strText As String
Dim strNew As String
Dim lngNewPos As Long
Dim strMsg As String
Dim intCount As Integer
Const ForReading = 1
Const ForWriting = 2
sSearchText = "cb:TRINGLE"
sReplaceText = "cb:MATERIAL"
sFileName = "C:\test.txt"
[B]intCount = CountWordOccurances(sSearchText, sFileName)
intCount = intCount \ 4[/B]
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ts = objFSO.OpenTextFile(sFileName, ForReading)
strText = ts.ReadAll
lngPosition = InStr(strText, sSearchText)
If lngPosition = 0 Then Exit Function
strNew = Left$(strText, lngPosition - 1) & sReplaceText & _
Mid$(strText, lngPosition + Len(sSearchText))
Starting
intNumOfMatches = intNumOfMatches + 1
lngNewPos = lngPosition + 1
Do
If InStr(lngNewPos, strText, sSearchText) <> 0 Then
lngPosition = InStr(lngNewPos, strText, sSearchText)
intNumOfMatches = intNumOfMatches + 1
lngNewPos = lngPosition + 1
If intNumOfMatches = 4 Then '4th occurrence
strNew = Left$(strNew, lngPosition - 1) & "/" & sReplaceText & _
Mid$(strNew, lngPosition + Len(sReplaceText))
End If
End If
Loop Until intNumOfMatches = 4 Or lngPosition = 0
Set objFile = objFSO.OpenTextFile(sFileName, ForWriting)
objFile.WriteLine strNew
objFile.Close
End Function
Function CountWordOccurances(WordToCount As String, Filename As String) As Integer
Dim strText As String
Dim strSearchText As String
Dim strFileName As String
Dim lngPos As Long
Dim lngStringCount As Long
strSearchText = WordToCount
strFileName = Filename
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set ts = objFSO.OpenTextFile(strFileName, 1)
strText = ts.ReadAll
lngPos = 1
Do
lngPos = InStr(lngPos, strText, strSearchText)
If lngPos > 0 Then
lngStringCount = lngStringCount + 1
lngPos = lngPos + Len(strSearchText)
End If
Loop Until lngPos = 0
End Function
Do
If InStr(lngNewPos, strText, sSearchText) <> 0 Then
lngPosition = InStr(lngNewPos, strText, sSearchText)
intNumOfMatches = intNumOfMatches + 1
lngNewPos = lngPosition + 1
If intNumOfMatches Mod 4 = 0 Or intNumOfMatches = 5 Or intNumOfMatches = 9 Or intNumOfMatches = 13 _
Or intNumOfMatches = 17 Or intNumOfMatches = 21 Then
strNew = Left$(strNew, lngPosition - 1) & sReplaceText & " " & _
Mid$(strNew, lngPosition + Len(sSearchText) + 1)
End If
Else
lngPosition = 0
End If
Loop Until lngPosition = 0
Comment