Hi Rob,Alex,
Based on my discussion with a Office Team Senior Engineer, here is the
result.
First off, this is a addin and methods that through exception back to
Office can be caught by Office and therfore not be "unhandled" to start
with. Second, if you are using the COM Shim Wizard to generate security
context for the managed Com Addin, then yes you are in a new (non-default)
appdomain. Third, the documentation talks about WinForm applications, which
I presume to mean host applications where the CLR controls the message loop
and app startup (etc.), not simply a WinForm shown from DLL in an unmanaged
host. I don't think there is a way to set just a filter for managed code. I
think this applied to entire application, which is problematic since Office
has its own filter (for watson reporting and document recovery) which you
don't want to mess with.
Based on my test, we just need to set the filter to the
Application.ThreadException one time even if there are more than one form.
Also according to the document
AppDomain.UnhandledException Event [C#]
This event occurs only for the application domain that is created by the
system when an application is started. If an application creates additional
application domains, specifying a delegate for this event in those
applications domains has no effect.
So the AppDomain.UnhandledException is not available in the Addin. But
even if we throw Exceptoin in the Addin(Except winform), the exception
will not propagate to the OS, and it will be caught by the Office own
filter.
So I think the best practice is to enbrace the "problem" in a try..catch
block everywhere.
Anyway, here I post the C# code per your request.
[Addin]
namespace MyAddin7
{
using System;
using Office = Microsoft.Office.Core;
using Extensibility;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
using System.Diagnostics;
[GuidAttribute("5B4E30C3-0E48-4749-8342-F097935ADB0A"),
ProgId("MyAddin7.Connect")]
public class Connect : Object, Extensibility.IDTExtensibility2
{
public Connect()
{
}
public void OnConnection(object application,
Extensibility.ext_ConnectMode connectMode, object addInInst, ref
System.Array custom)
{
Debug.WriteLine("OnConnection");
exApp = application as Excel.Application;
addInInstance = addInInst;
}
public void OnDisconnection(Extensibility.ext_DisconnectMode
disconnectMode, ref System.Array custom)
{
cbb.Delete(true);
cb.Delete();
}
public void OnAddInsUpdate(ref System.Array custom)
{
}
Office.CommandBar cb=null;
Office.CommandBarButton cbb=null;
Office.CommandBarButton cbbt=null;
public void OnStartupComplete(ref System.Array custom)
{
object oMissing = System.Reflection.Missing.Value;
cb=exApp.CommandBars.Add("Test",Office.MsoBarPosition.msoBarFloating,oMissin
g,oMissing);
cbb =
(Office.CommandBarButton)cb.Controls.Add(Office.MsoControlType.msoControlBut
ton,oMissing,oMissing,oMissing,oMissing);
cbb.Caption = "ShowForm";
cbb.Style = Office.MsoButtonStyle.msoButtonCaption;
cbb.Click+=new
Microsoft.Office.Core._CommandBarButtonEvents_ClickEventHandler(cbb_Click);
}
public void OnBeginShutdown(ref System.Array custom)
{
}
private Excel.Application exApp=null;
private object addInInstance;
private void curAppDomain_UnhandledException(object sender,
UnhandledExceptionEventArgs e)
{
Debug.WriteLine("curAppDomain_UnhandledException: Catch Exception " +
((Exception)e.ExceptionObject).ToString());
}
private void cbb_Click(Microsoft.Office.Core.CommandBarButton Ctrl, ref
bool CancelDefault)
{
Form1 fm = new Form1();
fm.Show();
Form2 fm2 = new Form2();
fm2.Show();
}
}
}
[Form1]
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Diagnostics;
namespace MyAddin7
{
public class Form1 : System.Windows.Forms.Form
{
//.........................................
//.........................................
private void button1_Click(object sender, System.EventArgs e)
{
throw new Exception("My Test Exception from winform");
}
private void Form1_Load(object sender, System.EventArgs e)
{
Application.ThreadException+=new
System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
}
private void Application_ThreadException(object sender,
System.Threading.ThreadExceptionEventArgs e)
{
Debug.WriteLine("Application_ThreadException: Catch Exception:" +
e.Exception.ToString());
}
}
}
[Form2]
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
namespace MyAddin7
{
//.................................................................
//.................................................................
private void button1_Click(object sender, System.EventArgs e)
{
throw new Exception("From winform 2");
}
}
}
Best regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! -
www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.