Create msgbox to confirm before Outlook closes

zoso

Well-known Member
Joined
Oct 23, 2003
Messages
725
Hi there!

I have the following code:
Code:
Private Sub Application_Quit()
    Dim Response As Integer
    Dim Title As String
    Dim Message As String

   Title = "Confirm"
   Message = "Do you really want to quit Outlook?"
   Response = MsgBox(Message, vbYesNo, Title)
   If Response = vbNo Then
       ???????????????
   End If
End Sub
What I'm after is some code in place of the ???????? so that Outlook remains open.

Hope you can help - and thanks!
 

Some videos you may like

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.

NateO

Legend
Joined
Feb 17, 2002
Messages
9,700
Hello,

I don't understand which part of your code could close Outlook?

If it's hidden, just make it visible... :)
 

zoso

Well-known Member
Joined
Oct 23, 2003
Messages
725
Hi Nate - thanks for trying to help me out here!

All I'm trying to do is stop myself closing Outlook by mistake and creating a yes/no msgbox to do so.

If I say 'Yes', then Outlook closes, if 'No' then Outlook stays open.

I hope that's clear?

Thanks again!
 

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
You can't do this in Outlook, the Object Model does not support it. This is known by MS and has been requested in future versions. Whether or not they add this type of functionality is not yet known (by myself anyway), but I certainly hope it does get done.

I'm actually facing the same issue. I'm looking into disabling the exit button, but it's proving to be quite difficult. The commandbars object is not like other office application objects, as it's located in a different object altogether (Application.ActiveExplorer).
 

zoso

Well-known Member
Joined
Oct 23, 2003
Messages
725

ADVERTISEMENT

Zack - thanks a lot for replying.

From my point of view Excel's coding is light years ahead of Outlook and you could be fooled into thinking it was not part of the same 'Office Family' :wink:
 

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
It is light years ahead of Outlooks Object Model. Programming in Outlook - and Publisher even - is quite the headache. They are just so under-developed it's not even funny.

As I said earlier, I'm working on this issue as well. If I get anything of substance I'll post it here for you. But I wouldn't hold my breath. :(
 

zoso

Well-known Member
Joined
Oct 23, 2003
Messages
725

ADVERTISEMENT

Zack - thanks, and good luck!
 

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
Zoso, I recommend you download OutlookSpy. It's probably the best way to view the Object Model and view any attributed properties along with it.

As I don't think there is any way we can stop somebody from closing outlook, this brought my search into another path: disable the Exit and close button. Of course this won't stop somebody from ending the OUTLOOK.EXE process at all, but it's a start.

To disable the Exit from the File menu, run this code in your ThisOutlookSession module ...

<font face=Tahoma New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Application_Startup()
    <SPAN style="color:#00007F">With</SPAN> Application.ActiveExplorer.CommandBars.Item("Menu Bar")
        .Controls.Item("&File").Controls.Item("E&xit").Enabled = <SPAN style="color:#00007F">False</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

What I am thinking of doing, as I want (as administrator) the right to close outlook, but in order to do this I think a password should be supplied. I'm working right now on two things, 1) creating a shortcut key with a password structure to allow administrators only to exit the program, and 2) disabling the 'X' close button. For the second, there is no way to do this with VBA, so I'm researching some API to see if I can retro-fit it to my cause.

Will post when/if I have more. Good luck!
 

zoso

Well-known Member
Joined
Oct 23, 2003
Messages
725
Excellent Zack, thanks a lot for keeping me posted!
 

Zack Barresse

MrExcel MVP
Joined
Dec 9, 2003
Messages
10,881
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
  2. Mobile
  3. Web
Okay, delved a little more (making some decent progress today). What I've done is decided that I wanted a little more interactivity between myself and Outlook, and I felt the best way to do that was to go the custom menu route. So this code will give you a custom menu called 'Admin' after your Help menu, which has two options, Enable Exit and Disable Exit.

This requires changing some code in your ThisOutlookSession module and creating 2 new Standard Modules.


In ThisOutlookSession paste:


<font face=Tahoma New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Application_Startup()

    <SPAN style="color:#00007F">Call</SPAN> DisableExit
    <SPAN style="color:#00007F">Call</SPAN> CreateMenu
    
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>


