Getting 5111 error - cant figure out why?

I have a folder with about 300 word docs that I need to do a find and replace of the same line of text. Normally, I would use scripting with the file system object. I found some code, but it is giving me an error 5111  http://support.microsoft.com/kb/920229

OS: Win 7 64 bit Pro
Office 2013
Word version 15.0
Visual basic 6.0

Any ideas how to make this run?

Public Function WordSwap()

Dim WordApp As New Word.Application
Dim Swap As Integer
   
    Swap = 0
    With WordApp.FileSearch
        .LookIn = "D:\mark\Desktop\op notes done"             ' where to search
        .SearchSubFolders = True    ' search the subfolders
        .FileName = "*.doc"         ' file pattern to match

        ' if more than one match, execute the following code
        If .Execute() > 0 Then
            ' for each file you find, run this loop
            For i = 1 To .FoundFiles.Count
                ' open the file based on its index position
                Documents.Open FileName:=.FoundFiles(i)

                ' search and replace the address
                selection.Find.ClearFormatting
                selection.Find.Replacement.ClearFormatting
                With selection.Find
                    .Text = "OldAddress"
                    .MatchCase = True
                    .Replacement.Text = "NewAddress"
                    Swap = Swap + 1
                End With
                selection.Find.Execute Replace:=wdReplaceAll

                ' replace e-mail address
                With selection.Find
                    .Text = "Oldemail"
                    .Replacement.Text = "Newemail"
                End With
                selection.Find.Execute Replace:=wdReplaceAll

                ' save and close the current document
                ActiveDocument.Close wdSaveChanges
            Next i
        Else
            ' if the system cannot find any files
            ' with the .doc extension
            MsgBox "No files found."
        End If
    End With
   
    MsgBox "There were a total of " & i & " files to process. Changes were made in " & Swap & " files."
   
End Function
M_EpsteinAsked:
Who is Participating?
 
GrahamSkanRetiredCommented:
I can't see any major problems with your code. However, this version shows another way of looping through the files in a folder and also uses Range objects instead the Selection object.
Public Function WordSwap2()

    Dim WordApp As New Word.Application
    Dim iDocCount As Integer
    Dim iTextFoundCount As Integer
    Dim strFolder As String
    Dim strFile As String
    Dim wrdDoc As Word.Document
    Dim bChangesMade As Boolean
    iDocCount = 0
    
    Set WordApp = CreateObject("Word.Application")
    strFile = Dir(strFolder & "\*.doc")
    Do Until strFile = ""
        iDocCount = iDocCount + 1
        Set wrdDoc = WordApp.Documents.Open(strFolder & "\" & strFile)
        
        ' search and replace the address
        With wrdDoc.Range.Find
            .Text = "OldAddress"
            .MatchCase = True
            .Replacement.Text = "NewAddress"
            If .Execute(Replace:=wdReplaceAll) Then
                bChangesMade = True
            Else
                bChangesMade = False
            End If
         End With
         
         ' replace e-mail address
        With wrdDoc.Range.Find
            .Text = "Oldemail"
            .Replacement.Text = "Newemail"
            If .Execute(Replace:=wdReplaceAll) Then
                bChangesMade = True
            End If
        End With
         
        ' close the current document and save if necessary
        If bChangesMade Then
            iTextFoundCount = iTextFoundCount + 1
            wrdDoc.Close wdSaveChanges
        Else
            wrdDoc.Close wdDoNotSaveChanges
        End If
        
        strFile = Dir()
    Loop
    WordApp.Quit
    MsgBox "There were a total of " & iDocCount & " files to process. Changes were made in " & iTextFoundCount & " files."
   
End Function

Open in new window

0
 
M_EpsteinAuthor Commented:
It hangs up on .filesearch
Apparently not a supported method.
0
 
GrahamSkanRetiredCommented:
The link that you posted explains that FileSearch stopped being supported at Office 2007. I notice that you are on 2013. You can either use the FSO, or the intrinsic VB6/A method Dir() that I showed in the sample.
0
 
M_EpsteinAuthor Commented:
I just tested your code. Worked like a charm! Thank you so much!!!
Have a wonderful holiday and new year!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.