Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Auto Replace In Word 2000

Status
Not open for further replies.

Mictain

MIS
Jul 4, 2001
79
0
0
GB
Hi All,

Is there a way via Macro to replicate the 'replace all' function in Word?

We have a template where six or so areas of variable text "AA", "BB", "CC" etc appear in about a dozen places throughout the document, and I'd like it so that whenever "AA" is modified then all the "AA" areas change at the same time, and so on.

Thanks,

Neil.
 
Hi Mictain,

Have you tried recording it?

I just did and it gave me this:

Code:
[blue]Sub Macro2()
'
' Macro2 Macro
' Macro recorded 12/10/2004 by Tony
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "dog"
        .Replacement.Text = "wolf"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub[/blue]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Hi Tony,

Thanks for that. I dropped it into the sheet and it does replace all, but the only trouble is - and my title probably didn't help - I don't have a definitive replacement text to use.

The text will actually be a variable name, so in effect when "AA" changes, I need it to replicate whatever name is inputted there across the rest of the "AA" areas as well. I suppose I should've spoken about a 'real time multiple copy' macro.

Aren't us newbies a pain? [sad]

Any tweaks to make this work would be gratefully received.

Thanks again,

Neil.
 
Hi Neil,

We were all newbies once.

It's easy enough to use variables instead of literals but where are the values coming from? I don't entirely follow what you want to do. Are you asking for an automatic document-wide replace when a single instance is (manually?) replaced? Or do you have all your values in code already? Or what?

Us old-timers can be a pain too - always asking for more detail [wink]

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Hello again Tony,

Okay, I'll see if I can get the info right this time [smile]

Yes, you're correct in your assumption, but I shall try and explain it just to be sure.

The text "AA" is placed in several areas throughout the document. Someone will type in a name manually in place of the first "AA". (This name will be different for each document). They then use the 'replace all' feature to switch "AA" for the name for the rest of the document.

I would like to have a macro which runs by default that automatically copies the name, once inputted, and replaces all "AA"s throughout the rest of the document with it.

Current Style:

"AA" "AA" "AA"
Type 'Fred Bloggs' in initial "AA"
"Fred Bloggs" "AA" "AA"
Replace all "AA" for 'Fred Bloggs'
"Fred Bloggs" "Fred Bloggs" "Fred Bloggs"

Would like

"AA" "AA" "AA"
Type 'Fred Bloggs' in initial "AA"
"Fred Bloggs" "Fred Bloggs" "Fred Bloggs"

Hope that helps [bigsmile]

Thanks,

Neil.
 
Hi Neil,

Sorry, but more questions ...

For this to happen, your macro has to have some way of identifying "Fred Bloggs" as being the text it wants, after the change has been made.

I don't suppose you're using a Word Form for this, are you?

If not you could use a bookmark but there's always a danger that the user will destroy it.

Or you could prompt the user for the name - either with a simple input box, or a complete Userform.

Or is there some other way of identifying the text - for example is it always at the start of a page?

I just reread your first post - is this something that always happens when a new document is created from your template - or might you want to run it at other times?

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Hi Tony,

That's okay. I don't mind questions if they get me closer to an answer [smile]

I didn't actually create the document, I'm just trying to tweak it! It's a standard Word doc with the "AA" text appearing either in a cell as part of a paragraph, or simply on the sheet as normal text.

There are actually 8 sets of text that could need replicating, but they all appear in set positions on the first page.

Maybe an input box would be the easiest thing without starting the document again from scratch, so there's a reference to pick up on for each text set? I've only ever played with VB within Excel so I don't know if this is a fair comment?

Not quite sure on your parting shot, there, Tony. I'd like the macro to be activated as the document is opened, so I could type in a name, it would replicate, I could then change the name, it would replicate, but if I closed the document then reopened it it would still show the last inputted name. Is that what you were after?

Going back to the input box, I presume this would simply be a box for each text set on the front page which provides the reference for replication?

I've probably made things as clear as mud now!

Hope there's something worthwhile in my rambling. Ugh. Need a break from the screen [morning]

Thanks,

Neil.
 
Code:
Sub ChangeText()
Dim strTextOut As String
Dim strTextIn As String
strTextOut = InputBox("Please enter the text to be changed.")
strTextIn = InputBox("Please input the new (replacment) text.")
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = strTextOut
        .Replacement.Text = strTextIn
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

This is your basic Selection, Find and Replace. However, it is MUCH better to use the Selection object as little as possible. Why? Because resources are used to show each an every selection. Where possible, it is better to use a Range object which does the same thing, but never actually interacts with the user interface until it is done.. Here is the same thing using a Range.

Code:
Sub ChangeText()
Dim strTextOut As String
Dim strTextIn As String
strTextOut = InputBox("Please enter the text to be changed.")
strTextIn = InputBox("Please input the new (replacment) text.")

With ActiveDocument.Content.Find
    .ClearFormatting
    .Execute FindText:=strTextOut, ReplaceWith:=strTextIn, Format:=True, _
        Replace:=wdReplaceAll
End With

Finally, this is the simple way. But it does use two input boxes, which is a bit of a drag. If tghe existing text is bookmark, you could build a UserForm that displays all bookmarks, with a textbox beside it. Select a bookmark, enter the text and click a Go button.

Hope this helps.

Gerry
 
Hi Neil,

Sorry - I've been away this afternoon. Your problem seems to be that the text to be replaced has no identifying features and so must be prompted for. If you've got to prompt for the text and its replacement I don't see any great advantage in running code - might as well just pres Ctrl+H and get the dialog which needs the same information.

I'd still like some clarification. [smile]

You talk of a template but I think what you actually have is a document which you use as a base and fill in the blanks, so to speak. You copy your blank document and change the copy (or you edit the blank and then do a Save As).

Firstly, is this correct?

If No, I'm not sure where we can take this.

If Yes, do you have control over the document? Could you (a) change it to a proper Word template (a .dot file) and/or (b) add bookmarks to it (or does it already have bookmarks)?

If Yes to either of those, then you can - as Gerry says - build a userform to handle it all. If yes to both of those, then the world's your oyster and you can run it automatically first time.

If you do have control and it's not sensitive, perhaps you could send me a copy of the doc - (my handle here) at VBAExpress dot com.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
 
Hi Gerry, thanks for that. I'll have to play with the InputBox route. I don't have to actually enter the info so two boxes is fine by me [thumbsup2]

Hi Tony, clarification ahead [smile]

Yes, sorry, a document as a base which will end up going through the 'save as' route.

Yes, I should be able to change it to a proper Word template. No, it doesn't have any bookmarks at present.

Looks like I'll have to play with UserForms and/or Boxes then. If I get stuck, you might get something through your electronic post medium, Tony [bigsmile]

Thanks again,

Neil.
 
Mind if I add a twist to this... I have searched elsewhere and have not found a solution.

How would I use the code above to replace or should I say place [carriage returns] using the find replace...

I know I want [cr] after every ', ' {comma space} and the ASCII code for [cr]=13

Thanks


Sid from Minnesota
 
Hi Sid,

To put a carriage return in a Find or Replace string, use [blue]^p[/blue] or [blue]^13[/blue]. You cannot use ^p in the find string if you are using wildcards - other than that I believe they are interchangeable.

Enjoy,
Tony

--------------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.
Excel VBA Training and more Help at [url=http://www.vbaexpress.
 
I found by using chr(13) it worked. Thanks for the tip, I will try that also.



Sid from Minnesota
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top