Solved

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

Posted on 2012-04-10
12
429 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 80

Assisted Solution

by:byundt
byundt earned 500 total points
Comment Utility
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
Comment Utility
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 80

Expert Comment

by:byundt
Comment Utility
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
 

Author Comment

by:pelampe
Comment Utility
That worked beautifully!

THANK YOU!
0
 
LVL 80

Expert Comment

by:byundt
Comment Utility
How long did it take to process 250 files?
0
 

Author Comment

by:pelampe
Comment Utility
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 80

Assisted Solution

by:byundt
byundt earned 500 total points
Comment Utility
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
Comment Utility
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 80

Accepted Solution

by:
byundt earned 500 total points
Comment Utility
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
Comment Utility
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 80

Assisted Solution

by:byundt
byundt earned 500 total points
Comment Utility
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
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

There is a feature provided by MS Word that lets you create an Table of Contents for your Word document automatically. To use this feature for other documents there are two steps involved,   1.  Prepare your document for a table of contents (he…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

743 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

15 Experts available now in Live!

Get 1:1 Help Now