?
Solved

Running the Find and Replace command in Microsoft Word WITHOUT opening DOC file

Posted on 2012-04-10
12
Medium Priority
?
438 Views
Last Modified: 2012-06-28
Hello,

I have received notice that I am about to receive about 250 Word DOC files, and the customer is asking me to go into each one and replace one particular word with another.

Is there a macro or utility that I can run to do such an action WITHOUT having to open each DOC file?

The word to be replaced is:   SURPLUS
The new word is to be:  ADDITIONAL

The files are Word DOC and NOT DOCX files.  I am using Word 2007 if that helps.

Any input would be appreciated.

Since this is a very urgent request, I am awarding the MAX points allowable.

TIA
0
Comment
Question by:pelampe
  • 6
  • 6
12 Comments
 
LVL 81

Assisted Solution

by:byundt
byundt earned 2000 total points
ID: 37830487
While you must open each workbook to update it, you can do so using a macro while you enjoy a cup of coffee.

Paste the following code in a new module sheet in an open Word document, then try running the DocumentUpdater macro.
Sub DocumentUpdater()
Dim doc As Document
Dim f As String, flPath As String
Dim n As Integer
flPath = InputBox("Please enter the path to the documents you want to update")
If flPath = "" Then Exit Sub

Application.ScreenUpdating = False
If Right(flPath, 1) <> Application.PathSeparator Then flPath = flPath & Application.PathSeparator
f = Dir(flPath & "*.doc")
Do Until f = ""
    Set doc = Documents.Open(flPath & f)
    n = n + 1
    With Selection.Find
        .Text = "surplus"
        .Replacement.Text = "additional"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    doc.Save
    doc.Close SaveChanges:=False
    f = Dir
Loop
Application.ScreenUpdating = True
MsgBox "All done. " & n & " files processed."
End Sub

Open in new window

0
 

Author Comment

by:pelampe
ID: 37832334
Are you saying that I must have each DOC file open each time I want to run this macro?

If YES - then I would like to ask what is the difference between running this macro and running the built-in function called FIND/REPLACE (CTRL-H)?

If NO, then are you saying that I can insert and run this macro in a BLANK MS Word DOC file, and the macro will go through ALL of the MS Word DOC files in the same directory and FIND and then REPLACE using the words embedded in the macro?
0
 
LVL 81

Expert Comment

by:byundt
ID: 37833505
The macro does it all for you.

1) Install the macro in a blank Microsoft Word document
2) Run the macro
3) The macro will ask you for the path to a folder containing your files
4) The macro will open each .doc file in that folder, one at a time
         a. Perform the Find & Replace
         b. Save the file
         c. Close the file
         d. Proceed to next file in the folder
5) When everything is done, the macro tells you how many files it processed

Step 4 will take a while if you have 250 files. Nothing will appear to be happening on the screen, which makes the macro run faster. So I suggest testing the code on a folder containing two or three files. Once you satisfy yourself that it is working, then run it on the folder containing your 250 files.
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:pelampe
ID: 37834636
That worked beautifully!

THANK YOU!
0
 
LVL 81

Expert Comment

by:byundt
ID: 37834802
How long did it take to process 250 files?
0
 

Author Comment

by:pelampe
ID: 37834958
Well, to be honest I only tried it on 10 "test" files.  Haven't gotten the 250  that are slated to come in, yet.

Seems like it took about 10 seconds, or slightly less.

The test files were 1 page (Letter size) each, single spaced.  47kb.

The ones that are coming are going to be about 3-4 pages each from what I gather.
0
 
LVL 81

Assisted Solution

by:byundt
byundt earned 2000 total points
ID: 37835070
pelampe,
If you wouldn't mind, could you please post the time that it takes to run 250 files? This looks like one of those questions where your membership in Experts Exchange is going to pay off, so I'd like to document it.

I've added a couple of instructions so the macro runs the stopwatch for you, then reports the elapsed time.

Sub DocumentUpdater()
Dim doc As Document
Dim f As String, flPath As String
Dim n As Integer
Dim d As Double
flPath = InputBox("Please enter the path to the documents you want to update")
If flPath = "" Then Exit Sub

Application.ScreenUpdating = False
d = Timer
If Right(flPath, 1) <> Application.PathSeparator Then flPath = flPath & Application.PathSeparator
f = Dir(flPath & "*.doc")
Do Until f = ""
    Set doc = Documents.Open(flPath & f)
    n = n + 1
    With Selection.Find
        .Text = "surplus"
        .Replacement.Text = "additional"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    doc.Save
    doc.Close SaveChanges:=False
    f = Dir
