A Find that never ends

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
 
H

Helmut Weber

Hi Benjamin,

much too complicated.
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.

Except the last paragraph mark in a sequence of paragraphs
of format "xxx", I assume.

So to speak, combine all consecutive paragraphs of style "xxx"
into one paragraph.

Sub Macro11()
Dim rTmp As Range
Set rTmp = ActiveDocument.Range
With rTmp.Find
.Style = "Normal"
While .Execute
If Not rTmp.Paragraphs.Last.Next Is Nothing Then
If rTmp.Paragraphs.Last.Next.Style = "Normal" Then
rTmp.Characters.Last = " "
End If
rTmp.Collapse direction:=wdCollapseEnd
rTmp.End = ActiveDocument.Range.End
End If
Wend
End With
End Sub

Which might need purging of the document's end.
See: http://tinyurl.com/2d2ocy

HTH

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
B

Benjamino5

Helmut,

Thanks for the vastly-simpler, better code! I do have one more problem to
solve: rTmp.Characters.Last is interpreted in VB.Net as a "read-only
property" so I can't set it to " ".

I can probably create a new variable and assign the value to it, but my
attempts so far haven't been successful. I'll investigate and post my results
later, if I can get it to work.

Ben
 
J

Jean-Guy Marcil

Benjamino5 was telling us:
Benjamino5 nous racontait que :
Helmut,

Thanks for the vastly-simpler, better code! I do have one more
problem to solve: rTmp.Characters.Last is interpreted in VB.Net as a
"read-only property" so I can't set it to " ".

I can probably create a new variable and assign the value to it, but
my attempts so far haven't been successful. I'll investigate and post
my results later, if I can get it to work.

Try to set a range to rTmp.Characters.Last :

Dim rgeTemp as Range

Set rgeTemp = rTmp.Characters.Last
rgeTemp.Text = " "

Does this work?

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 

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