Command for create/paste clipboard content into .doc file?

Discussion, questions and support.
Posts: 54
Joined: Sat Jun 06, 2015 2:28 am

Command for create/paste clipboard content into .doc file?

PostPosted by cadudesun » Sat Apr 08, 2017 11:03 pm

Hi,

Is there a command to create a new .doc file having as "body" the clipboard content?
Currently I create a blank .doc (1), open it (2), then paste clipboard content (3) and save.
I'm trying to replace all steps for a single command, which should generate a .doc file and when I open it the clipboard content will be there already.

Thanks for info!
Carlos_Cadu

Site Admin
User avatar
Posts: 641
Joined: Sat Mar 02, 2013 9:02 pm

Re: Command for create/paste clipboard content into .doc fil

PostPosted by Marko » Sun Apr 09, 2017 8:19 pm

Something like this?
Type: Command
Code: Select all
RunWait, winword
WinActivate, Microsoft Word
Send ^v
sleep 500
Send ^s

or using COM:
Code: Select all
wd:=ComObjCreate("Word.Application")
doc:=wd.Documents.Add()
doc.Content.Paste()
doc.SaveAs("Filename.docx")
doc.Close()
wd.Quit()

Posts: 54
Joined: Sat Jun 06, 2015 2:28 am

Re: Command for create/paste clipboard content into .doc fil

PostPosted by cadudesun » Mon Apr 10, 2017 12:28 am

Thanks for reply Marko.
I tried both commands, being:
"RunWait, winword" worked closer to what I need, and "wd:=ComObjCreate("Word.Application")" didn't run.
Actually, I wondering if the Word (.doc) file couldn't already be created through the command.
I recorded my screen showing what I'm trying to achieve (https://goo.gl/BL3m76), in that case my file explorer has a command that create .txt files based on clipboard content, and I'm trying to achieve similar process but for .doc through FastKeys.
Do you think it would be possible to create a .doc file similarly to what is showed for .txt in the video?
Thanks!

Site Admin
User avatar
Posts: 641
Joined: Sat Mar 02, 2013 9:02 pm

Re: Command for create/paste clipboard content into .doc fil

PostPosted by Marko » Mon Apr 10, 2017 12:50 pm

Carlos, I think the COM alternative should work for you (I tested it on Win7 and Win10). You just need to specify the right path for the file, like
Code: Select all
doc.SaveAs("D:\MyFolder\Filename.docx")

Posts: 54
Joined: Sat Jun 06, 2015 2:28 am

Re: Command for create/paste clipboard content into .doc fil

PostPosted by cadudesun » Mon Apr 10, 2017 1:50 pm

Many thanks for directions Marko.

I changed the command to:
Code: Select all
wd:=ComObjCreate("Word.Application")
doc:=wd.Documents.Add()
doc.Content.Paste()
doc.SaveAs("Z:\!TRANSFER\DOC\Filename.docx")
doc.Close()
wd.Quit()


However, I'm still having an error message, as showed in this snapshot: http://i.imgur.com/IHLLhEQ.png
I also recorded my screen showing the issue: https://goo.gl/61Fzz4
Do you have idea what is going wrong?

Site Admin
User avatar
Posts: 641
Joined: Sat Mar 02, 2013 9:02 pm

Re: Command for create/paste clipboard content into .doc fil

PostPosted by Marko » Mon Apr 10, 2017 2:55 pm

There must be a conflict with some other command you use.

You can also try to do it manually (adjust sleeps if needed):
Code: Select all
SendMode, Input
RunWait, winword
WinActivate, Microsoft Word
sleep 500
Send ^v
sleep 500
Send !f
Send a
sleep 1000
Send b
sleep 1000
Send Z:\!TRANSFER\DOC\Filename
sleep 1000
Send {Enter}
Send !{F4}

Posts: 54
Joined: Sat Jun 06, 2015 2:28 am

Re: Command for create/paste clipboard content into .doc fil

PostPosted by cadudesun » Mon Apr 10, 2017 6:53 pm

Many thanks Marko!
The last command worked perfectly.
Based on that I also created a shorter variation, that just open and paste content into word:
Code: Select all
SendMode, Input
RunWait, winword
WinActivate, Microsoft Word
sleep 500
Send ^v

I've never used this kind of command, which now opens so many possibilities in my workflow!

Posts: 9
Joined: Sat Apr 08, 2017 3:55 pm

Re: Command for create/paste clipboard content into .doc fil

PostPosted by burque505 » Wed Apr 12, 2017 5:09 pm

Code: Select all
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.
    ;Create a COM Object Word Application
oWord := ComObjCreate("Word.Application")
    ;Create a new Document.
oWord.Documents.Add
    ;Add Text to our document.
oWord.Selection.TypeText("AutoHotkey is a pretty cool scripting language.")
    ;Now lets add our comment
oWord.ActiveDocument.Comments.Add(oWord.ActiveDocument.Paragraphs[1].Range
                                  , "This is a very true statement!.")
    ;Make our document Visible
oWord.Visible := 1
    ;Set a Hotkey to Delete the Comment. (Windows Key + F1)
#F1::oWord.ActiveDocument.Comments(1).Delete


cadudesun, I'm having COM problems also, but this one runs on my Win7 64-bit machine with 64-bit MS Office 2016.
At Marko's suggestion, set FastKeys to always run as Admin, and from Tools->Preferences->Functions, check "Run as administrator".
So the above code works with these settings. However, if I run a script that uses the following:
Code: Select all
FilePath := "C:\test\AHK_test\WL-RTF.docx"
oDoc := ComObjGet(FilePath)
msgbox % FilePath ;for debugging, file path is correct.
oDoc.Range.FormattedText.Copy
oDoc.Close(0)
Send, ^v
Send, {Backspace}
return
Escape::ExitApp


.. it seems like the command oDoc.Range.FormattedText.Copy may be the culprit, because the content never gets to the clipboard.
(The same script works with AHK_L U64, by the way.)
:cry:

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 0 guests