Find & Replace - Multiple Word 2007 documents (1 Viewer)

MarieLine77

Registered User.
Local time
Today, 23:42
Joined
Apr 4, 2012
Messages
55
Hello everyone!

OS: Vista
Software: Microsoft Word 2007

I have one folder which contains 300+ documents.

I need to run Find & Replace for two words (it is the same two words for all documents)
:
eg. Find and Replace: A with B and then C with D.

I have found a command button called "Find File" but it doesn't seem to work.

So is there a way or doing this by selecting the folder and then run the two actions either together or separately?

Your help would be much appreciated.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 10:42
Joined
Jan 20, 2009
Messages
12,849
It can be done in code by automatically opening each document and applying the Replace method to the whole document.

I have almost exactly the code you need. I will dig it out later this evening and post it if nobody provides something in the meantime.
 

MarieLine77

Registered User.
Local time
Today, 23:42
Joined
Apr 4, 2012
Messages
55
It can be done in code by automatically opening each document and applying the Replace method to the whole document.

I have almost exactly the code you need. I will dig it out later this evening and post it if nobody provides something in the meantime.

Thank you so much GalaxiomAtHome :)!

What kind of code is it? Macro?

I look forward to receiving your code and would appreciate it ASAP as I have a deadline and I am currently doing it manually :( whilst waiting for a reply...
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 10:42
Joined
Jan 20, 2009
Messages
12,849
I have found the macro.

As it stands it is not exactly what you need. It is far more elaborate. It uses records in a text file to change instances of words in a master document based on the name and sex and saves them as new documents with the name.

I could certainly modify it to do what you want.

How urgent is your deadline?
 

MarieLine77

Registered User.
Local time
Today, 23:42
Joined
Apr 4, 2012
Messages
55
Thank you.
My deadline is Tuesday 8am (I am very slow as I have a medical condition that affects my fingers).
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 10:42
Joined
Jan 20, 2009
Messages
12,849
Not a problem. Will post tonight (East Australia, GMT +10).

The Word macro will allow you to choose a group of files in which it replace all instances of multiple designated text strings and save.
 

MarieLine77

Registered User.
Local time
Today, 23:42
Joined
Apr 4, 2012
Messages
55
Not a problem. Will post tonight (East Australia, GMT +10).

The Word macro will allow you to choose a group of files in which it replace all instances of multiple designated text strings and save.

Thank you so much!
Reminder: The word documents to be amended are all in one folder.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 10:42
Joined
Jan 20, 2009
Messages
12,849
Open the attached document and agree to the macros running.

Instructions are in the document.

It not an especially elegant rendition of the task as it requires you to edit the macro rather than have a nice list of replacements but it will do you your job.

I will check how you went in the morning.
 

Attachments

  • ReplaceMulti.zip
    18.6 KB · Views: 4,581

MarieLine77

Registered User.
Local time
Today, 23:42
Joined
Apr 4, 2012
Messages
55
Open the attached document and agree to the macros running.

Instructions are in the document.

It not an especially elegant rendition of the task as it requires you to edit the macro rather than have a nice list of replacements but it will do you your job.

I will check how you went in the morning.

Sorry! I don't know how to do this:
"Change the placeholder strings in the constants at top of the DoReplace macro to the strings you want to be found and replaced. (Notice the spaces before and after the strings to ensure it doesn't find partial matches in other words.)"

Could you give me a brief step by step?
 

MarieLine77

Registered User.
Local time
Today, 23:42
Joined
Apr 4, 2012
Messages
55
Not to worry. I am using the record feature of Macro and doing one document at a time.
Thank you for your help.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 10:42
Joined
Jan 20, 2009
Messages
12,849
Open Visual Basic in the Developer Ribbon.

Change the quoted section in the lines that start with Const to what you want them to be. For example:

Const Find1 = " first find string " to Const Find1 = " someword "
Const Replace1 = " first replacement " to Const Replace1 = " newword "

Note that the Replace is case sensitive.
 

MarieLine77

Registered User.
Local time
Today, 23:42
Joined
Apr 4, 2012
Messages
55
Sorry :eek: I do not see a quoted section. It is blank/grey...
See print screen attached. Sorry I have never used this feature before.
Please kindly let me know what to click on exactly.
Thank you.

[/URL] Uploaded with ImageShack.us[/IMG]
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 10:42
Joined
Jan 20, 2009
Messages
12,849
You seem to be looking at the macros in your own document. You need to open the one I sent.

All the target files need to be closed before you run it or there will be a conflict when the macro tries to open an already open document.

BTW I noticed a small error in the original macro. I hadn't fixed the error handler name and enabled it

Here it is again with that fixed.
 

Attachments

  • ReplaceMulti.zip
    16.2 KB · Views: 3,030

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 10:42
Joined
Jan 20, 2009
Messages
12,849
The screen shot is too blurry to see but I think you are being distracted by the marcos in your own Normal template.

There should be a macro called "DoReplace" in the macros for "ThisDocument".

I have downloaded the document I posted and the macro is definitely included.
 

MarieLine77

Registered User.
Local time
Today, 23:42
Joined
Apr 4, 2012
Messages
55
I had forgotten to enable macro...

However, it is still not working. Can you check if this is right?

Sub DoReplace()

Const Find1 = " company name "
Const Replace1 = " GREEN GIANTS "

Const Find2 = " service user "
Const Replace2 = " Resident "

I selected about 4 words documents and it went through the process and said it is complete but when I checked the documents there was no changes...
 

techfreak

New member
Local time
Today, 16:42
Joined
Apr 15, 2014
Messages
2
Thread resurrection alert!

This thread prompted me to register! Just wanted to say thanks to Galaxiom for the code! It works a treat.

I spent hours and hours searching for a simple script that would allow me to change some text in word documents, everything I found was too clunky and wouldn't work how I felt it should...This has been an absolute godsend!

Thank you Galaxiom!
 

Mr-ibo

New member
Local time
Today, 23:42
Joined
Mar 25, 2016
Messages
1
hi guys
i have 2 problems can u help me
first: macros can t replace AA with * when i have a text like AAA why?
second:how can i use unicode characters?
tnx.
 

katsloko

New member
Local time
Today, 19:42
Joined
Jun 23, 2016
Messages
1
Open Visual Basic in the Developer Ribbon.

Change the quoted section in the lines that start with Const to what you want them to be. For example:

Const Find1 = " first find string " to Const Find1 = " someword "
Const Replace1 = " first replacement " to Const Replace1 = " newword "

Note that the Replace is case sensitive.

Hi Galaxiom,

I found your post and register as well. Hopefully will share more and contribute to your Forum - I found it very helpful and useful to all of us. Per the code below, Is there a way to Replace special characters or pretty much limited to the normal words? Take a look at the code below and I'm getting a syntax or end of statement when compiling on the VB editor.

Thank you so much!

Code:
Sub DoReplace()

Const Find1 = " INA §214(b) "
Const Replace1 = " <a href="129#jd_ina245h1">INA §214(b) "

Const Find2 = " INA §101(a)(15)(L) "
Const Replace2 = " <a href="176">INA §101(a)(15)(L) "

Const Find3 = " INA §106 "
Const Replace3 = " <a href="272">INA §106 "


Dim FilePick As FileDialog
Dim FileSelected As FileDialogSelectedItems
Dim WordFile As Variant  ' FileName placeholder in selected files loop
Dim FileJob As String    ' Filename for processing

Dim WorkDoc As Object
Dim WholeDoc As Range
Dim FooterDoc As Range

'   On Error GoTo CreateReports_Error
    
    Set FilePick = Application.FileDialog(msoFileDialogFilePicker)
    
    With FilePick
        .Title = "Choose Report Template"
        .Filters.Clear
        .Filters.Add "Word Documents & Templates", "*.do*"
        .Filters.Add "Word 2003 Document", "*.doc"
        .Filters.Add "Word 2003 Template", "*.dot"
        .Filters.Add "Word 2007 Document", "*.docx"
        .Filters.Add "Word 2007 Template", "*.dotx"
        .Show
    End With
    
    Set FileSelected = FilePick.SelectedItems

    If FileSelected.Count <> 0 Then
         
        For Each WordFile In FileSelected
        
            FileJob = WordFile
  
            Set WorkDoc = Application.Documents.Open(FileJob, , , , , , , , , , , False)
            
            Set WholeDoc = WorkDoc.Content
            Set FooterDoc = WorkDoc.Sections(1).Footers(wdHeaderFooterPrimary).Range
            
            With FooterDoc
                .Find.Execute Find1, True, True, , , , True, , , Replace1, wdReplaceAll
                .Find.Execute Find2, True, True, , , , True, , , Replace2, wdReplaceAll
            End With
            
            With WholeDoc.Find
                .Execute Find1, True, True, , , , True, , , Replace1, wdReplaceAll
                .Execute Find2, True, True, , , , True, , , Replace2, wdReplaceAll
            End With

            WorkDoc.Save
            WorkDoc.Close
            
        Next
        
    End If
    
    MsgBox "Completed"
    
DoReplace_Exit:
    
    Set WholeDoc = Nothing
    Set FilePick = Nothing

    Set WorkDoc = Nothing
    Set FooterDoc = Nothing

   Exit Sub

DoReplace_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure DoReplace of VBA Document ReplaceMulti"
    Resume DoReplace_Exit
    
End Sub
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 10:42
Joined
Jan 20, 2009
Messages
12,849
The problem would be the double quotes inside the strings. They can be escaped by doubling them.

Code:
Const Replace2 = " <a href=""176"">INA §101(a)(15)(L) "
 

Users who are viewing this thread

Top Bottom