Hi
I am hoping someone can help me out.
I am trying to write some code that loops through all the text in a word
document and when it finds the text "[R1]" it copies the entire paragraph
where that text is contained to another location.
I’ve amended some code from the MS knowledgebase. I started out simply just
trying to get the text selected and formatted but the code isn’t selecting
the right text.
Sub EditFindLoopExampleA()
With ActiveDocument.Content.Find
.ClearFormatting
.Text = "[R1]"
Do While .Execute(Forward:=True, Format:=True) = True
With .Parent
'If the found text is the last
' paragraph in the document...
If .End = ActiveDocument.Content.End Then
With Selection
'I would expect this code to select the
current paragraph where
'the text had been found so i could format or
copy it
.StartOf Unit:=wdParagraph, Extend:=wdMove
.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
.Font.Color = wdColorRed
End With
Exit Do
'If the found text is *not* the last
' paragraph in the document...
Else
With Selection
'I would expect this code to select the
current paragraph where
'the text had been found so i could format or
copy it
.StartOf Unit:=wdParagraph, Extend:=wdMove
.MoveDown Unit:=wdParagraph, Count:=1,
Extend:=wdExtend
.Font.Color = wdColorRed
End With
End If
End With
'Goes back to the beginning of the Do...Loop statement.
Loop
End With
End Sub
Has any one got any suggestions?
Thanks
Judi
Dave Lett - 12 Jul 2005 17:54 GMT
Hi Judi,
You didn't specify in your post WHERE the other location is that you want to
copy the current paragraph to. Instead of using the selection object to
select the entire paragraph, you probably want to use ranges so that 1) you
don't have to make sure that you move to the end of the currently selected
R1 paragraph and 2) your routine will run faster. Once I know where you want
to copy the existing material, then I can show you a sample.
HTH,
Dave
> Hi
>
[quoted text clipped - 56 lines]
>
> Judi
Judi - 12 Jul 2005 23:06 GMT
Hi Dave
Thanks for the rapid response. I want to create a summary of the tagged
data and I intend to copy it to a bookmarked location at the beginning of the
document and every time i append a paragraph i will change the bookmark so
everything gets appended to the end. I hope thats enough information.
Thanks
Judi
> Hi Judi,
>
[quoted text clipped - 68 lines]
> >
> > Judi
Dave Lett - 13 Jul 2005 15:08 GMT
Hi Judi,
I've experimented in my environment (Word 2003 and NT) and it seems to work.
With this method, there was really no need to test if the found item was in
the last paragraph or not. Hope this helps.
Dim oRngToCopy As Range
Dim oRngBookmark As Range
Set oRngBookmark = ActiveDocument.Bookmarks("CopyTo").Range
oRngBookmark.Start = oRngBookmark.End
With Selection
.HomeKey Unit:=wdStory
With .Find
.Text = "[R1]"
.Forward = True
Do While .Execute
Set oRngToCopy = Selection.Paragraphs(1).Range
oRngBookmark.FormattedText = oRngToCopy.FormattedText
oRngBookmark.Start = oRngBookmark.End
Loop
End With
End With
HTH,
Dave
> Hi Dave
>
[quoted text clipped - 89 lines]
>> >
>> > Judi
Judi - 14 Jul 2005 17:19 GMT
Hi Dave
Thank you , that's great exactly what i wanted to do.
Best,
Judith
> Hi Judi,
>
[quoted text clipped - 117 lines]
> >> >
> >> > Judi