I have been using the excellent macro below, created by Macropod an posted in another forum, to run through all files in a designated folder to search for desired words. It works perfectly but I was wondering how to adjust it so that the search terms could be entered via an InputBox rather than having to manually adjust the VBA code each time.
Any help is greatly appreciated.
Many thanks in advance.
Luke
Sub CollateDocumentData()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, strDocNm As String, strTmp As String, strOut As String
Dim wdDoc As Document, i As Long: Const StrFnd As String = "than,and"
strDocNm = ActiveDocument.FullName
strFolder = GetFolder: If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
If strFolder & "\" & strFile <> strDocNm Then
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
strTmp = ""
With wdDoc
With .range.Find
.ClearFormatting
.Replacement.ClearFormatting
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
For i = 0 To UBound(Split(StrFnd, ","))
.Text = Split(StrFnd, ",")(i)
.Execute
If .Found = True Then strTmp = strTmp & vbCr & "" & Split(StrFnd, ",")(i)
Next
End With
If strTmp <> "" Then strOut = strOut & vbCr & strFile & ": " & strTmp & Chr(13)
.Close SaveChanges:=True
End With
End If
strFile = Dir()
Wend
Set wdDoc = Nothing
'If you want the results to be given in a new temporary document, remove the 'MsgBox' line below
'If you want the results to be given in a message box within the current document, remove the 'Document.Add' and 'ActiveDocument' lines below
'Documents.Add
'ActiveDocument.range.Text = "The following matches were made:" & strOut
MsgBox ("The following matches were made:" & vbCr & strOut)
Application.ScreenUpdating = True
End Sub
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.path
Set oFolder = Nothing
End Function