首页 > 解决方案 > 你能控制另一个程序.exe吗?我试过使用发送键,但这不可靠

问题描述

Private Const sw_restore = 9

#If VBA7 Then

Private Declare PtrSafe Function showwindow Lib "user32" (ByVal hwnd As Long, ByVal ncmdshow As Long) As Long
Private Declare PtrSafe Function isiconic Lib "user32.dll" (ByVal hwnd As Long) As Long
#Else
Private Declare Function showwindow Lib "user32" (ByVal hwnd As Long, ByVal ncmdshow As Long) As Long
Private Declare Function isiconic Lib "user32.dll" (ByVal hwnd As Long) As Long
#End If

Sub openProgram()

Dim strDirectory As String
Dim pID As Variant, sh As Variant
strDirectory = "H:\LocalStorage\Program.exe"
Set sh = CreateObject("shell.application")
For Each w In sh.Windows
If w.document.folder.self.Path = strDirectory Then
If CBool(isiconic(w.hwnd)) Then
w.Visible = False
w.Visible = True
End If
Exit Sub
End If
Next
pID = Shell("H:\LocalStorage\Program.exe", vbNormalFocus)

waittime 3000
Application.SendKeys "{Right}", True
waittime 3000
Application.SendKeys "{Right}", True
waittime 3000
Application.SendKeys "{Right}", True
waittime 3000
Application.SendKeys "{Right}", True
waittime 3000
Application.SendKeys "{Right}", True
waittime 3000
Application.SendKeys "{Right}", True
waittime 3000
Application.SendKeys "{Right}", True
waittime 3000
Application.SendKeys "{Right}", True
waittime 3000
Application.SendKeys "{%}", True

End Sub

Function waittime(ByVal milliseconds As Double)
Application.Wait (Now() + milliseconds / 24 / 60 / 60 / 1000)
End Function

我需要 {%} 或 {END} 键来注册程序以跳到下一行选项。然后向右走两次,然后在 Reports 上按 Enter。然后向下几次以创建新报告。然后继续使用粗略且不可靠的发送键,直到我可以自动化我每周需要运行的每个报告。节省一些时间。我更喜欢使用比 sendkeys 更强大的东西。还有其他可行的选择吗?谢谢

标签: excelvbasendkeys

解决方案


推荐阅读