Solved

Getting 5111 error - cant figure out why?

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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