Windows Service with Microsoft Office PIA assembly

S

sokolo

Hello,

I wrote a windows service application however it is not working. The
service is supposed to enter a new task every 5 sec within Microsoft
Outlook.

Here is the code for application:

public partial class CheckAccountsService : ServiceBase
{
private Timer timer = null;

public CheckAccountsService()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
EventLog.WriteEntry("It is starting!!!!", "It is
starting!!");
System.Threading.Thread.Sleep(10000);
//Debug.Write("x");
// TODO: Add code here to start your service
timer.Enabled = true;
timer.Start();
createTestTask();
}

protected override void OnStop()
{}

public enum Importance
{
High = 0,
Normal = 1,
Low = 2
}

public static void createTestTask()
{
CreateOutlookTask("bla bla ", DateTime.Parse("6/6/2006 1:00
PM"), Importance.High, "bla bla");
}

public static void CreateOutlookTask(string Body, DateTime
dueDate, Importance Importance, string Subject)
{
// Create an Outlook Application object.
Application outLookApp = new Application();
// Create a new TaskItem.
TaskItem newTask =
(TaskItem)outLookApp.CreateItem(OlItemType.olTaskItem);
// Configure the task at hand and save it.
newTask.Body = Body;
newTask.DueDate = dueDate;
switch (Importance)
{
case Importance.High:
newTask.Importance = OlImportance.olImportanceHigh;
break;
case Importance.Normal:
newTask.Importance =
OlImportance.olImportanceNormal;
break;
case Importance.Low:
newTask.Importance = OlImportance.olImportanceLow;
break;
}
newTask.ReminderSet = true;
newTask.ReminderTime = dueDate;
newTask.Subject = Subject;
newTask.Save();
}

private void ServiceTimer_Tick(object sender,
System.Timers.ElapsedEventArgs e)
{
this.timer.Stop();
createTestTask();
this.timer.Start();
}
}


I get the following errors :
********
Rejected Safe Mode action : Microsoft Office Outlook.

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.
********
AND
********
Service cannot be started. System.Runtime.InteropServices.COMException
(0x80080005): Retrieving the COM class factory for component with CLSID
{0006F03A-0000-0000-C000-000000000046} failed due to the following
error: 80080005.
at AccountRemainder.CheckAccountsService.CreateOutlookTask(String
Body, DateTime dueDate, Importance Importance, String Subject) in
C:\Documents and Settings\psokolowski\My Documents\Visual Studio
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
70
at AccountRemainder.CheckAccountsService.createTestTask() in
C:\Documents and Settings\psokolowski\My Documents\Visual Studio
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
55
at AccountRemainder.CheckAccountsService.OnStart(String[] args) in
C:\Documents and Settings\psokolowski\My Documents\Visual Studio
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
32
at
System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.
********

Any idea ?
 
N

Nicholas Paldino [.NET/C# MVP]

If you try to access outlook from an external application (for sending
mail, etc, etc) it throws up a dialog asking if you want to allow the
action. My guess is that this would be the case here as well.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,

I wrote a windows service application however it is not working. The
service is supposed to enter a new task every 5 sec within Microsoft
Outlook.

Here is the code for application:

public partial class CheckAccountsService : ServiceBase
{
private Timer timer = null;

public CheckAccountsService()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
EventLog.WriteEntry("It is starting!!!!", "It is
starting!!");
System.Threading.Thread.Sleep(10000);
//Debug.Write("x");
// TODO: Add code here to start your service
timer.Enabled = true;
timer.Start();
createTestTask();
}

protected override void OnStop()
{}

public enum Importance
{
High = 0,
Normal = 1,
Low = 2
}

public static void createTestTask()
{
CreateOutlookTask("bla bla ", DateTime.Parse("6/6/2006 1:00
PM"), Importance.High, "bla bla");
}

public static void CreateOutlookTask(string Body, DateTime
dueDate, Importance Importance, string Subject)
{
// Create an Outlook Application object.
Application outLookApp = new Application();
// Create a new TaskItem.
TaskItem newTask =
(TaskItem)outLookApp.CreateItem(OlItemType.olTaskItem);
// Configure the task at hand and save it.
newTask.Body = Body;
newTask.DueDate = dueDate;
switch (Importance)
{
case Importance.High:
newTask.Importance = OlImportance.olImportanceHigh;
break;
case Importance.Normal:
newTask.Importance =
OlImportance.olImportanceNormal;
break;
case Importance.Low:
newTask.Importance = OlImportance.olImportanceLow;
break;
}
newTask.ReminderSet = true;
newTask.ReminderTime = dueDate;
newTask.Subject = Subject;
newTask.Save();
}

private void ServiceTimer_Tick(object sender,
System.Timers.ElapsedEventArgs e)
{
this.timer.Stop();
createTestTask();
this.timer.Start();
}
}


I get the following errors :
********
Rejected Safe Mode action : Microsoft Office Outlook.

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.
********
AND
********
Service cannot be started. System.Runtime.InteropServices.COMException
(0x80080005): Retrieving the COM class factory for component with CLSID
{0006F03A-0000-0000-C000-000000000046} failed due to the following
error: 80080005.
at AccountRemainder.CheckAccountsService.CreateOutlookTask(String
Body, DateTime dueDate, Importance Importance, String Subject) in
C:\Documents and Settings\psokolowski\My Documents\Visual Studio
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
70
at AccountRemainder.CheckAccountsService.createTestTask() in
C:\Documents and Settings\psokolowski\My Documents\Visual Studio
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
55
at AccountRemainder.CheckAccountsService.OnStart(String[] args) in
C:\Documents and Settings\psokolowski\My Documents\Visual Studio
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
32
at
System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.
********

Any idea ?
 
S

sokolo

However I have tried it with windows forms application and it worked
fine. It gets complicated when I try to use the code to enter new
tasks in outlook from windows service application.
If you try to access outlook from an external application (for sending
mail, etc, etc) it throws up a dialog asking if you want to allow the
action. My guess is that this would be the case here as well.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hello,

I wrote a windows service application however it is not working. The
service is supposed to enter a new task every 5 sec within Microsoft
Outlook.

Here is the code for application:

public partial class CheckAccountsService : ServiceBase
{
private Timer timer = null;

public CheckAccountsService()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
EventLog.WriteEntry("It is starting!!!!", "It is
starting!!");
System.Threading.Thread.Sleep(10000);
//Debug.Write("x");
// TODO: Add code here to start your service
timer.Enabled = true;
timer.Start();
createTestTask();
}

protected override void OnStop()
{}

public enum Importance
{
High = 0,
Normal = 1,
Low = 2
}

public static void createTestTask()
{
CreateOutlookTask("bla bla ", DateTime.Parse("6/6/2006 1:00
PM"), Importance.High, "bla bla");
}

public static void CreateOutlookTask(string Body, DateTime
dueDate, Importance Importance, string Subject)
{
// Create an Outlook Application object.
Application outLookApp = new Application();
// Create a new TaskItem.
TaskItem newTask =
(TaskItem)outLookApp.CreateItem(OlItemType.olTaskItem);
// Configure the task at hand and save it.
newTask.Body = Body;
newTask.DueDate = dueDate;
switch (Importance)
{
case Importance.High:
newTask.Importance = OlImportance.olImportanceHigh;
break;
case Importance.Normal:
newTask.Importance =
OlImportance.olImportanceNormal;
break;
case Importance.Low:
newTask.Importance = OlImportance.olImportanceLow;
break;
}
newTask.ReminderSet = true;
newTask.ReminderTime = dueDate;
newTask.Subject = Subject;
newTask.Save();
}

private void ServiceTimer_Tick(object sender,
System.Timers.ElapsedEventArgs e)
{
this.timer.Stop();
createTestTask();
this.timer.Start();
}
}


I get the following errors :
********
Rejected Safe Mode action : Microsoft Office Outlook.

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.
********
AND
********
Service cannot be started. System.Runtime.InteropServices.COMException
(0x80080005): Retrieving the COM class factory for component with CLSID
{0006F03A-0000-0000-C000-000000000046} failed due to the following
error: 80080005.
at AccountRemainder.CheckAccountsService.CreateOutlookTask(String
Body, DateTime dueDate, Importance Importance, String Subject) in
C:\Documents and Settings\psokolowski\My Documents\Visual Studio
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
70
at AccountRemainder.CheckAccountsService.createTestTask() in
C:\Documents and Settings\psokolowski\My Documents\Visual Studio
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
55
at AccountRemainder.CheckAccountsService.OnStart(String[] args) in
C:\Documents and Settings\psokolowski\My Documents\Visual Studio
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
32
at
System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
state)

For more information, see Help and Support Center at
http://go.microsoft.com/fwlink/events.asp.
********

Any idea ?
 
W

Willy Denoyette [MVP]

There are a number of issues here, first of all, Office was not designed
(and is not supported) to be used from non interactive logon sessions, for
more info check this:
http://support.microsoft.com/default.aspx?scid=kb;en-us;257757
Your code fails because of COM security, that is, your service account has
no appropriate rights to start Outlook as COM automation server.

Second, you should not call Sleep in OnStart, the SCM will wait max. 30
seconds for OnStart to return before it considers the service has failed to
start successfully, I see no reason why you need to waste 10 seconds.
Third, you start a timer to call CreateOutlookTask, but at the same time you
call it from OnStart, what's the reasoning behind this?
Fourth, you don't have any error check/recovery in place, any exception
thrown on the thread running OnStart will abort the service, any exception
thrown on the timer thread will go unhandled.

Willy.


| Hello,
|
| I wrote a windows service application however it is not working. The
| service is supposed to enter a new task every 5 sec within Microsoft
| Outlook.
|
| Here is the code for application:
|
| public partial class CheckAccountsService : ServiceBase
| {
| private Timer timer = null;
|
| public CheckAccountsService()
| {
| InitializeComponent();
| }
|
| protected override void OnStart(string[] args)
| {
| EventLog.WriteEntry("It is starting!!!!", "It is
| starting!!");
| System.Threading.Thread.Sleep(10000);
| //Debug.Write("x");
| // TODO: Add code here to start your service
| timer.Enabled = true;
| timer.Start();
| createTestTask();
| }
|
| protected override void OnStop()
| {}
|
| public enum Importance
| {
| High = 0,
| Normal = 1,
| Low = 2
| }
|
| public static void createTestTask()
| {
| CreateOutlookTask("bla bla ", DateTime.Parse("6/6/2006 1:00
| PM"), Importance.High, "bla bla");
| }
|
| public static void CreateOutlookTask(string Body, DateTime
| dueDate, Importance Importance, string Subject)
| {
| // Create an Outlook Application object.
| Application outLookApp = new Application();
| // Create a new TaskItem.
| TaskItem newTask =
| (TaskItem)outLookApp.CreateItem(OlItemType.olTaskItem);
| // Configure the task at hand and save it.
| newTask.Body = Body;
| newTask.DueDate = dueDate;
| switch (Importance)
| {
| case Importance.High:
| newTask.Importance = OlImportance.olImportanceHigh;
| break;
| case Importance.Normal:
| newTask.Importance =
| OlImportance.olImportanceNormal;
| break;
| case Importance.Low:
| newTask.Importance = OlImportance.olImportanceLow;
| break;
| }
| newTask.ReminderSet = true;
| newTask.ReminderTime = dueDate;
| newTask.Subject = Subject;
| newTask.Save();
| }
|
| private void ServiceTimer_Tick(object sender,
| System.Timers.ElapsedEventArgs e)
| {
| this.timer.Stop();
| createTestTask();
| this.timer.Start();
| }
| }
|
|
| I get the following errors :
| ********
| Rejected Safe Mode action : Microsoft Office Outlook.
|
| For more information, see Help and Support Center at
| http://go.microsoft.com/fwlink/events.asp.
| ********
| AND
| ********
| Service cannot be started. System.Runtime.InteropServices.COMException
| (0x80080005): Retrieving the COM class factory for component with CLSID
| {0006F03A-0000-0000-C000-000000000046} failed due to the following
| error: 80080005.
| at AccountRemainder.CheckAccountsService.CreateOutlookTask(String
| Body, DateTime dueDate, Importance Importance, String Subject) in
| C:\Documents and Settings\psokolowski\My Documents\Visual Studio
|
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
| 70
| at AccountRemainder.CheckAccountsService.createTestTask() in
| C:\Documents and Settings\psokolowski\My Documents\Visual Studio
|
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
| 55
| at AccountRemainder.CheckAccountsService.OnStart(String[] args) in
| C:\Documents and Settings\psokolowski\My Documents\Visual Studio
|
2005\Projects\AccountRemainder\WindowsService1\CheckAccountsService.cs:line
| 32
| at
| System.ServiceProcess.ServiceBase.ServiceQueuedMainCallback(Object
| state)
|
| For more information, see Help and Support Center at
| http://go.microsoft.com/fwlink/events.asp.
| ********
|
| Any idea ?
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top