In module Mod_Menu (just my naming convention:


<font face=Tahoma New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> APP_NAME <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "Admin"

<SPAN style="color:#00007F">Sub</SPAN> CreateMenu()

    <SPAN style="color:#00007F">Dim</SPAN> CB <SPAN style="color:#00007F">As</SPAN> CommandBar
    <SPAN style="color:#00007F">Dim</SPAN> Menu <SPAN style="color:#00007F">As</SPAN> CommandBarPopup
    <SPAN style="color:#00007F">Dim</SPAN> Item <SPAN style="color:#00007F">As</SPAN> CommandBarButton
    <SPAN style="color:#00007F">Dim</SPAN> Pos <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>
    
    <SPAN style="color:#00007F">Call</SPAN> DeleteMenu
    
    <SPAN style="color:#00007F">Set</SPAN> CB = Application.ActiveExplorer.CommandBars.Item("Menu Bar")
    Pos = CB.Controls.Count + 1
    
<SPAN style="color:#007F00">'Main Menu</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> Menu = CB.Controls.Add(Type:=msoControlPopup, before:=Pos)
    Menu.Caption = APP_NAME
    Menu.BeginGroup = <SPAN style="color:#00007F">True</SPAN>
    
<SPAN style="color:#007F00">'Menu Items</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> Item = Menu.Controls.Add(Type:=msoControlButton)
    <SPAN style="color:#00007F">With</SPAN> Item
        .Caption = "&Enable Exit"
        .FaceId = 1 <SPAN style="color:#007F00">'set as desired</SPAN>
        .OnAction = "EnableExit"
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> Item = Menu.Controls.Add(Type:=msoControlButton)
    <SPAN style="color:#00007F">With</SPAN> Item
        .Caption = "&Disable Exit"
        .FaceId = 1 <SPAN style="color:#007F00">'set as desired</SPAN>
        .OnAction = "DisableExit"
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
    
    <SPAN style="color:#00007F">Set</SPAN> Item = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> Menu = <SPAN style="color:#00007F">Nothing</SPAN>
    <SPAN style="color:#00007F">Set</SPAN> CB = <SPAN style="color:#00007F">Nothing</SPAN>
    
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Sub</SPAN> DeleteMenu()
    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN>
    Application.ActiveExplorer.CommandBars.Item("Menu Bar").Controls(APP_NAME).Delete
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>


In module Mod_Routines (just my naming convention:


<font face=Tahoma New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Const</SPAN> PWD <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN> = "admin"

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Sub</SPAN> DisableExit()
    <SPAN style="color:#00007F">Dim</SPAN> tmp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    tmp = InputBox("Enter Password:", "Password")
    <SPAN style="color:#00007F">If</SPAN> Len(tmp) = 0 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    <SPAN style="color:#00007F">If</SPAN> tmp <> PWD <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Sorry!  Wrong password.  Try again.", vbInformation, "ERROR!"
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    Application.ActiveExplorer.CommandBars.Item("Menu Bar" _
        ).Controls.Item("&File").Controls.Item("E&xit").Enabled = <SPAN style="color:#00007F">False</SPAN>
    MsgBox "File | Exit is now disabled!", vbExclamation
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Sub</SPAN> EnableExit()
    <SPAN style="color:#00007F">Dim</SPAN> tmp <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
    tmp = InputBox("Enter Password:", "Password")
    <SPAN style="color:#00007F">If</SPAN> Len(tmp) = 0 <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    <SPAN style="color:#00007F">If</SPAN> tmp <> PWD <SPAN style="color:#00007F">Then</SPAN>
        MsgBox "Sorry!  Wrong password.  Try again.", vbInformation, "ERROR!"
        <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
    Application.ActiveExplorer.CommandBars.Item("Menu Bar" _
        ).Controls.Item("&File").Controls.Item("E&xit").Enabled = <SPAN style="color:#00007F">True</SPAN>
    MsgBox "File | Exit is now enabled!", vbExclamation
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>


Works great for me in OL 2003, do not have any previous versions here for me to test on. I'm working on that API call to disable the X button - still not sure if that can be done or not though.

Hope this helps you Zoso.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,388
Messages
5,601,359
Members
414,447
Latest member
CRAVIN

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top