VBA - Stop program from quitting, minimize instead
I want to write a macro for Outlook that is called when I click the X. I want the Application_Quit() subroutine to stop the program from quitting, then minimize 开发者_运维知识库it instead. I can figure out the minimization, but how to I prevent it from quitting?
Not truly possible to prevent the close. You can tell based on the method signature of the Application_Quit() event:
Private Sub Application_Quit()
End Sub
A cancellable event will look like this:
Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)
End Sub
I searched around, and found a kind of cool hacky trick, though, if you don't mind the side-effect that Outlook automatically minimizes when it launches (found on this forum, copied and reformatted):
Trick is to add a macro that auto-minimizes Outlook when it starts:
Hit Alt-F11 to go to the VBA editor. Paste this sub in the "ThisOutlookSession" section:
Private Sub Application_Startup()
SendKeys ("% n")
End Sub
Now after Outlook loads it will minimize. I also use this next one to minimize instead of close when someone attempts to close outlook.
Private Sub Application_Quit()
Call Shell("C:\Program Files\Microsoft Office\OFFICE11\relaunchOL.bat" _
, vbHide)
End Sub
Create C:\Program Files\Microsoft Office\OFFICE11\relaunchOL.bat and put these two lines in it:
Ping 1.2.3.4 -n 1
"C:\Program Files\Microsoft Office\OFFICE11\outlook.exe"
This will give Outlook a couple of seconds to close, then relaunch itself, then minimize itself sinse you have the macro for minimize in startup.
精彩评论