tags:

views:

517

answers:

1

I have a Word document with text in tables and text outside tables. I want to find and replace in all the text, not just the text outside the tables. The content property of the document is just the "main story" and not the tables. I don't want to iterate through all the table objects. I just want to find and replace in all the text, just like when I do it manually using the Word gui.

Please save me from the mental hell I am burning in.

[The stench of burning flesh is starting to worry me. Almost as much as the excruciating pain in my extremities.]

[My legs and arms have been devoured by the flames. I write this with my nose, which is sticky with melted plastic from the keys.]

+2  A: 

Use the Find property, it is the same as the UI. Search help topic for "Find Property" to get you started.

With ActiveDocument.Range.Find
    .Text = "hi"
    .Replacement.Text = "hello"
    .Execute Replace:=wdReplaceAll, Forward:=True, Wrap:=wdFindContinue
End With
AMissico
Thanks, but what is "Selection"? I assume it's a property of something, but of what?
I believe Selection is basically any thing that is selected. For example, selected text or selected objects.
Shivasubramanian A
Ok, how do I select the entire document, including the text in tables and the text outside tables, all in one go?
You do not have to select anything. This is just example code to show you how to use the Find object.
AMissico
ActiveDocument.Range.Find...
AMissico
I'm grateful for your contribution, but have you answered this question: "How to find and replace programmatically in ENTIRE Word document" ?Surely if I don't select something then I don't have a Selection object!?
ActiveDocument.Range is (for me anyway) a method requiring start and end parameters. Can you suggest values can I use?
ActiveDocument.Content.Find
AMissico
ActiveDocument.Content is just the text (or "main story" as MSDN refers to it). It doesn't include text in tables.
It does include text in tables.
AMissico
Execute returns False.From http://msdn.microsoft.com/en-us/library/aa196075%28office.11%29.aspx "Content Property":"Returns a Range object that represents the main document story"
Sadly that still doesn't work. It replaces text outside tables but not inside tables.
ActiveDocument.Range, as in the answer code, is (for me anyway) a method requiring start and end parameters. Please could you suggest values to use?
I have tested the sample code in the answer with a simple document that contains one sentence and another sentence in a table, and it works. Try using the Find property on a simple document, and play around with the parameters.
AMissico