MS Word: duplicate line

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

MS Word: duplicate line

05 Dec 2017, 04:39

I have this code to copy down the line above in MS Word. I'm posting in case anyone who knows Word, can improve it in any way. Thanks.

Code: Select all

q:: ;word - duplicate line (copy line above)
wdLine := 5, wdExtend := 1
oWd := ComObjActive("Word.Application")
;oWd.Activate
oWd.Selection.MoveUp(wdLine, 1)
oWd.Selection.HomeKey(wdLine)
oWd.Selection.EndKey(wdLine, wdExtend)
vText := RTrim(oWd.Selection.Text, "`r`n")
oWd.Selection.MoveDown(wdLine, 1)
oWd.Selection.EndKey(wdLine)
oWd.Selection.TypeText(vText)
oWd := ""
return
There is also this related link, in case anyone has any ideas:
Visual Studio: duplicate line - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=35918
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Rohwedder
Posts: 7630
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: MS Word: duplicate line

05 Dec 2017, 08:06

Hallo,
try:

Code: Select all

q::
ClipBoard =
Send, {Up}{End}+{Home}^c
ClipWait,1
Send, {Down 2}{Home}^v
Return
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: MS Word: duplicate line

05 Dec 2017, 08:20

@Rohwedder: Thank you, but I'm trying to do it without using the clipboard.

I have some similar examples using the clipboard here:
Sublime / VSCode keyboard shortcuts - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 22&t=40648
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: MS Word: duplicate line

07 Dec 2017, 19:22

jeeswg wrote:I have this code to copy down the line above in MS Word. I'm posting in case anyone who knows Word, can improve it in any way. Thanks.

Code: Select all

q:: ;word - duplicate line (copy line above)
wdLine := 5, wdExtend := 1
oWd := ComObjActive("Word.Application")
;oWd.Activate
oWd.Selection.MoveUp(wdLine, 1)
oWd.Selection.HomeKey(wdLine)
oWd.Selection.EndKey(wdLine, wdExtend)
vText := RTrim(oWd.Selection.Text, "`r`n")
oWd.Selection.MoveDown(wdLine, 1)
oWd.Selection.EndKey(wdLine)
oWd.Selection.TypeText(vText)
oWd := ""
return
Here is a very slight improvement:

Code: Select all

; replace
oWd.Selection.HomeKey(wdLine)
oWd.Selection.EndKey(wdLine, wdExtend)

; with
oWd.Selection.Expand(wdLine)
FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: MS Word: duplicate line

07 Dec 2017, 20:53

Thanks FanaticGuru.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: MS Word: duplicate line

08 Dec 2017, 00:42

This is possibly a good alternative, it depends on how Word handles lines / 'paragraphs':

Code: Select all

q:: ;word - duplicate line (copy line above)
wdFirstCharacterLineNumber := 10
oWd := ComObjActive("Word.Application")
vLineNum := oWd.Selection.Information(wdFirstCharacterLineNumber)
vText := oWd.ActiveDocument.Paragraphs[vLineNum-1].Range.Text
vText := RTrim(vText, "`r`n")
oWd.Selection.TypeText(vText)
oWd := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
FanaticGuru
Posts: 1906
Joined: 30 Sep 2013, 22:25

Re: MS Word: duplicate line

08 Dec 2017, 13:09

jeeswg wrote:This is possibly a good alternative, it depends on how Word handles lines / 'paragraphs':

Code: Select all

q:: ;word - duplicate line (copy line above)
wdFirstCharacterLineNumber := 10
oWd := ComObjActive("Word.Application")
vLineNum := oWd.Selection.Information(wdFirstCharacterLineNumber)
vText := oWd.ActiveDocument.Paragraphs[vLineNum-1].Range.Text
vText := RTrim(vText, "`r`n")
oWd.Selection.TypeText(vText)
oWd := ""
return
I have tried that approach.

It is easy to get the current line number but very hard to go from a line number to a range. Paragraphs and lines are not the same thing when lines are wrapping.

This is the closest I found: https://msdn.microsoft.com/en-us/vba/wo ... bject-word
which allows for something like this in VBA:
Set objLine = ActiveDocument.ActiveWindow.Panes(1).Pages(1).Rectangles(1).Lines.Item(1)

I was hoping to find a way without using Selection but had no luck. MS Word doesn't have a firm grasp on how lines are going to look until it actually renders the document through the page layout parameters. Kind of the same way a HTML file does not understand lines until it is rendered through the layout of a web browser. Lines are fluid in both. Lines are not really part of the data's structure.

Selection.Information seems to run a method to figure that stuff out, but not really a reverse method to go the other way when you have a piece of Selection.Information and want to find the Selection.

You can use GoTo to select a line but again I would really like to be able to work with just ranges and not selections.

Paragraphs, sentences, words, and characters are all easy to work with as they have collections that do not change regardless of page layout but that is not the case with lines.

FG
Hotkey Help - Help Dialog for Currently Running AHK Scripts
AHK Startup - Consolidate Multiply AHK Scripts with one Tray Icon
Hotstring Manager - Create and Manage Hotstrings
[Class] WinHook - Create Window Shell Hooks and Window Event Hooks
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: MS Word: duplicate line

04 Sep 2018, 19:23

This is what I'm using at present:

Code: Select all

^d:: ;word - send text - duplicate line
SendInput, {Up}{Home}+{End}
oWd := ComObjActive("Word.Application")
vText := RTrim(oWd.Selection.Text, "`r`n")
SendInput, {Down}{End}
oWd.Selection.TypeText(vText)
oWd := ""
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Anput, mikeyww and 312 guests