Hi there,

i wonder how fast the normal word replace function (Edit->Find/
Replace) is able to replace nested text in text boxes or tables. i
want to automatically do this for text in tables within text boxes
(Textbox->Table->TableCell->Text). the only approach i had so far is
to
- iterate through each Shape (and if it's a textbox)
- select the table within and
- replace the text

and this costs me a lot of time. the macro recorder function didn't
show me the exact find/replace method.

do you have any idea?

regards, matthias

RE: Replacing Nested Text by JeanGuyMarcil

JeanGuyMarcil
Fri May 09 07:11:01 PDT 2008

"mamue" wrote:

> Hi there,
>
> i wonder how fast the normal word replace function (Edit->Find/
> Replace) is able to replace nested text in text boxes or tables. i
> want to automatically do this for text in tables within text boxes
> (Textbox->Table->TableCell->Text). the only approach i had so far is
> to
> - iterate through each Shape (and if it's a textbox)
> - select the table within and
> - replace the text
>
> and this costs me a lot of time. the macro recorder function didn't
> show me the exact find/replace method.
>

You cannot use the simple Replace/Find in this case. Since you want to
target tables inside textboxes, you need more code. And it will take some
time to execute depending on the document content.

Here is some code to get you going:

Option Explicit

Sub test()

Dim docMain As Document
Dim rngTable As Range
Dim i As Long
Dim j As Long

Set docMain = ActiveDocument

With docMain
If .Shapes.Count > 0 Then
For i = 1 To .Shapes.Count
With .Shapes(i)
If .Type = msoTextBox Then
If .TextFrame.HasText Then
With .TextFrame.TextRange.Tables
If .Count > 0 Then
For j = 1 To .Count
Set rngTable = .Item(j).Range
With rngTable.Find
.Text = "cat"
.Replacement.Text = "Bird"
.Execute Replace:=wdReplaceAll
End With
Next
End If
End With
End If
End If
End With
Next
End If
End With

End Sub

Re: Replacing Nested Text by mamue

mamue
Tue May 13 00:57:22 PDT 2008

On 9 Mai, 16:11, Jean-Guy Marcil
<JeanGuyMar...@discussions.microsoft.com> wrote:
> "mamue" wrote:
> > Hi there,
>
> > i wonder how fast the normal word replace function (Edit->Find/
> > Replace) is able to replace nested text in text boxes or tables. i
> > want to automatically do this for text in tables within text boxes
> > (Textbox->Table->TableCell->Text). the only approach i had so far is
> > to
> > - iterate through each Shape (and if it's a textbox)
> > - select the table within and
> > - replace the text
>
> > and this costs me a lot of time. the macro recorder function didn't
> > show me the exact find/replace method.
>
> You cannot use the simple Replace/Find in this case. Since you want to
> target tables inside textboxes, you need more code. And it will take some
> time to execute depending on the document content.
>
> Here is some code to get you going:
>
> Option Explicit
>
> Sub test()
>
> Dim docMain As Document
> Dim rngTable As Range
> Dim i As Long
> Dim j As Long
>
> Set docMain = ActiveDocument
>
> With docMain
> If .Shapes.Count > 0 Then
> For i = 1 To .Shapes.Count
> With .Shapes(i)
> If .Type = msoTextBox Then
> If .TextFrame.HasText Then
> With .TextFrame.TextRange.Tables
> If .Count > 0 Then
> For j = 1 To .Count
> Set rngTable = .Item(j).Range
> With rngTable.Find
> .Text = "cat"
> .Replacement.Text = "Bird"
> .Execute Replace:=wdReplaceAll
> End With
> Next
> End If
> End With
> End If
> End If
> End With
> Next
> End If
> End With
>
> End Sub

Hi Jean-Guy,

my question was not how to replace the text, but how the normal manual
find/replace function does the same job in a far less time. i already
wrote a code similar to yours but it costs to much time when
processing a large document

Re: Replacing Nested Text by JeanGuyMarcil

JeanGuyMarcil
Tue May 13 05:37:01 PDT 2008

"mamue" wrote:

> Hi Jean-Guy,
>
> my question was not how to replace the text, but how the normal manual
> find/replace function does the same job in a far less time. i already
> wrote a code similar to yours but it costs to much time when
> processing a large document

This is what I wrote... The first line in my reply stated: "You cannot use
the simple Replace/Find in this case". The "this case" refers to the fact
that you wrote to explain that you wanted to replace text in tables that are
only found in textboxes.

You mention the macro recorder. What did you do with the recorder?

Re: Replacing Nested Text by mamue

mamue
Wed May 14 00:06:05 PDT 2008

On 13 Mai, 14:37, Jean-Guy Marcil
<JeanGuyMar...@discussions.microsoft.com> wrote:
> "mamue" wrote:
> > Hi Jean-Guy,
>
> > my question was not how to replace the text, but how the normal manual
> > find/replace function does the same job in a far less time. i already
> > wrote a code similar to yours but it costs to much time when
> > processing a large document
>
> This is what I wrote... The first line in my reply stated: "You cannot use
> the simple Replace/Find in this case". The "this case" refers to the fact
> that you wrote to explain that you wanted to replace text in tables that are
> only found in textboxes.
>
> You mention the macro recorder. What did you do with the recorder?

I used the macro recorder to see what Word does when replacing the
text in a far less time. The text was replaced correctly but the
recorded code didn't work when i called it from the Word editor.

Re: Replacing Nested Text by JeanGuyMarcil

JeanGuyMarcil
Wed May 14 05:35:01 PDT 2008

"mamue" wrote:

> <JeanGuyMar...@discussions.microsoft.com> wrote:
> >
> > You mention the macro recorder. What did you do with the recorder?
>
> I used the macro recorder to see what Word does when replacing the
> text in a far less time. The text was replaced correctly but the
> recorded code didn't work when i called it from the Word editor.

It is true that the Built-in Find/Replace is much faster than paragraph
iteration.

That being said, I am really confused here.
In your original post, you wrote, and I quote:

"i wonder how fast the normal word replace function (Edit->Find/
Replace) is able to replace nested text in text boxes or tables. i
want to automatically do this for text in tables within text boxes
(Textbox->Table->TableCell->Text). the only approach i had so far is
to
- iterate through each Shape (and if it's a textbox)
- select the table within and
- replace the text
"

This query means that you want to replace a certain string of text, but only
for instances that occur within tables that are inside a text box.

This cannot be done with the the built-in "Find/Replace" (Edit > Replace...).

So, again, how did you use the macro recorder to achieve this?