Solved

Getting 5111 error - cant figure out why?

Posted on 2014-12-28
4
208 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
  • 2
  • 2
4 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 500 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now