how to run word macro on multiple files

I have a macro (see below) saved as a macro.txt which I have used in the past with word'03 to change text in multiple word files in a given folder.

However, I can not remember how to run the macro!  

I tried running it within Word'07 but no success 'run time error 5111, command 'With Application.FileSearch' not available on this platform'.

Where can I run it, or is there an alternative method I now need to use?

Thanks,
Andy

Sub FindReplaceAllDocsInFolder( )
Dim i As Integer
Dim doc As Document
Dim rng As Range

With Application.FileSearch
    .NewSearch
    .LookIn = "B:\Attachments\"
    .SearchSubFolders = False
    .FileType = msoFileTypeWordDocuments
    If Not .Execute( ) = 0 Then
        For i = 1 To .FoundFiles.Count
             Set doc = Documents.Open(.FoundFiles(i))
             Set rng = doc.Range
              With rng.Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .Text = "Andy"
                .Replacement.Text = "Matt"
                .Forward = True
                .Wrap = wdFindContinue
                .Format = False
                .MatchCase = False
                .MatchWholeWord = False
                .MatchWildcards = False
                .MatchSoundsLike = False
                .MatchAllWordForms = False
                .Execute Replace:=wdReplaceAll
               End With

             doc.Save
             doc.Close

             Set rng = Nothing
             Set doc = Nothing
        Next i
    Else
        MsgBox "No files matched " & .FileName
    End If
End With
End Sub
Answer
Answer
FileSearch is not available in Word 2007 vba, however the following will work:

Sub BatchProcess()
Dim strFileName As String
Dim strPath As String
Dim oDoc As Document
Dim fDialog As FileDialog
Dim oStory As Range
Dim oRng As Range
Const strFind As String = "Andy"
Const strRepl As String = "Matt"
    Set fDialog = Application.FileDialog(msoFileDialogFolderPicker)
    With fDialog
        .Title = "Select folder and click OK"
        .AllowMultiSelect = False
        .InitialView = msoFileDialogViewList
        If .Show <> -1 Then
            MsgBox "Cancelled By User", , _
                   "List Folder Contents"
            Exit Sub
        End If
        strPath = fDialog.SelectedItems.Item(1) & "\"
    End With
    strFileName = Dir$(strPath & "*.doc")
    While Len(strFileName) <> 0
        WordBasic.DisableAutoMacros 1
        Set oDoc = Documents.Open(strPath & strFileName)
        For Each oStory In ActiveDocument.StoryRanges
            Set oRng = oStory
            With oRng.Find
                Do While .Execute(FindText:=strFind)
                    oRng.Text = strRepl
                    oRng.Collapse wdCollapseEnd
                Loop
            End With
            If oStory.StoryType <> wdMainTextStory Then
                While Not (oStory.NextStoryRange Is Nothing)
                    Set oStory = oStory.NextStoryRange
                    Set oRng = oStory
                    With oRng.Find
                        Do While .Execute(FindText:=strFind)
                            oRng.Text = strRepl
                            oRng.Collapse wdCollapseEnd
                        Loop
                    End With
                Wend
            End If
        Next oStory
        oDoc.SaveAs Filename:=strPath & strFileName
        oDoc.Close SaveChanges:=wdDoNotSaveChanges
        strFileName = Dir$()
        WordBasic.DisableAutoMacros 0
    Wend
    Set oDoc = Nothing
    Set oStory = Nothing
    Set oRng = Nothing
End Sub
Graham Mayor (Microsoft Word MVP 2002-2019)
For more Word tips and downloads visit my web site
https://www.gmayor.com/Word_pages.htm

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated October 5, 2021 Views 6,323 Applies to: