Solved

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

Posted on 2012-04-10
12
434 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
[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
  • 6
  • 6
12 Comments
 
LVL 81

Assisted Solution

by:byundt
byundt earned 500 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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 500 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 500 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 500 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
word 2016 1 39
How to transform one row line like this in excel table 2010? 8 40
analyze malware in windows document 9 63
Word automation in Visual FoxPro 3 25
When creating Microsoft Word-based forms there may be a need to have a form field repeated throughout the whole document. For instance, with a company name, you may want this information repeated automatically throughout the document rather than man…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
Office 365 is currently available in five editions. Three of them are for business use: Office 365 Business Essentials, Office 365 Business, and Office 365 Business Premium. Two of them are for home/personal use: Office 365 Home and Office 365 Perso…
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …

734 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