replace paragraphmarks

F

Flip

Hi NG
I have a doc with several paragraphs in two different styles.
Style 1 is ok, always one line of text.
Style 2 follows the para in Style 1, but often there are 2 to 5 more lines
of text. Unfortunately every line is closed by a paragraphmark.

I would like to group these lines to one paragraph (so that other formatting
gets easier).
A Find and Replace paragraphmark by space, within the style, does not work
because if the last mark is replaced, the folowing Style 1 paragraph is
altered to Style 2. This is called logic I believe :)

So what should I do?
Flip
 
H

Helmut Weber

Hi Flip,
this turned out to be more complicated, that I thought.
Have a look at this one,
with various possibilities of improvement, probably:

First, I search for a style, which is "00-bu-nc" here and now.
Then, if found, I get the number of the first paragraph with this
particular style in the documents main story.
Then, I check, whether the next paragraph is of this style, also.
If so, I repeat the check.
If not so, then the range end is set back one character.
In the reminding range, vbcr is being replaced by " ".

Great fun!

Sub Makro1()
Dim iPrg As Integer
Dim rDcm As Range
Dim rTmp As Range
Set rDcm = ActiveDocument.Range
Set rTmp = ActiveDocument.Range
ResetSearch
With rDcm.Find
.Style = "00-bu-nc"
If .Execute Then
rTmp.Start = rDcm.Start
rTmp.End = rDcm.End
iPrg = rTmp.Paragraphs.Count
While ActiveDocument.Paragraphs(iPrg + 1).Style = "00-bu-nc"
iPrg = iPrg + 1
Wend
rTmp.End = ActiveDocument.Paragraphs(iPrg).Range.End - 1
With rTmp.Find
.Text = vbCr
.Replacement.Text = " "
.Execute Replace:=wdReplaceAll
End With
End If
End With
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
F

Flip

Hello Helmut,
Thank you for responding. I did sense my question might be difficult to
answer.
Or, I made the question look too simple in my attempt to be as clear as
possible.
Or, maybe I overasked.

Anyway, this is what happens.
The parts that have the style "00-bu-nc" as you call it, all are within a
section (number 3)
Your code works fine only at the first occurence of the style.
It skips the next and other paragraphs that have a different style which is
perfect, but it also skips all other following occurences of the target
style.

Outside section 3, before and after, all CR's are replaced!

I'll try to make adjustments to get it going, yet being a beginner in the
beginners-NG any suggestions are welcome.
BTW FWIW, we are on Word 2000
Thanks,
Flip
 
H

Helmut Weber

Hi Flip,
here comes a hopefully improved version.
This time using a mixture of selection and range,
which doesn't make it perfect, certainly,
but it is so difficult, sometimes, to code using range,
as you don't see on the screen, where the range currently is.
Could be a step into the right direction:

Sub Test411()
Selection.ExtendMode = False
Selection.HomeKey unit:=wdStory
Dim iPrg As Integer
Dim rTmp As Range
ResetSearch
With Selection.Find
.Style = "00-bu-nc"
While .Execute
Set rTmp = Selection.Range
rTmp.Start = 0
iPrg = rTmp.Paragraphs.Count ' get Paragraph number
Set rTmp = Selection.Range
On Error GoTo finish ' no more target paragraphs
While ActiveDocument.Paragraphs(iPrg + 1).Style = "00-bu-nc"
iPrg = iPrg + 1
Wend
rTmp.End = ActiveDocument.Paragraphs(iPrg).Range.End - 1
rTmp.Select
With rTmp.Find
.Text = vbCr
.Replacement.Text = " "
.Execute Replace:=wdReplaceAll
End With
Selection.Collapse direction:=wdCollapseEnd
Selection.MoveDown unit:=wdLine
Selection.HomeKey unit:=wdLine
Wend
End With
finish:
ResetSearch
End Sub

Public Sub ResetSearch()
With Selection.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
F

Flip

Hi Helmut,
This is more then a step into the right direction.
Beutifull.
Thanks a lot!!
Flip
 

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