PDA

View Full Version : Using .Find and .Replace



MWE
02-21-2006, 07:44 AM
Yes, yet another question about Word ...

I am runnning Word2000. I am using a .Find / .Replace combination to find certain text in a document and replace it with something else. Actually, I am using the approach to tweak the format of the text string. With ActiveDocument.Content.Find
.Font.Bold = True
.Text = "TBD"
With .Replacement
.Font.Bold = True
.Text = "TBD"
.Font.Italic = True
.Font.ColorIndex = wdBlue
End With
.Execute Format:=True, Replace:=wdReplaceAll
End With This approach works quite well but there is an interesting anomoly. It seems to ignore text in tables.

What am I doing wrong?

Thanks

TonyJollans
02-21-2006, 08:49 AM
Hi MWE,

First off, Find and Replace does work in tables :)

There are several possible reasons - the obvious one is: do you have bolded matching strings in your table(s)?

Assuming you do have, then you must look at all the options you haven't used. The trouble with using Find is that it is (sort of) persistent. For example, you are not specifying where, or in which direction, to search, so it is using a remembered setting which may not be the one you want. Nor are you clearing any other formatting which might previously have been set, etc.

Record yourself doing a F&R for anything (unformatted) just to get some basic code to control all the settings.

MWE
02-21-2006, 03:19 PM
Hi MWE,

First off, Find and Replace does work in tables :)

There are several possible reasons - the obvious one is: do you have bolded matching strings in your table(s)?

Assuming you do have, then you must look at all the options you haven't used. The trouble with using Find is that it is (sort of) persistent. For example, you are not specifying where, or in which direction, to search, so it is using a remembered setting which may not be the one you want. Nor are you clearing any other formatting which might previously have been set, etc.

Record yourself doing a F&R for anything (unformatted) just to get some basic code to control all the settings.
Thanks for the leads. I will look into direction, but isn't Activedocument.content the whole document? How do I tell .Find to "not remember" anything?

I would like to use the macro recorder but I find that (unlike Excel) virtually nothing works when I turn on the recorder. :banghead:

TonyJollans
02-21-2006, 05:18 PM
'fraid you can't tell Find not to remember - although I'm never entirely sure what anything other than Selection.Find actually does remember - you really need to be explicit about it all the time.

The mouse not working during recording is a bit of a nuisance but nearly everything is possible without the mouse so it's not such a huge problem when you get used to it. I have no idea why it works as it does - it's one of life's little mysteries.

fumei
02-26-2006, 06:48 PM
I am not sure what it is you do want to not remember. As Tony says, Find does work in Tables. Could you clarify what the problem is?

mdmackillop
02-27-2006, 11:41 AM
Try preceding your code with the following. It ensures you start from the beginning and gets rid of any previous formatting searches.

Selection.HomeKey Unit:=wdStory
Selection.Find.ClearFormatting