Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / July 2007

Tip: Looking for answers? Try searching our database.

Remove all excess vbcrlf from end of document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
elle0612 - 06 Jul 2007 17:24 GMT
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
Russ - 06 Jul 2007 19:01 GMT
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

> 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
[quoted text clipped - 3 lines]
>
> Thanks

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 06 Jul 2007 19:32 GMT
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

>     Set objRange = ActiveDocument.Range(0, 0)
>     With objRange.Find
[quoted text clipped - 28 lines]
>>
>> Thanks

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 06 Jul 2007 19:41 GMT
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.

> Elle,
> For the whole routine to work without an error, you need to add the line
[quoted text clipped - 43 lines]
>>>
>>> Thanks

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

elle0612 - 06 Jul 2007 22:46 GMT
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???

> 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.
[quoted text clipped - 46 lines]
> >>>
> >>> Thanks
Russ - 07 Jul 2007 02:01 GMT
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 (¶).
---------------------------
I would use .^13 or .\n to find those in more code to delete them or delete
the line manually and rerun the loop code or whole subroutine again.

> Thanks very much, thats a great piece of code to know.
>
[quoted text clipped - 54 lines]
>>>>>
>>>>> Thanks

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 07 Jul 2007 02:20 GMT
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 ¶

> Elle,
> In part of the code:
[quoted text clipped - 83 lines]
>>>>>>
>>>>>> Thanks

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Russ - 07 Jul 2007 02:32 GMT
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
[quoted text clipped - 16 lines]
>>          ActiveDocument.Paragraphs.First.Range.Delete
>>     Loop
   Do While ActiveDocument.Paragraphs.Last.Range.Characters.Count = 1
       ActiveDocument.Paragraphs.Last.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.
[quoted text clipped - 72 lines]
>>>>>>>
>>>>>>> Thanks

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

elle0612 - 08 Jul 2007 22:36 GMT
Thanks very much Russ.  You've explained it very well, and I understand what
the code is doing.

> Oops,
> I was referring to the wrong loop below.
[quoted text clipped - 99 lines]
> >>>>>>>
> >>>>>>> Thanks
Russ - 09 Jul 2007 06:59 GMT
Elle,
You're Welcome.
Glad to help.
I've been helped tremendously by these forums.

> Thanks very much Russ.  You've explained it very well, and I understand what
> the code is doing.
[quoted text clipped - 107 lines]
>>>>>>>>>
>>>>>>>>> Thanks

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

Helmut Weber - 09 Jul 2007 16:42 GMT
Hi Russ,

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

You can't replace the end-of-doc mark.
>>>>         .Text = "^13{2,}" 'Two or More?
>>>>         .Replacement.Text = "^p"
>>>>         .Execute Replace:=wdReplaceAll

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.
Russ - 09 Jul 2007 23:27 GMT
Helmut,
This was in my first reply to her initial question. Notice the second
sentence.

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

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.

> Hi Russ,
>
[quoted text clipped - 30 lines]
>
> Have a nice day.

Signature

Russ

drsmN0SPAMikleAThotmailD0Tcom.INVALID

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.