I have some Word file names in Column A, and some key Words in Row 1.
I use this code to Loop through all the Word Files mentioned in Col A & count each keyword from Row 1 and return the count.
I am using this code which runs perfectly.
Sub CountWordsInWordFiles()
Dim objWord As Word.Application, objDoc As Word.Document
Dim X As String, Y As Integer, LR As Integer, LC As Integer, Counter As Integer, LCCounter As Integer, FileName As String
Application.ScreenUpdating = False
LR = WordFileNames.Cells(Rows.Count, 1).End(xlUp).Row
LC = WordFileNames.Cells(1, Columns.Count).End(xlToLeft).Column
Set objWord = New Word.Application
For Counter = 2 To LR
FileName = WordFileNames.Cells(Counter, 1)
Set objDoc = objWord.Documents.Open(FileName)
'objWord.Visible = True
For LCCounter = 7 To LC
Y = 0
X = WordFileNames.Cells(1, LCCounter) ' Word to be counted
With objDoc.Content.Find
Do While .Execute(FindText:=X, Forward:=True, Format:=True, _
MatchWholeWord:=True) = True
'Display message in Word's Status Bar.
StatusBar = "Word is counting the occurrences of the text " & _
Chr$(34) & X & Chr$(34) & "."
Y = Y + 1
Loop
End With
Cells(Counter, LCCounter).Value = Y
Next LCCounter
objDoc.Close
Next Counter
Application.ScreenUpdating = True
End Sub
The only issue is some Word Files are password protected and cannot be viewed without a password which is where the code stops.
All I want is to select the cancel button on the Pop-Up so it closes down and the code moves to the next file.
Can anyone help?
Bookmarks