Replacing Nested Text

M

mamue

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
 
J

Jean-Guy Marcil

mamue said:
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
 
M

mamue

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
 
J

Jean-Guy Marcil

mamue said:
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?
 
M

mamue

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.
 
J

Jean-Guy Marcil

mamue said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top