Emacs-like Control-K

B

Ben Bullock

Greetings,

I want to get an Emacs-like Control-K in Word. This means a function
to delete ("kill") everything from the cursor position to the end of
the current line.

There is a partial solution here:

http://www.macosxhints.com/article.php?story=20070215034801484

but it needs some improvement.

So far I have this:

--------------------

Sub EmacsControlK()
'
' EmacsControlK Macro
'
'
If Selection.Information(wdWithInTable) Then
MsgBox ("disabled in tables")
Else
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Cut
End If
End Sub

-----------------

It works OK on lines, although it is not quite perfect, but inside
table cells, for some reason, if it is on the last line of multiline
text, or if there is only one line in the cell, it deletes everything
in the cell, including everything before the cursor (even multiple
lines of content) - something I don't want. I have googled somewhat
and tried to do various things, but every effort seems to send me back
to the debugger.

Thanks for any wild guesses or ideas.
 
G

Greg Maxey

Try this:

Sub EmacsControlK()
Dim oRng As Word.Range
If Selection.Information(wdWithInTable) Then
Set oRng = Selection.Cells(1).Range
oRng.MoveEnd wdCharacter, -1
oRng.Start = Selection.Start
oRng.Delete
Else
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Cut
End If
End Sub


Ben said:
Greetings,

I want to get an Emacs-like Control-K in Word. This means a function
to delete ("kill") everything from the cursor position to the end of
the current line.

There is a partial solution here:

http://www.macosxhints.com/article.php?story=20070215034801484

but it needs some improvement.

So far I have this:

--------------------

Sub EmacsControlK()
'
' EmacsControlK Macro
'
'
If Selection.Information(wdWithInTable) Then
MsgBox ("disabled in tables")
Else
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Cut
End If
End Sub

-----------------

It works OK on lines, although it is not quite perfect, but inside
table cells, for some reason, if it is on the last line of multiline
text, or if there is only one line in the cell, it deletes everything
in the cell, including everything before the cursor (even multiple
lines of content) - something I don't want. I have googled somewhat
and tried to do various things, but every effort seems to send me back
to the debugger.

Thanks for any wild guesses or ideas.

--
Greg Maxey

See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.

"It is not the critic who counts, not the man who points out how the
strong man stumbles, or where the doer of deeds could have done them
better. The credit belongs to the man in the arena, whose face is
marred by dust and sweat and blood, who strives valiantly...who knows
the great enthusiasms, the great devotions, who spends himself in a
worthy cause, who at the best knows in the end the triumph of high
achievement, and who at the worst, if he fails, at least fails while
daring greatly, so that his place shall never be with those cold and
timid souls who have never known neither victory nor defeat." - TR
 
G

Greg Maxey

What would it take to make it perfect?

Ben said:
Greetings,

I want to get an Emacs-like Control-K in Word. This means a function
to delete ("kill") everything from the cursor position to the end of
the current line.

There is a partial solution here:

http://www.macosxhints.com/article.php?story=20070215034801484

but it needs some improvement.

So far I have this:

--------------------

Sub EmacsControlK()
'
' EmacsControlK Macro
'
'
If Selection.Information(wdWithInTable) Then
MsgBox ("disabled in tables")
Else
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Cut
End If
End Sub

-----------------

It works OK on lines, although it is not quite perfect, but inside
table cells, for some reason, if it is on the last line of multiline
text, or if there is only one line in the cell, it deletes everything
in the cell, including everything before the cursor (even multiple
lines of content) - something I don't want. I have googled somewhat
and tried to do various things, but every effort seems to send me back
to the debugger.

Thanks for any wild guesses or ideas.

--
Greg Maxey

See my web site http://gregmaxey.mvps.org
for an eclectic collection of Word Tips.

"It is not the critic who counts, not the man who points out how the
strong man stumbles, or where the doer of deeds could have done them
better. The credit belongs to the man in the arena, whose face is
marred by dust and sweat and blood, who strives valiantly...who knows
the great enthusiasms, the great devotions, who spends himself in a
worthy cause, who at the best knows in the end the triumph of high
achievement, and who at the worst, if he fails, at least fails while
daring greatly, so that his place shall never be with those cold and
timid souls who have never known neither victory nor defeat." - TR
 
B

Ben Bullock

What would it take to make it perfect?

Thanks very much for your help so far. There is one more addition,
which stops it from deleting the paragraph mark if there is a
paragraph mark at the end of the line:

Sub EmacsControlK()
'
' EmacsControlK Macro
'
'
Dim oRng As Word.Range
If Selection.Information(wdWithInTable) Then
Set oRng = Selection.Cells(1).Range
oRng.MoveEnd wdCharacter, -1
oRng.Start = Selection.Start
oRng.Delete
Else
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
If Selection.End > Selection.Start + 1 Then
Selection.End = Selection.End - 1
End If
Selection.Cut
End If
End Sub

This is now approximately the same behaviour as Control-K on Emacs.
However, if I notice any more problems like the one with the text
cells, I'll ask again.

Thanks again.

Ben Bullock
 

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