Jump to content

Copy and paste from one document to another document in MS Word


Go to solution Solved by Zuhair,

Recommended Posts

While working on two MS Word documents, I have to copy hundreds of randomly selected words from Document-1 to Document-2 one by one. I tried to do the job with AutoIt but the paste command is not working at all. I select the text in Document-1 and then I press the shortcut key so the script should copy that selected text to clipboard and then it should paste the text in the Document-2, and then it should insert three spaces after it and then the script should re-activate the Document-1. But it is not working as required.

Func copy_paste_word()
        Local $hWnd1 = WinGetHandle($MSWord1)       ; Retrieves the handle of the Document-1 window.
        Local $hWnd2 = WinGetHandle($MSWord2)       ; Retrieves the handle of the Document-2 window.
        Send('^c')                                  ; Copies the selected text (two or three words) to clipboard.
        WinActivate($hWnd2)                         ; activates MS Word Document-2 to paste text
        Send('^v')                                  ; pastes the text from clipboard 
        Send('{SPACE 3}')                           ; inserts three spaces after the pasted text
        WinActivate($hWnd1)                         ; Goes back ot MS Word Document-1 for next text selection
EndFunc

Is it possible with RegEx to copy the text and insert 3 spaces in the 2nd document without activating the document-2 window?

Link to comment
Share on other sites

  

Is it necessary to do this with keyboard commands? Or have you looked at the _Word_ functions included with Autoit? You can do this simply enough with MS Word commands, such as (I think) .Selection.Copy and Selection.Paste, from AutoIt.

