Basically I want to replicate the Windows 7 style Snipping Tool so when you open it, it gets a snip ready. But in Windows 8, all it does is open the program and you either have to press CTRL+N or click New. I would like to avoid using Sleep(100) or other time functions as it can be unreliable for different systems under certain loads. I tried looping
While error <> 0
AppActivate "Snipping Tool", True
application.SendKeys "^N", True
exit sub
Wend
Ok. That's not what I used, but it's along the same lines. The problem with that is it kept giving VB Debug errors (which is good and all, except for this instance) and ruined this possible solution. Any other ideas to wait for the shell to open the program before being able to run
Application.SendKeys "^N", True
I have also tried using
shell.Run FileLocation, 1 , True
but that runs the sendkeys AFTER it closes! It's nearly what I want.
EDIT: WORKING CODE BELOW
//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