Go Back   Access World Forums > Apps and Windows > Word

 
Reply
 
Thread Tools Rating: Thread Rating: 2 votes, 5.00 average. Display Modes
Old 05-28-2012, 06:12 AM   #16
MarieLine77
Newly Registered User
 
Join Date: Apr 2012
Posts: 55
Thanks: 20
Thanked 1 Time in 1 Post
MarieLine77 is on a distinguished road
Re: Find & Replace - Multiple Word 2007 documents

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...

MarieLine77 is offline   Reply With Quote
The Following User Says Thank You to MarieLine77 For This Useful Post:
hassanogaibi (06-27-2016)
Old 04-15-2014, 05:08 AM   #17
techfreak
Newly Registered User
 
Join Date: Apr 2014
Posts: 2
Thanks: 1
Thanked 1 Time in 1 Post
techfreak is on a distinguished road
Re: Find & Replace - Multiple Word 2007 documents

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!
techfreak is offline   Reply With Quote
The Following User Says Thank You to techfreak For This Useful Post:
hassanogaibi (06-27-2016)
Old 03-25-2016, 10:36 AM   #18
Mr-ibo
Newly Registered User
 
Join Date: Mar 2016
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
Mr-ibo is on a distinguished road
Re: Find & Replace - Multiple Word 2007 documents

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.

Mr-ibo is offline   Reply With Quote
The Following User Says Thank You to Mr-ibo For This Useful Post:
hassanogaibi (06-27-2016)
Old 06-23-2016, 07:59 AM   #19
katsloko
Newly Registered User
 
Join Date: Jun 2016
Posts: 1
Thanks: 0
Thanked 1 Time in 1 Post
katsloko is on a distinguished road
Re: Find & Replace - Multiple Word 2007 documents

Quote:
Originally Posted by Galaxiom View Post
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 &sect;214(b) "

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

Const Find3 = " INA &sect;106 "
Const Replace3 = " <a href="272">INA &sect;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
katsloko is offline   Reply With Quote
The Following User Says Thank You to katsloko For This Useful Post:
hassanogaibi (06-27-2016)
Old 06-23-2016, 01:05 PM   #20
Galaxiom
Super Moderator
 
Join Date: Jan 2009
Location: NSW Australia
Posts: 10,730
Thanks: 58
Thanked 1,291 Times in 1,218 Posts
Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold
Re: Find & Replace - Multiple Word 2007 documents

The problem would be the double quotes inside the strings. They can be escaped by doubling them.

Code:
Const Replace2 = " <a href=""176"">INA &sect;101(a)(15)(L) "
Galaxiom is offline   Reply With Quote
The Following User Says Thank You to Galaxiom For This Useful Post:
hassanogaibi (06-28-2016)
Old 07-18-2017, 09:33 AM   #21
rakini22
Newly Registered User
 
Join Date: Jul 2017
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
rakini22 is on a distinguished road
Re: Find & Replace - Multiple Word 2007 documents

Hi Galaxiom,
Thanks for the post it was really useful. I just have a quick question. I need to find words like abcB1 and replace it with abcB2. The above macro changes all B1 to B2. That's good I have words like that but in addition I need to change for the words abcB1, abB1c, abB1, .cB1. too. If its a part of one word how to get it done? Any insights would be great. Thanks in advance.

Last edited by rakini22; 07-18-2017 at 09:38 AM.
rakini22 is offline   Reply With Quote
Old 07-19-2017, 02:57 AM   #22
Galaxiom
Super Moderator
 
Join Date: Jan 2009
Location: NSW Australia
Posts: 10,730
Thanks: 58
Thanked 1,291 Times in 1,218 Posts
Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold Galaxiom is a splendid one to behold
Re: Find & Replace - Multiple Word 2007 documents

Quote:
Originally Posted by rakini22 View Post
If its a part of one word how to get it done? Any insights would be great.
I have not quite been able to understand how you need to vary the outcome.

However, take a look at the parameters of the Find Object. My sub just uses the defaults (the blanks between the commas) for most of them. Maybe you will be able to work out what you need to change?

Alternatively, break out the Find section of the sub into a separate sub and implement some arguments for the find and replace strings. Then you can loop through multiple replacements to cover all the required variations.

Galaxiom is offline   Reply With Quote
Old 08-16-2017, 09:58 AM   #23
Santi
Newly Registered User
 
Join Date: Aug 2017
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Santi is on a distinguished road
Re: Find & Replace - Multiple Word 2007 documents

Hey Galaxiom, thanks for the code it is very useful, I also registered to reply !
I am facing two issues.

What if I want to change just a part of a word? For instance if I want to turn intention to intent°

I trid to define :

Const Find1 = "tion"
Const Replace1 = "t°"

But if it is inside a word it would not change it.

Also, how can I insert a symbol. I want an upper arrow and I tried to define it with unicode but the way the value is defined (as a const) it doesn't accept it.

Const Find2 = "increasing"
Const Replace2 = ChrW(&H2191)

If you could tell me how can I change a word into a symbol and how can I replace letters within a word it would be amazing !

Regards

Santi

Santi is offline   Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question Is it possible to search Word Documents using Access 2007? JimmyTap General 3 05-17-2012 08:30 AM
Find and replace from Access 2007 to Word 2007 PuddinPie Modules & VBA 0 03-29-2012 05:13 AM
Populate Multiple Word Documents with Recordset abroniewski Modules & VBA 1 01-27-2012 04:58 PM
Merge multiple Word-documents into one using Access spikepl Modules & VBA 12 03-26-2011 08:54 AM
word find and replace VBA or Update mole999 Modules & VBA 6 12-28-2008 08:52 AM




All times are GMT -8. The time now is 02:11 PM.


Microsoft Access Help
General
Tables
Queries
Forms
Reports
Macros
Modules & VBA
Theory & Practice
Access FAQs
Code Repository
Sample Databases
Video Tutorials

Sponsored Links

How to advertise

Media Kit


Powered by vBulletin®
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
(c) copyright 2017 Access World