copying paragraphs containing specific text

J

Judi

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
 
D

Dave Lett

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
 
J

Judi

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

Dave Lett said:
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

Judi said:
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
 
D

Dave Lett

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

Judi said:
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

Dave Lett said:
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

Judi said:
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
 
J

Judi

Hi Dave

Thank you , that's great exactly what i wanted to do.

Best,

Judith

Dave Lett said:
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

Judi said:
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

Dave Lett said:
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

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
 

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