2

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

1 Answer 1

2

You could start the Exe then take the IsExeRunning function from here:

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

And loop on Do until IsExeRunning(SnippingToolExecutable)

Sign up to request clarification or add additional context in comments.

3 Comments

This seems like it would work! I'll confirm in the afternoon.
I couldn't get it to work. Windows 8 Excel freezes, but resumes after pressing escape. It gives an error and clicking Debug... highlights this in the link above: "Exit Function" for the Error_Handler_Exit label.
Nevermind I figured it out. It was looping forever because "Snipping Tool.exe" did not exist. It was "SnippingTool.exe"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.