gpt4 book ai didi

excel - SendKeys 并不总是有效

转载 作者:行者123 更新时间:2023-12-04 15:57:36 27 4
gpt4 key购买 nike

我正在使用 .SendKeys()在 Excel VBA 中将击键发送到我正在使用 shell .AppActive 激活的外部窗口方法。问题是 SendKeys只是行为不一致,有时发送 key ,有时不发送。我认为这与根据 MSDN 文档将 key 存储在缓冲区中有关。

如何解决这个问题?

最佳答案

您可以考虑使用 Autoit,它比 SendKeys 更可靠。

从下面的链接下载 Autoit
http://www.autoitscript.com/site/autoit/downloads/

添加引用autoit addin
AutoItX3 1.0 类型库

下面是打开计算器并输入 123456789 的示例代码

Sub sendkeys()

'Open a calc
StartCalculator

Dim au As New AutoItX3Lib.AutoItX3
au.AutoItSetOption "WinTitleMatchMode", 2

au.WinActivate "Calculator"

'send key strokes
au.ControlClick "Calculator", "", "Button5"
au.ControlClick "Calculator", "", "Button11"
au.ControlClick "Calculator", "", "Button16"
au.ControlClick "Calculator", "", "Button4"
au.ControlClick "Calculator", "", "Button10"
au.ControlClick "Calculator", "", "Button15"
au.ControlClick "Calculator", "", "Button3"
au.ControlClick "Calculator", "", "Button9"
au.ControlClick "Calculator", "", "Button14"

End Sub

Sub StartCalculator()
Dim Program As String
Dim TaskID As Double
On Error Resume Next
Program = "calc.exe"
TaskID = Shell(Program, 1)
If Err <> 0 Then
MsgBox "Can't start " & Program
End If
End Sub

enter image description here

关于excel - SendKeys 并不总是有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21039489/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com