Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 08/12/2024 in all areas

  1. 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 ;--------------------------------------------------------------------------------------------------------------------------------
    1 point
  2. 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.
    1 point
×
×
  • Create New...