When creating an Excel Application there are some things to keep in mind. Recently I was creating some tests, which I’m not sure if they’d be "unit test", but they do test Excel. I created a fixture setup and a fixture tear down that would create my Excel Application object that I?d want to test again. The code I ended up with is shown below.
[TestFixture]
public class ConnectionManagement
{
public Application ExcelApplication { get; set; }
[TestFixtureSetUp]
public void CreateExcelAppAppropriately()
{
ExcelApplication = new Application();
ExcelApplication.Workbooks.Add(Type.Missing);
}
[TestFixtureTearDown]
public void QuitExcelAppAppropriately()
{
foreach(Workbook workbook in ExcelApplication.Workbooks)
{
workbook.Close(false, false, Type.Missing);
}
ExcelApplication.Quit();
}
}
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, “Courier New”, courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
Now if anyone has a better idea on how to test Excel and knowing the code will work against the actual Excel Application, PLEASE, let me know as this doesn’t feel like the best way to do this. I keep getting the sinking suspicion that there should be a better way to test Excel Application Addins, Spreadsheet code and such.
I think you may want to get the PID, (Process Id)/HWND (handle) for two reasons. One: there may be an instance of Excel already running that you do NOT want to terminate. Two: I have automated excel in the past and without a hard reference to the process, quitting the app was inconsistent. I remember having to force it to die using Task Manager before using the PID. You can use the System.Diagnostics namespace for the Process object. You may also try the Microsoft.Office.Interop.Excel.ApplicationClass
By the way when adding this comment I experienced a strange scenario. Clicking in the E-mail text box, OR tabbing to it via the keyboard forced my browser to navigate to your smugmug site. Matter of fact, just waving my mouse over the E-mail field or the Name field displays the smugmug link.