Loop
Application.ScreenUpdating = True
MsgBox "All done. " & n & " files processed." & vbLf & _
    "Elapsed time " & (Timer - d) & " seconds"
End Sub

Open in new window


Brad
0
 

Author Comment

by:pelampe
ID: 37835126
Sure thing.  It will be the end of this week when I get them.

And since you were tweaking the code, I was wondering how difficult it would be to put a way so that when you initialize the macro, to have it able to navigate to a  folder instead of a blank field? I believe that is called the common dialog from my old vb class days (but I could be wrong on that).

It's not a big deal either way, but I thought I'd ask.

Phil
0
 
LVL 81

Accepted Solution

by:
byundt earned 2000 total points
ID: 37835304
Phil,
Displaying the file browser is easy in Excel (my real expertise), but somewhat more challenging in Word. Nevertheless, I modified the code to let you pick a file (by actually opening it), and thereby define the path.

Brad

Sub DocumentUpdater()
Dim doc As Document
Dim f As String, flFirst As String, flPath As String
Dim n As Integer
Dim d As Double
Dialogs(wdDialogFileOpen).Show
If ActiveDocument.Name = ThisDocument.Name Then Exit Sub
Set doc = ActiveDocument

Application.ScreenUpdating = False
d = Timer
flPath = doc.Path & Application.PathSeparator
f = doc.Name
flFirst = f
Do Until f = ""
    If n > 0 Then Set doc = Documents.Open(flPath & f)
    n = n + 1
    With Selection.Find
        .Text = "surplus"
        .Replacement.Text = "additional"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = True
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
    doc.Save
    doc.Close SaveChanges:=False
    f = IIf(n = 1, Dir(flPath & "*.doc"), Dir)
    If f = flFirst Then f = Dir
Loop
Application.ScreenUpdating = True
MsgBox "All done. " & n & " files processed." & vbLf & _
    "Elapsed time " & (Timer - d) & " seconds"
End Sub

Open in new window

0
 

Author Comment

by:pelampe
ID: 37835977
Brad,

I ran the modified macro you posted above, it got locked into a loop and never recovered.  I waited about 40 seconds to see if it would return back, but it did not.

I did notice that of the nine files it tried to run the FIND/REPLACE on, it managed to complete 3 of them before it looped out.

Is there any way of going through it step-by-step in the macro editor to see where it might lock up?

I resorted back to the previous code (posted at 16:39) and it ran fine.  I noticed that after I ran it, it posted a results dialog (which i've attached a screen shot of).

For the 9 test files (sorry, i thought it was 10 earlier, but actually it was nine) the time elapsed was was just 4 seconds.  Pretty quick indeed.

Thanks,
Phil
4-11-2012-11-01-56-PM.jpg
0
 
LVL 81

Assisted Solution

by:byundt
byundt earned 2000 total points
ID: 37839191
Phil,
If you want to step through a macro, do the following:

1)  Comment out statement 10 by putting a single quote before the first character:
'Application.ScreenUpdating = False               'You can now see the document as each new one is opened

2) Click in the margin to the left of statement 30. VBA should respond by adding a big maroon dot in the margin. This will act as a stop, so code execution will cease just before trying to execute that statement--and you will be returned to the VBA Editor.

3) You may now execute a single statement at a time with F8. Or you can continue code execution until the next stop with F5. If you go statement by statement, you can return to the Word user interface with ALT + F11 (and back again so you can hit F8 or F5).

4) You may want to display the Immediate pane. Do this from the View menu in the VBA Editor. With the Immediate pane, you can change the value of variables, execute one or more commands or get the value of a variable or expression.
?f              'If you put the cursor to the right of f and then hit Enter, the Immediate pane will display the value of f
f = Dir : ?f         'The colon is used to separate one statement from another. The first statement updates the value of f and the second displays that new value.


If you would like to try a completely different approach, here is a macro to do a find and replace on all files in a folder from the Microsoft Word MVP web site: http://word.mvps.org/FAQs/MacrosVBA/BatchFR.htm

Brad
0
 

Author Comment

by:pelampe
ID: 37840195
Brad,

I am going to be going out of town tomorrow and will not be able to try this out until Monday, but I will try it then for sure.

Thanks,
Phil
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
A few years ago I was very much a beginner at VBA, and that very much remains the case today.  I'll do my best to explain things as I go in the hope that other beginners can follow.  If you just want to check out a tool that creates a Select Case fu…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…
Suggested Courses

840 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