简体   繁体   中英

VSTO Outlook 2013 Addin Quit

I have a VSTO addin that displays aa 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.

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; } 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM