Forum Home
Press F1
 
Thread ID: 66755 2006-03-06 10:40:00 find+replace 1000 same words with 1000 unique words superoman (6703) Press F1
Post ID Timestamp Content User
435956 2006-03-06 10:40:00 Hi

We have a file that has about 1000 words that are all the same. The word is BAM

We need to replace the word BAM with a word from a list of 1000 different words

ie change the:

first BAM to DOG
second BAM to CAT
the third BAM to BUTTON

etc

The words are spread throughout a document

Is there a way of doing this automatically?- we do not want to type the 1000 words in individually by hand.

Thanks if you can help.
superoman (6703)
435957 2006-03-06 10:59:00 Interesting challenge. Can you tell us what programme/s you have, eg MS Word etc? Greg (193)
435958 2006-03-06 12:00:00 Basic means, the 1000 unique words should be stored in an array, which can be achieved from the file it's stored in. Then all that would be required is string replacement with the pattern searching for BAM and the replacement being the array in which it would iterate over each array item replacing the word BAM.

Not sure if this is related to programming, or just something you need done, but it's possible none the less.


Cheers,


KK
Kame (312)
435959 2006-03-06 20:10:00 Can be done with VBA using a list in either a Word table, Excel, Access or a text file. Probably easiest in Excel. You would need to be certain of the order of the array and you would need to consider what happens for partial matching. eg will it be BAM then space or will it change BAMBINI into DOGBINI using the word DOG as an example. Parry (5696)
435960 2006-03-07 00:34:00 I tried to think of a way this could be done without programming. While many tools will do a global search and replace, I can't think of any that'll stop after the first replacement.

It looks like some form of programming would be in order. VBA would seem to be it.
kingdragonfly (309)
435961 2006-03-07 00:53:00 Im too busy at work but if I have a chance tonight I'll post an example. Parry (5696)
435962 2006-03-07 00:58:00 Hi

Have access to most programs word excel etc

Partial matching wont be a problem because the words are all unique in both files i think

Hopefully someone can come up with a simple solution, Im not into programming, however this is a job for someone else so they might know

Someone suggested mail merge but we couldn't find a replece sequence field or something similar. My mail merge skills iz zero

Thanks for your help
superoman (6703)
435963 2006-03-07 05:24:00 Hi, hopefully this will work for you. A couple of things to be aware of before you run the macro...

1) Macros cannot be undone. Once you run them then the changes are permanent. You could of course close Word without saving and the file will remain untouched. I strongly suggest you make a copy of the Word doc and use the copy as your target file.
2) Im making the assumption that the file is C:\testdoc.doc ... replace this filepath with that of your file.
3) Ensure that your target Word doc is closed before running the macro

The following code is to be copied and pasted into a module in Excel.
1. Open the Visual Basic Editor (ALT+F11 or Tools|Macro|Visual Basic Editor from the menu)
2. Select Insert|Module from the menu
3. Paste the code in the right-hand window
4. Close the Visual Basic Editor (ALT+Q or File|Close and return to Microsoft Excel from the menu)

Now copy your text of 1000 words or so into Column A, starting from A1.

To run the macro you may need to tone down the macro security first. To alter security settings select Tools|Macro|Security then amend the option to Medium then save, close and re-open the Excel file and select the Enable Macro button.

To run the macro select Tools|Macro|Macros and the text ReplaceWords should be highlighted then select Run.


Sub ReplaceWords()
Dim Rng As Range, c As Range, MyDoc As Object, Wd As Object, MyRange

'Set a range representing list of text that will replace word doc value of BAM
Set Rng = Range("A1:A" & Range("A65536").End(xlUp).Row)

'Open Word and make word visible
Set Wd = CreateObject("Word.Application")
Wd.Visible = True

'Open the file. Change this path to suit.
Set MyDoc = Wd.Documents.Open("C:\testdoc.doc") 'change this to equal your path

'Loop though all the values in column A
For Each c In Rng
'set a range in the Word doc
Set MyRange = ActiveDocument.Content
'Look for text BAM
MyRange.Find.Execute FindText:="BAM", Forward:=True
'If found then replace BAM with text in Excel cell
If MyRange.Find.Found = True Then MyRange.Text = c

Next c 'move onto the next cell

'Release objects from memory
Set Wd = Nothing
Set Rng = Nothing
Set c = Nothing
Set MyRange = Nothing
Set MyDoc = Nothing

End Sub
Parry (5696)
435964 2006-03-07 22:44:00 thanks parry for your help,

however the person doing the replace Tried it and got...

Run-time error 424 Object required

On clicking the Debug option this line of the macro is highlighted:

Set MyRange = ActiveDocument.Content

I have of course edited the macro but the only changes I have made were changing the path to the Word file and changing BAM (which was an example only) to the actual text I have in the Word file

Any ideas how to fix?
superoman (6703)
435965 2006-03-08 00:38:00 Sorry, needs wd infront. Replace that line with...

Set MyRange = Wd.ActiveDocument.Content
Parry (5696)
1 2