Solved

Getting 5111 error - cant figure out why?

Posted on 2014-12-28
4
212 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
My experience with Windows 10 over a one year period and suggestions for smooth operation
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
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…

930 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

16 Experts available now in Live!

Get 1:1 Help Now