1
votes

I am trying to create a VBScript that inserts some text from the clipboard into an existing (and loaded) Word document. I have tried numerous approaches without success (including Selection.Paste) but Word (2013) will just not paste. Whilst trying to diagnose the problem, I have got down to this minimal script.

Set objWord = GetObject(, "Word.Application")
objWord.Application.Activate
Dim objShell
Set objShell = CreateObject("WScript.Shell")
objShell.SendKeys "Arggh 1"
objShell.SendKeys "^V"
objShell.SendKeys "Arggh 2"

If I place some text on clipboard (I have got down to basic unformatted text, I originally started with MathML but that is another story), the above script generates

Arggh 1Arggh 2

with the text left on the clipboard. If I then press Ctrl+V, the clipboard text gets pasted as expected.

I feel like I am missing something obvious. Any help most welcome.

3
Can you show your non-working non-sendkeys code? - Tim Williams
To be honest, I have tried dozens of combinations - some I know are wrong, some that should be right. I originally went to SendKeys to get a baseline of something that would work - and that didn't either! I tried objWord.Paste objWord.ActiveDocument.Paste objWord.ActiveDocument.Selection.Paste objWord.Selection.Paste and a whole heap of PasteSpecials. After nothing worked, I tried the (less desirable) SendKeys approach. - Paul Hooper
objWord.Selection.Paste worked for me. - Ansgar Wiechers
Hmmm. It did for me as well. See answer below. - Paul Hooper
This issue is now solved. I will post the answer properly as soon as StackOverflow lets me but in summary. 1. objWord.Selection.Paste was failing due to a strange timing issue in the software we are working on. In effect, the script was trying to paste something that the clipboard didn't know about. We have not yet solved this issue but we now understand it and can work on it. 2. Sendkeys is still failing but I do not know why. We will not be working on this as the problem is effectively solved. Thanks everyone. - Paul Hooper

3 Answers

0
votes

I had similar issues before (I need Ctrl-C), but this should work for you:

objShell.SendKeys "^{V}"

0
votes

Avoid unsafe keystroke simulation when you have other programmatic facilities:

Set objWord = GetObject(, "Word.Application")
objWord.Selection.TypeText "Arggh 1"
objWord.Selection.Paste
objWord.Selection.TypeText "Arggh 2"

Also note objWord.Application is same as just objWord.

0
votes

Ok. This is partially solved.

  1. The initial problem - objWord.Selection.Paste not working - ended up being a strange timing interaction between the clipboard and the software creating the VBS script. Basically, the script is trying to do the pasting before the clipboard knows about the data. Once I took away the timing issue, objWord.Selection.Paste is working as expected. We have not solved this problem yet but at least we now know what is going on and can work on it. From a purely pragmatic point of view, the issue raised above is no longer important.

  2. SendKeys pasting is still not working and I don't know why. I won't be spending any more time on it as the problem I have has now morphed.

This is one of those rare times that trying to make things simpler for diagnostic purposes has actually made things more difficult.