PDA

View Full Version : Find & replace multiple words



Dharma
01-10-2008, 04:33 AM
Hi, I have never programmed with VB or VBA (though I have done some basic programming in other languages). I'd really appreciate if anyone could point me in the right direction with what I want to do.

I have some words in a word document with generic names like {category 1}..{category n} in curly brackets.

I want to write a VBA macro script that can display all the generic words that are used wih the word document and if possible, return fields which would give me an option to replace them with something else.

Any ideas anyone on how to get started on this?

Also, could someone point me to a good first book for VBA.

Cheers in advance.

fumei
01-10-2008, 11:08 AM
I hate to say it, but the "Dummies" book for VBA can at least get you started, sort of. The best thing is to actually do some. Record macros, and look at the code. Help is much improved over what it used to be. Use it . The F1 key is your friend.

Regarding your intention, you would need to fully state your logic.

"I want to write a VBA macro script that can display all the generic words that are used wih the word document and if possible, return fields which would give me an option to replace them with something else. "

What does "display all the generic words" actually mean?

Say...{category 1}... OK.

Say, there are 14 instances of "{category 1}". The quotation marks mean it is a string literal. {category 1} is real text in the document, and there are 14 instances of that string. OK.

So your code runs along. It finds the first one of {category 1}. Can do.

Now what? You want to display some sort of dialog to the user saying...HEY! here is a {category 1}. Whaddaya want to do?

It is a question of precise logic. As it usually is.

The user can make four choices, logically speaking.

1. Nothing, but if there are OTHER {category 1}, I want to decide about those separately.

2. Nothing. I don't give a crap about ANY {category 1}. Forget about {category 1}, I want {category 2}.

3. Change this to "yaddaYadda". In fact, hey, save me some time wil you? Change ALL {category 1} to "yaddaYadda". Thanks.

4. Change this to "yaddaYadda". If there are OTHER {category 1}, I may...or I may not...want to change those, so keep it coming.

Each one of these scenarios requires different coding, which makes sense. Each scenarios does different things, right?

Could all of those be handled? Sure, absolutely. VBA can do pretty much everything you ask. The trick is figuring out what it is EXACTLY you want it to do.

fumei
01-10-2008, 11:11 AM
Hmmm. Maybe I did not phrase that very well.

Is there, could there, be only ONE example of a specific text? Say {category 1}.

That is actually where your logic starts. Although is does come back to your code finding that first (and possibly only) {category 1}.

What, exactly, do you want to happen?

Dharma
01-11-2008, 10:52 AM
I'm sorry if it wasn't clear. I'll try again.

I have a template with many words that need to be replaced each time. Some of the words are replicated on different pages (say {word1} can appear on page 1 and page 2).

So this is what I want it to do:

1. Scan through word document and return all the words that are in braces (eg. {word1}, {word2}... {wordn}).

2. Provide me with a facility to change all instances of these words in braces.

I hope that's a tad clearer?

Thanks

fumei
01-14-2008, 12:43 PM
Nope, still unclear, try again.

You stated that {word 1} can appear twice, say on page 1, and on page 2. I have already asked this....What, exactly, do you want to happen?

I really do not know how to express better than that. Exactly means...exactly. Try again. Here is your #1.

"1. Scan through word document and return all the words that are in braces (eg. {word1}, {word2}... {wordn})."

What does that mean, even in the example you give. {word 1} is on two pages. What does "return" mean????? Are you going to "return" {word 1} once....or twice?

You must state - even in your mind - EXACTLY what it is you want to happen. I truly suggest you write it out, on paper. Write out precisely, and I really really mean precisely, the logic.

I don't know what you mean by return. And I don't know whether to return a "yes" (I found {word 1}) once, or twice.

Is the sequence of search parameters - {word 1}, {word 2}....{word n} known. Do you know what n may be?

Here is an example, that may, or may not help - as I still do not know what it is , exactly, what you want to happen.Sub CountEm()
Dim myWords()
Dim r As Range
Dim j As Long
Dim var
Dim msg As String
myWords = Array("{word 1}", "{word 2}")
Set r = ActiveDocument.Range
For var = 0 To UBound(myWords)
With r.Find
Do While .Execute(FindText:=myWords(var), Forward:=True) _
= True
j = j + 1
Loop
msg = msg & "The string " & myWords(var) & _
" found " & j & " times."
Set r = ActiveDocument.Range
j = 0
End With
msg = msg & vbCrLf & vbCrLf
Next
MsgBox msg
End SubI realize this is NOT what you want, but it may push you off in the direction you want to go.

The code has an array (for simplicity, an array of the strings {word 1}, and {word 2}.

It goes through the document, and "returns" a message with the count of found instances of {word 1}, and {word 2}.

In my test document, I have - as you mentioned - {word 1} on both page 1, and on page 2. It is in the document two times.

{word 2}, on the other hand I put in 15 times, scattered all over the place.

The messagebox displays:

The string {word 1) found 2 times.

The string {word 2} found 15 times.

Here is your #2.

"2. Provide me with a facility to change all instances of these words in braces. "

What does that mean? I have asked, I will ask again. What exactly?

OK. Just for laugh, this is what I did in my test doc. I added {word x} with x = 10. So my test doc is sprinked with {word 1 to 10}.

I used the code above to dump all instances of founds, into a userform combobox. Then I could select whatever one I wanted, say [word 7}, click a Delete button. That button would go through the document deleting all instances of {word 7}.

This stuff is really not all that hard. The coding part really isn't. If the logic part is there, then the coding can be done. I really can not stress this enough. The actual coding is secondary. You can write all the fancy-smancy code you like, but if it does not work well logically, then it does not work.

Look at the code posted above. There is nothing difficult at all. The entire power lies in the logic, along with some understanding of basic objects within Word. Range for one.

Let me see if I can spell it out, in logic. The example above, not my expanded one using a userform.

1. declare variables
2 set string array
3 set range object
4. loop through each word in the array
5. use the range to find each word
6 if found, increment a counter
7 add to a message string: the current search word (from the array), and the counter of founds for it
8 reset the range object back to the whole document
9 reset the counter to 0
10 go to the next word

11 - assume not needed for production: display message


In my simple example, it searchs (and counts):
{word 1}
{word 2}

But it could as easily action:

{word 1}
{word 2}
{word 3}
{word 4}
{word 5}
{word 6}