Question:
When a program is at run time, can I give it focus and bring it to the foreground via scripts?
Solution:
Yes. To do so, use the script below:
Sub CommandButton1_Click()
‘Using Viewer method: ExecuteExternalApp
Set Service = GetObject(“winmgmts:”)
strProgramPath = “C:\Windows\system32\calc.exe”
For Each Process in Service.InstancesOf(“Win32_Process”)
If Process.Name = “calc.exe” Then
running = True
End If
Next
If running Then
MsgBox “Calculator is already being executed!”
Set WshShell = CreateObject(“WScript.Shell”)
WshShell.AppActivate “calculator”
newkeys = “% R” ‘Maximiza objeto ativo
WshShell.SendKeys newkeys
Else
Application.ExecuteExternalApp strProgramPath, “”, “”, 1 ‘Can be replaced by objShell.Run strProgramPath
End If
End Sub
NOTE: You can also issue this command with Shell object (see the application attached to this article).