1

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.

4

3 回答 3

0

我之前遇到过类似的问题(我需要 Ctrl-C),但这应该对你有用:

objShell.SendKeys "^{V}"

于 2013-10-03T05:24:50.980 回答
0

好的。这部分解决了。

  1. 最初的问题 - objWord.Selection.Paste 不起作用 - 最终成为剪贴板和创建 VBS 脚本的软件之间奇怪的时间交互。基本上,脚本试图在剪贴板知道数据之前进行粘贴。一旦我解决了时间问题,objWord.Selection.Paste 就会按预期工作。我们还没有解决这个问题,但至少我们现在知道发生了什么并且可以解决它。从纯粹务实的角度来看,上面提出的问题已不再重要。

  2. SendKeys 粘贴仍然无法正常工作,我不知道为什么。我不会再花时间在这上面了,因为我现在遇到的问题已经发生了变化。

这是为了诊断目的而试图使事情变得更简单实际上使事情变得更加困难的罕见时期之一。

于 2013-10-03T13:56:52.677 回答
0

当您有其他编程工具时,请避免不安全的击键模拟:

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

还注意objWord.Application与 just 相同objWord

于 2013-10-03T09:10:01.160 回答