问题描述: 在使用AppActivate函数激活应用程序窗口时,无法发送键盘按键或真正激活窗口。
解决方法:
使用SendKeys函数发送键盘按键。 示例代码:
AppActivate "Notepad" ' 激活记事本应用程序窗口
SendKeys "Hello World!" ' 发送键盘按键
使用Shell函数启动应用程序,并使用AppActivate函数激活窗口。 示例代码:
Dim appPath As String
appPath = "C:\Program Files\Notepad\notepad.exe" ' 应用程序路径
Shell appPath ' 启动应用程序
Application.Wait Now + TimeValue("00:00:01") ' 等待应用程序启动
AppActivate "Notepad" ' 激活记事本应用程序窗口
使用Windows API函数激活窗口。 示例代码:
Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal hWnd As LongPtr) As LongPtr
Dim appHwnd As LongPtr
appHwnd = FindWindow(vbNullString, "Notepad") ' 获取应用程序窗口句柄
If appHwnd <> 0 Then
SetForegroundWindow appHwnd ' 激活应用程序窗口
End If
请根据您的具体情况选择适合的解决方法。