Remove all excess vbcrlf from end of document

E

elle0612

I am creating a template which is nearly complete, except for the fact that I
would like to remove all excess VBCrLf from below the last line of the
document, bar the footer.

Is there a way of achieving this?

Thanks
 
R

Russ

Elle,
Here's a routine I use to purge the mainstory of document of its 'empty'
lines.
I use it maybe before or after a paragraph sort for lists, for instance.

Public Sub Delete_Empty_Lines()
Set objRange = ActiveDocument.Range(0, 0)
With objRange.Find
.MatchWildcards = True
.Text = "^13"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = "^13{2,}"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With

Do While ActiveDocument.Paragraphs.Last.Range.Characters.Count = 1
ActiveDocument.Paragraphs.Last.Range.Delete
Loop

Do While ActiveDocument.Paragraphs.First.Range.Characters.Count = 1
ActiveDocument.Paragraphs.First.Range.Delete
Loop
End Sub



This part is one of many ways to do what you want.
Do While ActiveDocument.Paragraphs.Last.Range.Characters.Count = 1
ActiveDocument.Paragraphs.Last.Range.Delete
Loop
 
R

Russ

Elle,
For the whole routine to work without an error, you need to add the line
below. I forgot that I had the object 'objRange' declared Globally in the
(Declarations) section of the project.
Elle,
Here's a routine I use to purge the mainstory of document of its 'empty'
lines.
I use it maybe before or after a paragraph sort for lists, for instance.

Public Sub Delete_Empty_Lines()

Dim objRange As Range
 
R

Russ

Since this is a beginner's forum. I should also mention that MacWord macro
users should substitute \n when they see a ^13 in a WinWord macro.
 
E

elle0612

Thanks very much, thats a great piece of code to know.

Somehow though, I was left with one full stop about 3/4 of the way down the
page (at least I think it was a full stop . I don't quite understand what
the code is doing - is it looking for carriage returns and replacing with
paragraphs???
 
R

Russ

Elle,
In part of the code:
The .Text = "^13" is looking for 'odd' paragraph endings and replacing with
^p which are true 'Word' paragraph endings.

------------------------
The code segment:
Do While ActiveDocument.Paragraphs.First.Range.Characters.Count = 1
ActiveDocument.Paragraphs.First.Range.Delete
Loop

Is looking for those 'lines' at the end of the document that have only 1
character and in Word that character would have to be a paragraph mark.

Working backwards from the document end, it stops when it reaches a line
with more than one character. (your full stop line)
----------------------------
Because
..
Is actually
..¶
Two characters.
Seen by toggling the Main Toolbar Show/Hide Button (¶).
 
R

Russ

Elle,
The other part of the code is look for a string of paragraph marks, and
replacing with one.
.Text = "^13{2,}" 'Two or More?
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll


If ¶¶¶¶¶ found (of course would be vertical in a document, but I am
emphasizing the string concept), replace with ¶
 
R

Russ

Oops,
I was referring to the wrong loop below.
Elle,
The other part of the code is look for a string of paragraph marks, and
replacing with one.
.Text = "^13{2,}" 'Two or More?
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll


If ¶¶¶¶¶ found (of course would be vertical in a document, but I am
emphasizing the string concept), replace with ¶
Do While ActiveDocument.Paragraphs.Last.Range.Characters.Count = 1
ActiveDocument.Paragraphs.Last.Range.Delete
Loop
 
E

elle0612

Thanks very much Russ. You've explained it very well, and I understand what
the code is doing.
 
H

Helmut Weber

Hi Russ,

sorry, could not resist,
at least for cleaning the doc's end.

You can't replace the end-of-doc mark.
If there is an empty paragraph at the doc's end,
You won't get rid of it that way.

If google groups search is available again,
seems to be down for a while, search for PurgeDocEnd
and my decent name.

Sub Test33()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
While Len(rDcm.Paragraphs.Last.Range.Text) = 1
rDcm.Paragraphs.Last.Range.Delete
Wend
End Sub

Many other ways, of course.

Which doesn' deal with trailing paragraphs containing
only white pace. But that's another question.

Hmm, and when replacing two chr(13) with "^p",
what formatting, if different, should win?

Have a nice day.
 
R

Russ

Helmut,
This was in my first reply to her initial question. Notice the second
sentence.
Here's a routine I use to purge the mainstory of document of its 'empty'
lines.
I use it maybe before or after a paragraph sort for lists, for instance.

Public Sub Delete_Empty_Lines()
Dim objRange as Range 'was added in a follow-up message
Set objRange = ActiveDocument.Range(0, 0)
With objRange.Find
.MatchWildcards = True
.Text = "^13"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = "^13{2,}"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With

Do While ActiveDocument.Paragraphs.Last.Range.Characters.Count = 1
ActiveDocument.Paragraphs.Last.Range.Delete
Loop

Do While ActiveDocument.Paragraphs.First.Range.Characters.Count = 1
ActiveDocument.Paragraphs.First.Range.Delete
Loop
End Sub

Helmut, later in the message thread I was trying to explain the reasoning
behind each part of the macro and apparently that was the only thing you
noticed.
 

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