?
Solved

Getting 5111 error - cant figure out why?

Posted on 2014-12-28
4
Medium Priority
?
230 Views
Last Modified: 2014-12-28
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
0
Comment
Question by:M_Epstein
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 40520943
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
 

Author Comment

by:M_Epstein
ID: 40520964
It hangs up on .filesearch
Apparently not a supported method.
0
 
LVL 76

Expert Comment

by:GrahamSkan
ID: 40521015
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
 

Author Comment

by:M_Epstein
ID: 40521139
I just tested your code. Worked like a charm! Thank you so much!!!
Have a wonderful holiday and new year!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
Cancel future meetings from user mailboxes in Office 365 using Remove-CalendarEvents
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question