I have a large number of txt-files, which I want to search for specific words. My approach is using an excel macro to open the txt files in word and than search each occurence of a list of words, which I provide in the excel file. It gives me a list, of how often each word occurs in each document. I have managed to do so, using the following code:
Sub CounterofWords()
Application.ScreenUpdating = False
Dim wdApp As Word.Application
Set wdApp = CreateObject("Word.application")
wdApp.Visible = False
For d = 1 To 23
Dim wdDoc As Word.Document
FName = "C:\Users\Andreas\Desktop\test\" & Cells(d + 1, 11) & "_htm.txt"
On Error GoTo txtdesign
Set wdDoc = wdApp.Documents.Open(filename:=FName)
i = 15
Do While Cells(1, i) <> ""
iCount = 0
Application.ScreenUpdating = False
With wdApp.Selection.Find
.ClearFormatting
.Text = Cells(1, i).Value
Do While .Execute
iCount = iCount + 1
wdApp.Selection.MoveRight
Loop
End With
Cells(d + 1, i).Value = iCount
i = i + 1
Loop
wdDoc.Close savechanges:=False
Set wdDoc = Nothing
Next d
wdApp.Quit
Set wdApp = Nothing
Application.ScreenUpdating = True
Exit Sub
txtdesign:
FName = "C:\Users\Andreas\Desktop\test\" & Cells(d + 1, 11) & "_txt.txt"
Resume
End Sub
Everything works fine so far. Now I want to be able to search for regular expressions. I need this for example to avoid certain combinations of words in my search.
It seems to be a problem that I can not write something like
With wdApp.Selection.regex
Anyways, I don't know how to make regex work in a situation like this and appreciate your help!