Append clipboard to MSWord document

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
smarkturner
Posts: 7
Joined: 14 Mar 2022, 19:15

Append clipboard to MSWord document

Post by smarkturner » 14 Mar 2022, 19:24

Can someone please provide code which will append the contents of the clipboard (txt or image) to a MS Word document without opening the document? I've only been able to find a very basic example of opening a document and writing a hardcoded text string. Thank you.

User avatar
mikeyww
Posts: 26889
Joined: 09 Sep 2014, 18:38

Re: Append clipboard to MSWord document

Post by mikeyww » 14 Mar 2022, 20:00

Welcome to this AutoHotkey forum!

Code: Select all

Clipboard = abcde
Sleep, 100
; ------------------------------------
file = d:\temp2\xyz.docx
SoundBeep, 1500
append(file, Clipboard)
Run, %file%

append(file, text) { ; Append text to a Microsoft Word file
 oWord := ComObjCreate("Word.Application"), oWord.Visible := False
 doc   := oWord.Documents.Open(file)
 doc.Content.InsertAfter(text), doc.Save(), doc.Close(), oWord.Quit()
}
For pictures: viewtopic.php?p=442238#p442238https://docs.microsoft.com/en-us/office/vba/api/word.inlineshapes.addpicture

smarkturner
Posts: 7
Joined: 14 Mar 2022, 19:15

Re: Append clipboard to MSWord document

Post by smarkturner » 16 Mar 2022, 10:14

Hi mikeyww,

Thank you very much for the quick reply.

Your code works perfectly for text, however I would also like to be able to paste images.

Perhaps using the Paste method?

I've experimented with it and can't get it to work (I don't really have any idea what I'm doing when it comes to the VBA stuff.

When you have a minute, could you please have a look?

Thank you so much!
Last edited by smarkturner on 16 Mar 2022, 13:10, edited 1 time in total.

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Append clipboard to MSWord document

Post by flyingDman » 16 Mar 2022, 12:09

Did you try:

Code: Select all

oWord.selection.paste
?
14.3 & 1.3.7

smarkturner
Posts: 7
Joined: 14 Mar 2022, 19:15

Re: Append clipboard to MSWord document

Post by smarkturner » 16 Mar 2022, 13:17

I accidentally deleted my earlier response to mikeyww. I'm having one of those days!

So, I basically said I needed to be able to append images in addition to text and asked about using the Paste method, and since I don't know anything about using the VBA code in AHK, asked if he could take a look and send an update to the above suggestion. Thank you!
image.png

smarkturner
Posts: 7
Joined: 14 Mar 2022, 19:15

Re: Append clipboard to MSWord document

Post by smarkturner » 16 Mar 2022, 13:30

Got it!!!

Code: Select all

        file = C:\Users\smark\Desktop\Test.docx 
        oWord := ComObjCreate("Word.Application"), oWord.Visible := False
        doc   := oWord.Documents.Open(file)
        oWord.Selection.PasteAndFormat(16), doc.Save(), doc.Close(), oWord.Quit() 
Many thanks!!!!!!

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Append clipboard to MSWord document

Post by flyingDman » 16 Mar 2022, 14:17

oWord.selection.paste works as well.

And, btw, doc.Save(), doc.Close(), oWord.Quit() can be replaced by oWord.Quit(-1)
14.3 & 1.3.7

smarkturner
Posts: 7
Joined: 14 Mar 2022, 19:15

Re: Append clipboard to MSWord document

Post by smarkturner » 16 Mar 2022, 15:11

Got it. Thanks for helping. You guys are such a valuable resource. I appreciate it.

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Append clipboard to MSWord document

Post by BoBo » 16 Mar 2022, 15:24

@flyingDman - a similar topic (sort of) :silent: Would you mind having a look at this: https://devblogs.microsoft.com/scripting/use-powershell-to-add-bulk-autocorrect-entries-to-word/ ... and how its Word.Application object to add/delete entries to an MSO.acl file can be converted into plain AHK?! Thx :)

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Append clipboard to MSWord document

Post by flyingDman » 16 Mar 2022, 17:41

@BoBo oWord.AutoCorrect.Entries is not a simple array and I have not even been able to get anything out of it...
14.3 & 1.3.7

BoBo
Posts: 6564
Joined: 13 May 2014, 17:15

Re: Append clipboard to MSWord document

Post by BoBo » 16 Mar 2022, 22:53

Thx :)

Auto HA
Posts: 1
Joined: 10 Aug 2022, 06:57

Re: Append clipboard to MSWord document

Post by Auto HA » 10 Aug 2022, 07:06

Hi mikeyww! and @flyingDman :wave:
Is there anyway to append the pasted contents instead of overwriting?
The below script is working, but doesnot append the file like your script in the beginning of the text...

Code: Select all

        file = C:\Users\smark\Desktop\Test.docx 
        oWord := ComObjCreate("Word.Application"), oWord.Visible := False
        doc   := oWord.Documents.Open(file)
        oWord.Selection.PasteAndFormat(16), doc.Save(), doc.Close(), oWord.Quit() 
Do you have any idea how the above code can be modified to append text without overwriting and loosing formatting?
The above was my earlier question the below is a mod:
I tested the script but I saw that it is appending but from top, not from expected bottom.
Also, is there any way to store the multiple clipboard contents in to a variable and pasting those multiple items at one go into the Docx file?

User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Append clipboard to MSWord document

Post by flyingDman » 10 Aug 2022, 12:42

First off, pasteandformat applies to tables and cells and not to text and images. As indicate above just paste seems the better way to go. To paste at the end of the document try this:

Code: Select all

file := test.docx"
oWord := ComObjCreate("Word.Application")
oWord.Visible := False
oWord.Documents.Open(file)
oWord.Selection.EndKey(wdStory := 6)
oWord.Selection.Paste
oWord.Quit(-1)
Re: your last question, are you trying to accumulate the content of various successive clipboards and then paste it all to a document? I guess you would need to create a temporary document or variable to hold the various successive clipboards and then, when done, paste the whole thing to your document. Maybe you can elaborate on what you want to do (and what the these clipboards will contain).
14.3 & 1.3.7

Post Reply

Return to “Ask for Help (v1)”