I am using translation software. Sorry for any mistakes.
When executing the code below, it switches from Excel to another window (Explorer, Chrome, etc.) without permission before the message box is displayed.
I think that it is easy to wake up immediately after opening another window and returning to Excel with Alt + Tab.
How can I prevent the active window from switching while controlling input and screen transitions? If anyone knows a solution, I would appreciate it if you could let me know.
Sub TEST()
Application.Interactive = False
' Transition to "RUNNING" sheet'
Workbooks("TEST.xlsm").Activate
Worksheets("RUNNING").Activate
Application.ScreenUpdating = False
' Start connection confirmation batch "ping.bat"'
TaskID = Shell("c:\test\ping.bat", 2)
hProc = OpenProcess(PROCESS_ALL_ACCESS, False, TaskID)
If OpenProcess(PROCESS_ALL_ACCESS, False, TaskID) <> vbNull Then
Call WaitForSingleObject(hProc, INFINITE)
CloseHandle hProc
End If
' If you can’t connect, transition to "ERROR" sheet'
FILECHK = Dir("c:\test\PINGERR.txt", 0)
DoEvents
If FILECHK = "PINGERR.txt" Then
Workbooks("TEST.xlsm").Activate
Worksheets("ERROR").Activate
MsgBox "ERROR!!" '←I would like to avoid switching here.'
DoEvents
Application.ScreenUpdating = True
Application.Interactive = True
End
Else
MsgBox "Nomarl End!" '←I would like to avoid switching here.'
End If
' If you can connect, transition to "OK" sheet'
Workbooks("TEST.xlsm").Activate
Worksheets("OK").Activate
Application.ScreenUpdating = True
Application.Interactive = True
End Sub
What I have tried:
1. Commenting out “Application.Interactive = False”.
2. Put “DoEvents” before “Application.Interactive = False, True” and “MsgBox "ERROR!!".”
3. Put a waiting time with “sleep” before displaying the message box.
Only “1” was effective, but I am looking for other methods because it will be in a state where the user can operate.