Copy text from one Microsoft Word doc & paste it into another MS Word doc Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Nantes
Posts: 7
Joined: 16 Jun 2021, 21:19

Copy text from one Microsoft Word doc & paste it into another MS Word doc

18 Jun 2021, 09:11

In my professional work as a translator I often have to translate just select paragraphs of a file, which I have always manually copied and pasted to another Word document, which will be imported into my Computer-Assisted Translation software. It would help my productivity if, when copying the text, it automatically pasted it into the other Word file, with three empty lines (like pressing Return/Enter) between the new copied paragraph and the next (to make these paragraphs that came from distant parts of the document easier to identify compared to e.g. copying several contiguous paragraphs), and keeping the original formatting (so that things like numbers in subscript get carried over).

Searching these forums I found this script by FanaticGuru, which I took out the parts I didn't need and changed trigger hotkey to {, but it doesn't work when there are two Word files open (it pastes the text onto the same file I copied it from), doesn't maintain the original formatting, and doesn't add three empty lines between each copying.

Code: Select all

F12::
	if (Toggle = "")
	{
		wdApp := ComObjActive("Word.Application")
		wdApp.Selection.Range.ListFormat.ApplyOutlineNumberDefault
		Toggle := 0
	}
	Toggle := !Toggle
	MsgBox % Toggle ? "Clip to Word: ON" : "Clip to Word: OFF"
return

#if Toggle
{::
	Send ^c
	ClipWait, 1
	wdApp.Selection.TypeText(Clipboard "`n")
return
#If
Thanks a lot!
GosHotk
Posts: 7
Joined: 11 Jun 2021, 12:57

Re: Copy text from one Microsoft Word doc & paste it into another MS Word doc  Topic is solved

18 Jun 2021, 11:49

Rather than use AutoHotkey for this, it would probably make more sense to use a Word macro and assign the macro to a key combination in Word.

If you're unfamiliar with Word key-assignments, you can get to them by right-clicking on the ribbon and choosing "Customize," and then clicking on the keyboard customize button at the bottom of the dialog.

Here's some simple Word VBA that I just quick-tested in Word 2019. It assumes that you've already selected the Document 1 text when you call the macro, and that the document receiving the pastes is the only other document open.

Code: Select all

Sub PasteInDoc2()

    Dim docSourceDoc As Word.Document
 
    Set docSourceDoc = ActiveDocument

    Selection.Copy

     Select Case ActiveWindow.index
      Case 1
        Windows(2).Activate
      Case 2
        Windows(1).Activate
    End Select

    Selection.Paste
    Selection.InsertAfter vbCrLf & vbCrLf & vbCrLf
    Selection.Collapse wdCollapseEnd

    docSourceDoc.Activate

    Selection.Collapse wdCollapseEnd

End Sub
Last edited by GosHotk on 18 Jun 2021, 14:59, edited 2 times in total.
gregster
Posts: 8992
Joined: 30 Sep 2013, 06:48

Re: Copy text from one Microsoft Word doc & paste it into another MS Word doc

18 Jun 2021, 12:40

GosHotk wrote:
18 Jun 2021, 11:49
Rather than use AutoHotkey for this, it would probably make more sense to use a Word macro and assign the macro to a key combination in Word.
If you connect to Word via its COM interface (like FanaticGuru's script above), it uses the same automation technique like internal Word macros, afaik. It's not a bad approach.
User avatar
SirSocks
Posts: 360
Joined: 26 Oct 2018, 08:14

Re: Copy text from one Microsoft Word doc & paste it into another MS Word doc

18 Jun 2021, 12:54

Try this.....

Code: Select all

#NoEnv 
SendMode Input 
#Persistent
#SingleInstance Force

#IfWinActive ahk_exe WINWORD.EXE
F12::
keywait, %A_Thishotkey%
ClipSaved := ClipboardAll   ; Save the entire clipboard to a variable of your choice.
Clipboard := ""
Send, ^c ; Ctrl Copy
ClipWait ; Waits for item to be copied to the clipboard
sleep 500
send, ^n ; launch new document
sleep, 1000 ; wait for document to open
send, %Clipboard%
Clipboard := ClipSaved   ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
ClipSaved := ""   ; Free the memory in case the clipboard was very large.
return
GosHotk
Posts: 7
Joined: 11 Jun 2021, 12:57

Re: Copy text from one Microsoft Word doc & paste it into another MS Word doc

18 Jun 2021, 13:33

Besides assuming that only two docs were open, the VBA in my original post also assumed that the source doc was Windows(1). So I've tweaked it (revised that post) to allow for the possibility that the source doc is 2 and the target doc is 1. (And I also removed the extraneous definition of docTargetDoc.)
Nantes
Posts: 7
Joined: 16 Jun 2021, 21:19

Re: Copy text from one Microsoft Word doc & paste it into another MS Word doc

18 Jun 2021, 13:48

SirSocks wrote:
18 Jun 2021, 12:54
Try this.....
You're on the right track, but that opens a new document each time I copy another paragraph; it should always paste to the same document. It should also have 3 lines of spacing between each pasted output, so that if I copy a block of several contiguous paragraphs and then later copy a block of paragraph that's further down the document, that second block is visually well-separated from the first block.
Nantes
Posts: 7
Joined: 16 Jun 2021, 21:19

Re: Copy text from one Microsoft Word doc & paste it into another MS Word doc

18 Jun 2021, 14:11

GosHotk wrote:
18 Jun 2021, 11:49
Rather than use AutoHotkey for this, it would probably make more sense to use a Word macro and assign the macro to a key combination in Word.
That works wonders, thank you a whole bunch!!
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Copy text from one Microsoft Word doc & paste it into another MS Word doc

18 Jun 2021, 23:05

There is a bunch of reasons to use AHK over VBA and, after all, this is a AHK forum, not a VBA forum ;) .
Here is a way to do it via COM:

Code: Select all

#{::
oWord := ComObjActive("Word.Application")
if (oWord.documents.count != 2)                                              ; checks if 2 documents are open
	{
	Msgbox Two Word documents need to be open
	return
	}
copyto := (oWord.activedocument.fullname = oword.documents(1).fullname) ? oword.documents(2) : oword.documents(1)
oWord.Selection.copy
copyto.Content.InsertAfter(clipboard "`n`n`n")
14.3 & 1.3.7

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: haomingchen1998, mikeyww, mmflume, roysubs, scriptor2016, ShatterCoder and 102 guests