gpt4 book ai didi

excel - 使用 Shell 打开应用程序,然后等待它打开以继续执行命令

转载 作者:行者123 更新时间:2023-12-04 20:14:35 24 4
gpt4 key购买 nike

基本上我想复制 Windows 7 风格的截图工具,所以当你打开它时,它会准备好截图。但在 Windows 8 中,它所做的只是打开程序,您必须按 CTRL+N 或单击新建。我想避免使用 Sleep(100) 或其他时间函数,因为它在某些负载下对于不同的系统可能是不可靠的。我尝试循环

While error <> 0
AppActivate "Snipping Tool", True
application.SendKeys "^N", True
exit sub
Wend

好的。这不是我使用的,但它是相同的。问题在于它不断给出 VB 调试错误(这很好,除了这个实例)并破坏了这个可能的解决方案。在能够运行之前等待shell打开程序的任何其他想法
Application.SendKeys "^N", True

我也尝试过使用
shell.Run FileLocation, 1 , True

但是在它关闭之后运行sendkeys!这几乎是我想要的。

编辑:下面的工作代码
//Used to wait for 200 milliseconds in the Sub.
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Function IsExeRunning(sExeName As String, Optional sComputer As String = ".") As Boolean
On Error GoTo Error_Handler
Dim objProcesses As Object

Set objProcesses = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputer & "\root\cimv2").ExecQuery("SELECT * FROM Win32_Process WHERE Name = '" & sExeName & "'")
If objProcesses.Count <> 0 Then IsExeRunning = True

Error_Handler_Exit:
On Error Resume Next
Set objProcesses = Nothing
Exit Function

Error_Handler:
MsgBox "The following error has occured." & vbCrLf & vbCrLf & _
"Error Number: IsExeRunning" & vbCrLf & _
"Error Description: " & Err.Description, _
vbCritical, "An Error has Occured!"
Resume Error_Handler_Exit

End Function

Sub SnipTool()

Dim wsh As Object
Set wsh = VBA.CreateObject("WScript.Shell")

wsh.Run "C:\Windows\sysnative\SnippingTool.exe"
X = 0
Select Case Mid(Application.OperatingSystem, 21)
Case 6.02
Do Until IsExeRunning("SnippingTool.exe") = True Or X = 200
//added Timeout if snipping tool fails to load. Prevents Excel freezing.
X = X + 1
Loop
Sleep (200)
AppActivate "Snipping Tool", True
Application.SendKeys "^N", True
End Select

End Sub

最佳答案

您可以启动 Exe,然后从这里获取 IsExeRunning 函数:

http://www.devhut.net/2014/10/20/vba-wmi-determine-if-a-process-is-running-or-not/

并循环执行直到 IsExeRunning(SnippingToolExecutable)

关于excel - 使用 Shell 打开应用程序,然后等待它打开以继续执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30469001/

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