We help IT Professionals succeed at work.

Word vba to delete paragraph marks at end of document

etech0
etech0 asked
on
In Word VBA, how can I have a macro check if there are any paragraph marks (aka blank lines) at the end of my document, and if so, delete them?
Comment
Watch Question

Business Analyst
CERTIFIED EXPERT
Commented:
etch0 –

This VBA should do it quite nicely

Sub KillEndBlanks()
'
' KillEndBlanks Macro
'
    ' Go to the end of the file
    Selection.EndKey Unit:=wdStory
    ' Select the last character
    Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    ' As long as the last character is a carriage return [CHR(13)]...
    While Selection.Text = vbCr
        ' ... Delete the character, and select the new last character
        Selection.Delete Unit:=wdCharacter, Count:=1
        Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
    Wend
    ' Go to the end of the file again to not leave a character selected
    Selection.EndKey Unit:=wdStory
End Sub

Hope this helps.

b.r.t.

Author

Commented:
Great!
Thanks a lot.
Barry TiceBusiness Analyst
CERTIFIED EXPERT

Commented:
You're welcome, etech0! Thanks for the points!