Delete to End of Current Line

Home Up Odds & Ends Photo Gallery Search Contact Me Privacy Notice What's New?

 

 

The information in this website is provided without risk or obligation and free of charge.  However, if you have benefitted from my efforts here and would like to make a contribution to help me continue and maintain this work then any donation will be greatly appreciated. Please click the adjacent button to access PayPal.  Thank you.
 
This Microsoft Word Tips & Microsoft Word Help page provides a VBA solution for the task of deleting all text from the insertion point (IP) to the end of the current line.  Unlike some other word processing application, Word does not include a built-in feature to delete all text from the IP to the end of the current line.
With Word you can use a keyboard shortcut combination to select and then delete text to the end of the current line as follows:

  - Shift + End (selects text to end of line)    

  - Delete (deletes the selected text)

Recording a macro of these steps results in the following VBA code:

Sub Macro1()
' Macro1 Macro
' Macro recorded 10/4/2009 by Gregory K. Maxey
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Delete Unit:=wdCharacter, Count:=1
End Sub
The steps and recorded macro outlined above can produce an undesired result if the current line is the last line of the current paragraph or or if the text is the last line in a table cell as illustrated below.

The following macro overcomes these issues:
Sub DeleteToEndOfLine()
Dim oRng As Word.Range
Dim oIPRng As Word.Range
With Selection
    If Not .Information(wdWithInTable) Then
        .EndKey Unit:=wdLine, Extend:=wdExtend
        If .End > .Start + 1 Then
            .End = .End - 1
        End If
        .Cut
    Else
        Set oIPRng = .Range
        oIPRng.Start = .Start
        .EndKey Unit:=wdLine, Extend:=wdExtend
        If .Range = .Cells(1).Range Then
            Set oRng = .Cells(1).Range
            With oRng
                .MoveEnd wdCharacter, -1
                .Start = oIPRng.Start
                .Delete
            End With
        Else
            If .End > .Start + 1 Then
               .End = .End - 1
            End If
            .Cut
        End If
    End If
End With
End Sub
This code will delete text to the end of the current line.  It preserves the paragraph mark and works with text in a table cell.
:old: Note:  I assigned this macro to a keyboard shortcut ALT+Shift+End, Delete 

See: Installing Macros for instructions on how to set up and use the macro listed above.

See: Setup Menu/Toolbar for instructions on how to run a macro from a menu or toolbar icon.

See: Keyboard Shortcut for instructions on how to run a macro using a keyboard shortcut.


Looking for something else?

Google