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