1

I have a VSTO addin that displays a a dialog box with buttons yes no and cancel. I want the form to close anytime cancel or the X are clicked. I also want the application to quit when the form is closed. Here is my code:

        var frm = new Form1();
        DialogResult res = frm.ShowDialog();

        if (client != null)
        {
            if (res == DialogResult.Yes)
            {
                path = DRIVE_LETTER + ":/Clients/" + client + "/Correspondence/";
            }
            else if (res == DialogResult.No)
            {
                path = DRIVE_LETTER + ":/Clients Project History/" + client + "/Correspondence/";
            }
            else if (res == DialogResult.Cancel)
            {
                frm.Close();
            }
            else
            {
                frm.Close();
            }

And then my form closing event handler:

    private void Form1_Closing(object sender, CancelEventArgs e)
    {
        Application.Exit();
    }

But it doesn't seem to work. Microsoft.Office.Interop.Outlook.Application doesn't have an Exit method. How can I do the equivalent from within VSTO? I want my application to stop executing completely when those forms are canceled/closed.

Thanks

EDIT: can anyone provide an example of quitting the addin. Or stopping all execution if a certain condition is met, like Pyton's sys.exit(). I don't want outlook to close, just the addin to stop execution. Not even unload, just stop.

ss7
  • 2,902
  • 7
  • 41
  • 90

1 Answers1

2

If you need to shut down Outlook you may use the Quit method of the Application class. The associated Outlook session will be closed completely; the user will be logged out of the messaging system and any changes to items not already saved will be discarded.

But if you need to shut down the add-in (not the host application) you can:

  1. Disable all event handlers and UI controls. To get the job done you may check out the global boolean variable which can indicate the state of the add-in (allowed to run or not).
  2. The Connect property of the ComAddIn class allows to set the state of the connection for the specified COMAddIn object. The property returns true if the add-in is active; it returns false if the add-in is inactive. An active add-in is registered and connected; an inactive add-in is registered but not currently connected.

    Outlook.Application outlook = new Outlook.Application();
    
    if (outlook.Application.COMAddIns.Item("OutlookAddIn").Connect)
    {
        outlook.Application.COMAddIns.Item("OutlookAddIn").Connect = false;
    }
    else
    {
        outlook.Application.COMAddIns.Item("OutlookAddIn").Connect = true;
    }
    
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Forgive my stupidity, would it be possible to show me a short example of what you mean for numbers 1 and 2? – ss7 May 27 '15 at 15:33
  • Which is the boolean global that inidicates the addin state? – ss7 May 29 '15 at 18:16
  • You need to use your own (define it yourself). – Eugene Astafiev May 29 '15 at 20:50
  • Yeah I haven't a clue how to do that, any examples or guides you can give me? I looked over your Connect docs but still can't figure it out. Sorry :( – ss7 Jun 01 '15 at 19:55
  • Outlook.Application OutLook = new Outlook.Application(); if (OutLook.Application.COMAddIns.Item("OutlookAddIn").Connect) { Outlook.Application.COMAddIns.Item("OutlookAddIn").Connect = false; } else { Outlook.Application.COMAddIns.Item("OutlookAddIn").Connect = true; } – Eugene Astafiev Jun 01 '15 at 20:01