Hi,
when i am using below code with windows application it always fires WOrkBookOpen event.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Microsoft.Office.Interop.Excel.Application app;
private void button1_Click(object sender, EventArgs e)
{
app = new Microsoft.Office.Interop.Excel.Application();
app.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(app_WorkbookOpen);
app.WorkbookActivate += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookActivateEventHandler(app_WorkbookActivate);
}
void app_WorkbookActivate(Microsoft.Office.Interop.Excel.Workbook Wb)
{
MessageBox.Show(Wb.FullName);
}
void app_WorkbookOpen(Microsoft.Office.Interop.Excel.Workbook Wb)
{
MessageBox.Show(Wb.FullName);
}
private void button2_Click(object sender, EventArgs e)
{
app.Quit();
Marshal.FinalReleaseComObject(app);
}
}
But when I want to fire same event using windows service its not firing. Below is the code used for service. I am creating Excel interop object in OnStart() of service and attaching same event. But after installation of service this event doesnt fire. whereas in same windows application it fires up. Here for testing purpose i am creating FIle on my disk to check if event is firing or not.
public partial class Service1 : ServiceBase
{
Microsoft.Office.Interop.Excel.Application excel;
public Service1()
{
InitializeComponent();
}
protected override void OnStart(string[] args)
{
try
{
File.Create("C:\\SampleService\\Start - " + DateTime.Now.Ticks.ToString());
excel = new Microsoft.Office.Interop.Excel.Application();
excel.WorkbookOpen += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookOpenEventHandler(excel_WorkbookOpen);
excel.WorkbookActivate += new Microsoft.Office.Interop.Excel.AppEvents_WorkbookActivateEventHandler(excel_WorkbookActivate);
File.Create("C:\\SampleService\\Start - " + DateTime.Now.Ticks.ToString());
}
catch (Exception e)
{
using (StreamWriter stream = new StreamWriter(@"C:\SampleService\Err.txt", true))
{
stream.Write(e.Message);
}
}
}
void excel_WorkbookActivate(Microsoft.Office.Interop.Excel.Workbook Wb)
{
File.Create("C:\\SampleService\\EXCEL - " + DateTime.Now.Ticks.ToString());
}
public void excel_WorkbookOpen(Microsoft.Office.Interop.Excel.Workbook Wb)
{
File.Create("C:\\SampleService\\EXCEL - " + DateTime.Now.Ticks.ToString());
}
protected override void OnStop()
{
if (excel != null)
{
excel.Quit();
Marshal.FinalReleaseComObject(excel);
}
}
}
I have also using serviceInstaller and i am installling service on machine. I am giving proper rights to service to create object of Excel.Application com component.
Is anyone came across such issue? Or do you find I am missing anything?
Thanks Paresh