When calling the command prompt with the shell function, the screen switching no longer occurs by calling it with 0 (vbHide).
However, we had to be careful because we could not take action when the command prompt did not exit.
Sheet protection is also effective if you just want to prevent users from typing.
When Application.Interactive = False, I want to prevent the active window from switching arbitrarily when displaying a message box.
Marino_Y
41
Reputation points
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.
Below is what I have tried.
- Commenting out “Application.Interactive = False”.
- Put “DoEvents” before “Application.Interactive = False, True” and “MsgBox "ERROR!!".”
- 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. Sub TEST()
' Transition to “RUNNING” sheet'Application.Interactive = False
Workbooks("TEST.xlsm").Activate
Worksheets("RUNNING").Activate
' Start connection confirmation batch “ping.bat”'Application.ScreenUpdating = False
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 too.'
End If ' If you can connect, transition to “OK” sheet'
Workbooks("TEST.xlsm").Activate
Worksheets("OK").Activate
End SubApplication.ScreenUpdating = True Application.Interactive = True
1 answer
Sort by: Most helpful
-
Marino_Y 41 Reputation points
2022-11-30T06:35:16.003+00:00