Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Replacing Nested Text

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
mamue - 09 May 2008 10:43 GMT
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
Jean-Guy Marcil - 09 May 2008 15:11 GMT
> Hi there,
>
[quoted text clipped - 9 lines]
> 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
mamue - 13 May 2008 08:57 GMT
On 9 Mai, 16:11, Jean-Guy Marcil
<JeanGuyMar...@discussions.microsoft.com> wrote:
> > Hi there,
>
[quoted text clipped - 53 lines]
>
> 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
Jean-Guy Marcil - 13 May 2008 13:37 GMT
> 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?
mamue - 14 May 2008 08:06 GMT
On 13 Mai, 14:37, Jean-Guy Marcil
<JeanGuyMar...@discussions.microsoft.com> wrote:
> > Hi Jean-Guy,
>
[quoted text clipped - 9 lines]
>
> 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.
Jean-Guy Marcil - 14 May 2008 13:35 GMT
> <JeanGuyMar...@discussions.microsoft.com> wrote:
> >
[quoted text clipped - 3 lines]
> 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?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.