B
Benjamino5
The goal of this sub is to find every paragraph with the style "EssayAnswer."
If there are multiple EssayAnswer paragraphs, it combines them by replacing
the paragraph marks with spaces.
The problem is, after it works through the whole document, combining every
batch of EssayAnswer paragraphs, the Find continues from the beginning, ad
nauseum.
I don't know how to get out of the Find after I've gone through the whole
document once. I'm not too familiar with the Find object, so I've probably
missed something simple. Here's the code. Please note that it's actually
taken from VB.Net, but the only real difference is the extraneous sets of
parentheses.
Thanks a lot!
Ben
_____________________________
Sub OneLineMetadata()
' puts all metadata on a single line instead of one para per item
Dim r As Word.Range
Dim rToChange As Word.Range
Dim paraNext As Word.Paragraph
Dim f As Word.Find
' adoc is a variable that refers to the ActiveDocument
r = adoc.Range
f = r.Find
With f
.ClearFormatting()
.Style = "EssayAnswer"
.Forward = True
.Wrap = 0
.Execute()
Do While .Found
If r.Paragraphs.Last.Range.End <>
adoc.Range.Paragraphs.Last.Range.End Then
paraNext = r.Paragraphs.Last.Next
While paraNext.Style.namelocal = "EssayAnswer"
r.MoveEnd(Unit:=4, Count:=1) ' wdParagraph
paraNext = r.Paragraphs.Last.Next
End While
' now assign current r to a new variable and modify that
to exclude
' the final para mark, which I want to keep
rToChange = adoc.Range(Start:=r.Start, End:=r.End)
With rToChange
.MoveEnd(Unit:=1, Count:=-1) ' wdCharacter
.Find.Execute(FindText:="^p", ReplaceWith:="
", Replace:=2)
' 2 is wdReplaceAll
End With
End If
.Execute()
Loop
End With
End Sub
If there are multiple EssayAnswer paragraphs, it combines them by replacing
the paragraph marks with spaces.
The problem is, after it works through the whole document, combining every
batch of EssayAnswer paragraphs, the Find continues from the beginning, ad
nauseum.
I don't know how to get out of the Find after I've gone through the whole
document once. I'm not too familiar with the Find object, so I've probably
missed something simple. Here's the code. Please note that it's actually
taken from VB.Net, but the only real difference is the extraneous sets of
parentheses.
Thanks a lot!
Ben
_____________________________
Sub OneLineMetadata()
' puts all metadata on a single line instead of one para per item
Dim r As Word.Range
Dim rToChange As Word.Range
Dim paraNext As Word.Paragraph
Dim f As Word.Find
' adoc is a variable that refers to the ActiveDocument
r = adoc.Range
f = r.Find
With f
.ClearFormatting()
.Style = "EssayAnswer"
.Forward = True
.Wrap = 0
.Execute()
Do While .Found
If r.Paragraphs.Last.Range.End <>
adoc.Range.Paragraphs.Last.Range.End Then
paraNext = r.Paragraphs.Last.Next
While paraNext.Style.namelocal = "EssayAnswer"
r.MoveEnd(Unit:=4, Count:=1) ' wdParagraph
paraNext = r.Paragraphs.Last.Next
End While
' now assign current r to a new variable and modify that
to exclude
' the final para mark, which I want to keep
rToChange = adoc.Range(Start:=r.Start, End:=r.End)
With rToChange
.MoveEnd(Unit:=1, Count:=-1) ' wdCharacter
.Find.Execute(FindText:="^p", ReplaceWith:="
", Replace:=2)
' 2 is wdReplaceAll
End With
End If
.Execute()
Loop
End With
End Sub