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
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