Something similar to this: ( I don't had a chance to test it right now.) Okay I tested it, and it should work for you.

#include <MsgBoxConstants.au3>
#include <Word.au3>

Local $sDocName1 = "Doc1.docx"
Local $sDocName2 = "Doc2.docx"

Local $oDoc1, $oDoc2

; Create application object
Local $oWord = _Word_Create()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

; Attach to the document by name
$oDoc1 = _Word_DocAttach($oWord, $sDocName1, "FileName")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error attaching to Document 1 by 'FileName'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

    ; Attach to the second document by name
$oDoc2 = _Word_DocAttach($oWord, $sDocName2, "FileName")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error attaching to Document 2 by 'FileName'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

$oDoc1.ActiveWindow.Selection.Copy; Copy to clipboard from Document 1

$oDoc2.ActiveWindow.Selection.Paste(); Paste in Document 2

$oDoc2.ActiveWindow.Selection.InsertAfter("   "); Insert 3 spaces in Document 2

 

Edited by donnyh13

LibreOffice UDF  ; Scite4AutoIt Spell-Checker Using LibreOffice

Spoiler

"Life is chiefly made up, not of great sacrifices and wonderful achievements, but of little things. It is oftenest through the little things which seem so unworthy of notice that great good or evil is brought into our lives. It is through our failure to endure the tests that come to us in little things, that the habits are molded, the character misshaped; and when the greater tests come, they find us unready. Only by acting upon principle in the tests of daily life can we acquire power to stand firm and faithful in the most dangerous and most difficult positions."

 

Link to comment
Share on other sites

  • Solution

Dear donnyh13 Thank you for reply but your script did not work for me. May be I am missing something but I succeeded in creating a script. Here is the script 

HotKeySet('{F3}', 'copy_paste_word')  

Func copy_paste_word()
    Send("^c")
    Sleep(100)                      ; Wait for clipboard to be populated    
    $text = ClipGet()               ; Retrieve the copied text from the clipboard
    
    ; Check if the clipboard is empty or failed to copy
    If $text = "" Then
        MsgBox(0, "Error", "Clipboard is empty or copy failed.")
        Return
    EndIf    
    
    $text = $text & "   "           ; Add three spaces after the text
    ClipPut($text)                  ; Set the modified text back to the clipboard
    
    ; Activate Document-2 and paste the text
    Local $hWnd2 = WinGetHandle('Document-2.docx')       ; Change the document name for your use
    
    ; Check if the Document-2 window is present
    If $hWnd2 = "" Then
        MsgBox(0, "Error", "Document-2 window handle not found.")
        Return
    EndIf
    
    WinActivate($hWnd2)             ; activate the document-2 window
    Sleep(300)                      ; delay to ensure the window is activated
    Send("^v")                      ; Paste the text
    Sleep(400)                      ; delay to view the paste process by yourself
    
    ; Go back to Document-2 by activating it
    Local $hWnd2 = WinGetHandle('Document-1.docx')      ; Change the document name for your use
    If $hWnd1 <> "" Then
        WinActivate($hWnd1)
    EndIf
EndFunc

This script is working correctly. I select some text from Document-1 and then I press the Hotkey, it copies the selected text then activates document-2 and pastes the text along with three spaces and then goes back to the Document-1 by reactivating it.

One important thing that I experienced, though I don't know its reason,  is that while using the command WinGetHAndle() if I use a variable for the document name then the script don't work as required. For example if I assign the document name to a variable and then use the variable with this command as follows

$Doc1 = 'Document-1.docx'
$Doc2 = 'Document-2.docx'

Local $hWnd2 = WinGetHandle($Doc2)

Local $hWnd1 = WinGetHandle($Doc1)

then the script do not work correctly.

Link to comment
Share on other sites

the first one wasn't bad, you just need some time after Send('^c') ,
 

Global $MSWord1 = "Doc1.docx - Word"
Global $MSWord2 = "Doc2.docx - Word"

HotKeySet("{F3}", "copy_paste_word")
HotKeySet("{ESC}", "_Exit")

;**********************************
While 1
    Sleep(50)
WEnd
;**********************************


;--------------------------------------------------------------------------------------------------------------------------------
Func _Exit()
    Exit
EndFunc
;--------------------------------------------------------------------------------------------------------------------------------
Func copy_paste_word()
        Local $hWnd1 = WinGetHandle($MSWord1)       ; Retrieves the handle of the Document-1 window.
        Local $hWnd2 = WinGetHandle($MSWord2)       ; Retrieves the handle of the Document-2 window.
        WinActivate($hWnd1)                         ; * Goes on MS Word Document-1 for text selection
        Send('^c')                                  ; Copies the selected text (two or three words) to clipboard.
        Sleep(100)                                  ; * Give some time
        WinActivate($hWnd2)                         ; activates MS Word Document-2 to paste text
        Send('^v')                                  ; pastes the text from clipboard
        Send('{SPACE 3}')                           ; inserts three spaces after the pasted text
        WinActivate($hWnd1)                         ; Goes back ot MS Word Document-1 for next text selection
EndFunc

;--------------------------------------------------------------------------------------------------------------------------------

however,
what donnyh13 suggested is better because it works without activating the document-2 window

#include <MsgBoxConstants.au3>
#include <Word.au3>

HotKeySet("{F3}", "copy_paste_word")
HotKeySet("{ESC}", "_Exit")

Global $sDocName1 = @ScriptDir & "\Doc1.docx"
Global $sDocName2 = @ScriptDir & "\Doc2.docx"
Global $oDoc1, $oDoc2

; Create application object
Global $oWord = _Word_Create()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "$oWord", "Error creating a new Word application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)

; Attach to the document by name
$oDoc1 = _Word_DocOpen($oWord, $sDocName1)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "@error", "Error opening " & $sDocName1 & @CRLF & "@error = " & @error & ", @extended = " & @extended)

; Attach to the document by name
$oDoc2 = _Word_DocOpen($oWord, $sDocName2)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "@error", "Error opening " & $sDocName2 & @CRLF & "@error = " & @error & ", @extended = " & @extended)


;**********************************
While 1
    Sleep(50)
WEnd
;**********************************


;--------------------------------------------------------------------------------------------------------------------------------
Func _Exit()
    Exit
EndFunc   ;==>_Exit
;--------------------------------------------------------------------------------------------------------------------------------
Func copy_paste_word()
    $oDoc1.ActiveWindow.Selection.Copy ; Copy to clipboard from Document 1
    $oDoc2.ActiveWindow.Selection.Paste() ; Paste in Document 2
    $oDoc2.ActiveWindow.Selection.InsertAfter("   ") ; Insert 3 spaces in Document 2
EndFunc   ;==>copy_paste_word
;--------------------------------------------------------------------------------------------------------------------------------

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...