Append clipboard to MSWord document
-
- Posts: 7
- Joined: 14 Mar 2022, 19:15
Append clipboard to MSWord document
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.
Re: Append clipboard to MSWord document
Welcome to this AutoHotkey forum!
For pictures: viewtopic.php?p=442238#p442238 • https://docs.microsoft.com/en-us/office/vba/api/word.inlineshapes.addpicture
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()
}
-
- Posts: 7
- Joined: 14 Mar 2022, 19:15
Re: Append clipboard to MSWord document
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!
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.
- flyingDman
- Posts: 2821
- Joined: 29 Sep 2013, 19:01
Re: Append clipboard to MSWord document
Did you try:
?
Code: Select all
oWord.selection.paste
14.3 & 1.3.7
-
- Posts: 7
- Joined: 14 Mar 2022, 19:15
Re: Append clipboard to MSWord document
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!
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!
-
- Posts: 7
- Joined: 14 Mar 2022, 19:15
Re: Append clipboard to MSWord document
Got it!!!
Many thanks!!!!!!
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()
- flyingDman
- Posts: 2821
- Joined: 29 Sep 2013, 19:01
Re: Append clipboard to MSWord document
oWord.selection.paste works as well.
And, btw, doc.Save(), doc.Close(), oWord.Quit() can be replaced by oWord.Quit(-1)
And, btw, doc.Save(), doc.Close(), oWord.Quit() can be replaced by oWord.Quit(-1)
14.3 & 1.3.7
-
- Posts: 7
- Joined: 14 Mar 2022, 19:15
Re: Append clipboard to MSWord document
Got it. Thanks for helping. You guys are such a valuable resource. I appreciate it.
Re: Append clipboard to MSWord document
@flyingDman - a similar topic (sort of) 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
- flyingDman
- Posts: 2821
- Joined: 29 Sep 2013, 19:01
Re: Append clipboard to MSWord document
@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
Re: Append clipboard to MSWord document
The above was my earlier question the below is a mod:Hi mikeyww! and @flyingDman
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...Do you have any idea how the above code can be modified to append text without overwriting and loosing formatting?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()
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?
- flyingDman
- Posts: 2821
- Joined: 29 Sep 2013, 19:01
Re: Append clipboard to MSWord document
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:
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).
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)
14.3 & 1.3.7