repeated searching in a document that contains tables

D

demyan

Hi,
I have a macro that goes through a document and selects (and places in a new document) words written in red; please see below. (I use a Range object, and shrink range-to-be-searched after each found instance). As I discovered, the macro - its 'Range.MoveStart' bit, I believe - does not work when a word in red is found within a table. Could someone please tell me what this is about?
Thank you very much

PS. I added an iteration-counter j in the code below in order to break the infinite loop that I get without it

Sub extract(
If MsgBox("This macro will select and place in a new Word document words written in red", vbOKCancel, "Extract") = vbCancel The
En
End I
Dim rSch, rRes As Rang
Set rSch = ActiveDocument.Rang
Set rRes = rSch.Duplicat
Dim newDoc As New Documen
newDoc.Content.InsertAfter Text:=vbC
j =
D
With rRes.Fin
.ClearFormattin
.Font.Color = wdColorRe
.Format = Tru
.MatchWildcards = Tru
.Text = "<*>
.Execut
End Wit
If rRes.Find.Found And j < 10 The
j = j +
rRes.Selec
newDoc.Content.InsertAfter Text:=Selection.Text & vbC
rRes.MoveStart wdWor
rRes.End = rSch.En
End I
Loop Until Not rRes.Find.Foun
End Su
 
D

Doug Robbins - Word MVP

Use the following:

Dim source As Document, target As Document
Set source = ActiveDocument
Set target = Documents.Add
source.Activate
Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
.Font.Color = wdColorRed
Do While .Execute(FindText:="<*>", Format:=True, MatchWildcards:=True,
Wrap:=wdFindStop, Forward:=True) = True
target.Range.InsertAfter Selection.Range & vbCr
Selection.Collapse wdCollapseEnd
Loop
End With
target.Activate


--
Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
demyan said:
Hi,
I have a macro that goes through a document and selects (and places in a
new document) words written in red; please see below. (I use a Range object,
and shrink range-to-be-searched after each found instance). As I discovered,
the macro - its 'Range.MoveStart' bit, I believe - does not work when a word
in red is found within a table. Could someone please tell me what this is
about?
Thank you very much.

PS. I added an iteration-counter j in the code below in order to break the
infinite loop that I get without it.
Sub extract()
If MsgBox("This macro will select and place in a new Word document
words written in red", vbOKCancel, "Extract") = vbCancel Then
 

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