Forum Discussion
pjs15
Feb 10, 2026Copper Contributor
Search a specific string in URLs across multiple Word documents
I would like to search for a specific string ("urldefense") in URLs across multiple Word documents that are all in the same folder. (I use Word on a Windows 11 desktop.) If it's possible to do this, ...
cdrgreg
Feb 12, 2026Copper Contributor
Sub BacthFindI()
'Compiled by Greg Maxey
Dim strFileName As String
Dim strFolderPath As String
Dim oDoc As Document
Dim oRng As Range
Dim lngCount As Long
Application.ScreenUpdating = True
'Specify folder containing files
strFolderPath = "D:\Batch\Test Batch\"
'Loop through all the files of *.doc type in the directory by using Dir$ function
strFileName = Dir$(strFolderPath & "*.doc*")
Do While strFileName <> ""
Set oDoc = Documents.Open(strFolderPath & strFileName, , , False, , , , , , , , False)
If Not ActiveWindow.View.ShowFieldCodes = True Then oDoc.Fields.ToggleShowCodes
Set oRng = oDoc.Range
With oRng.Find
.Text = "urldefense"
Do While .Execute
lngCount = lngCount + 1
Loop
End With
oDoc.Close wdDoNotSaveChanges
strFileName = Dir$
Loop
MsgBox lngCount
Application.ScreenUpdating = True
End SubTo search and count. Use the above.
To search and revise, use something like the following:
Sub BacthFindII()
'Compiled by Greg Maxey
Dim strFileName As String
Dim strFolderPath As String
Dim oDoc As Document
Dim oRng As Range
Application.ScreenUpdating = False
'Specify folder containing files
strFolderPath = "D:\Batch\Test Batch\"
'Loop through all the files of *.doc type in the directory by using Dir$ function
strFileName = Dir$(strFolderPath & "*.doc*")
Do While strFileName <> ""
Set oDoc = Documents.Open(strFolderPath & strFileName, , , False, , , , , , , , False)
If Not ActiveWindow.View.ShowFieldCodes = True Then oDoc.Fields.ToggleShowCodes
Set oRng = oDoc.Range
With oRng.Find
.Text = "urldefense"
.Replacement.Text = "someothertext"
.Execute Replace:=wdReplaceAll
End With
oDoc.Fields.ToggleShowCodes
oDoc.Close wdSaveChanges
strFileName = Dir$
Loop
Application.ScreenUpdating = True
lbl_Exit:
Exit Sub
End Sub