470,628 Members | 2,316 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,628 developers. It's quick & easy.

Problem: Windows Service creating Outllook object

Hi

I am trying to create a windows service which queries SQL
Server on timed intervals and depending on the results
send appointments to Outlook.

The problem lies when I try to create an outlook
application object.
StreamWriter sWriter6 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter6.WriteLine(" ** Doing Outlook Stuff **");
sWriter6.Close();

// Add it OUTLOOK
Microsoft.Office.Interop.Outlook.Application oApp = new
Microsoft.Office.Interop.Outlook.Application();

StreamWriter sWriter10 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter10.WriteLine(" ** Getting Namespace **");
sWriter10.Close();

Microsoft.Office.Interop.Outlook.NameSpace oNS =
oApp.GetNamespace("mapi");

StreamWriter sWriter11 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter11.WriteLine(" ** Setting Profile **");
sWriter11.Close();
I have a try catch block around this but it never goes
into the catch block.

The log file has the following

** DO DATABASE **
** Connecting **
** Connected OK **
** Closing Connection **
** Closed OK **
** Count = 5 **
** Doing Outlook Stuff **

** DO DATABASE **
** Connecting **
** Connected OK **
** Closing Connection **
** Closed OK **
** Count = 5 **
** Doing Outlook Stuff **

** DO DATABASE **
** Connecting **
** Connected OK **
** Closing Connection **
** Closed OK **
** Count = 5 **
** Doing Outlook Stuff **
Does anyone have any ideaq why this is happening ?????
PLEASE HELP !!!!!!!!


Nov 16 '05 #1
3 2162
W Akthar,

What do you have in your try/catch block? It's hard to tell without
seeing that, but it would seem that you are not writing to the log in it.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"W Akthar" <an*******@discussions.microsoft.com> wrote in message
news:12****************************@phx.gbl...
Hi

I am trying to create a windows service which queries SQL
Server on timed intervals and depending on the results
send appointments to Outlook.

The problem lies when I try to create an outlook
application object.
StreamWriter sWriter6 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter6.WriteLine(" ** Doing Outlook Stuff **");
sWriter6.Close();

// Add it OUTLOOK
Microsoft.Office.Interop.Outlook.Application oApp = new
Microsoft.Office.Interop.Outlook.Application();

StreamWriter sWriter10 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter10.WriteLine(" ** Getting Namespace **");
sWriter10.Close();

Microsoft.Office.Interop.Outlook.NameSpace oNS =
oApp.GetNamespace("mapi");

StreamWriter sWriter11 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter11.WriteLine(" ** Setting Profile **");
sWriter11.Close();
I have a try catch block around this but it never goes
into the catch block.

The log file has the following

** DO DATABASE **
** Connecting **
** Connected OK **
** Closing Connection **
** Closed OK **
** Count = 5 **
** Doing Outlook Stuff **

** DO DATABASE **
** Connecting **
** Connected OK **
** Closing Connection **
** Closed OK **
** Count = 5 **
** Doing Outlook Stuff **

** DO DATABASE **
** Connecting **
** Connected OK **
** Closing Connection **
** Closed OK **
** Count = 5 **
** Doing Outlook Stuff **
Does anyone have any ideaq why this is happening ?????
PLEASE HELP !!!!!!!!

Nov 16 '05 #2
static void DoDataBase(Object state)
{

StreamWriter sWriterss = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriterss.WriteLine(" ** DO DATABASE **");
sWriterss.Close();

SqlConnection _sqlConn;
string connString = "data source=L004
\\L004;initial catalog=NamesAndAddresses;persist security
info=True;user id=sa;password=;workstation id=L004
\\L004;packet size=4096;Max Pool Size=500";
try
{

StreamWriter sWriter1 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter1.WriteLine(" ** Connecting
**");
sWriter1.Close();

_sqlConn = new SqlConnection(connString);
_sqlConn.Open();

StreamWriter sWriter2 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter2.WriteLine(" ** Connected OK
**");
sWriter2.Close();

DataSet ds = new DataSet("Users");

// Create a SqlDataAdapter.
SqlDataAdapter myAdapter = new
SqlDataAdapter();

string sqlString = "SELECT [ID],
[Profile], [Password], [Subject], [Content], [Location],
[StartTime], [EndTime], [SET] FROM
[Outlook_Visit_Polling] WHERE [SET] = 0"; //FROM Names
WHERE ";
SqlCommand sqlNamesCmd = new SqlCommand
(sqlString, _sqlConn);

sqlNamesCmd.CommandType =
CommandType.Text;
myAdapter.SelectCommand = sqlNamesCmd;
myAdapter.Fill(ds);

StreamWriter sWriter3 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter3.WriteLine(" ** Closing
Connection **");
sWriter3.Close();

_sqlConn.Close();

StreamWriter sWriter4 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter4.WriteLine(" ** Closed OK
**");
sWriter4.Close();

DataTable dsTable = ds.Tables[0];
DataRow dsRow;
for(int row=0; row < dsTable.Rows.Count;
row++)
{

StreamWriter sWriter5 = new
StreamWriter(@"C:\CMSOutlook.log", true);
sWriter5.WriteLine(" ** Count
= " + dsTable.Rows.Count.ToString() + " **");
sWriter5.Close();

dsRow = dsTable.Rows[row];

string profile = "";
string password = "";
string subject = "";
string content = "";
string location = "";
int id = 0;
DateTime startTime = new DateTime
();
DateTime endTime = new DateTime();

if (!Convert.IsDBNull(dsRow
["ID"]))
id = Int32.Parse(dsRow
["ID"].ToString());

if (!Convert.IsDBNull(dsRow
["Profile"]))
profile = dsRow
["Profile"].ToString().TrimEnd();

if (!Convert.IsDBNull(dsRow
["Password"]))
password = dsRow
["Password"].ToString().TrimEnd();

if (!Convert.IsDBNull(dsRow
["Subject"]))
subject = dsRow
["Subject"].ToString().TrimEnd();

if (!Convert.IsDBNull(dsRow
["Content"]))
content = dsRow
["Content"].ToString().TrimEnd();

if (!Convert.IsDBNull(dsRow
["Location"]))
location = dsRow
["Location"].ToString().TrimEnd();

if (!Convert.IsDBNull(dsRow
["StartTime"]))
startTime =
Convert.ToDateTime(dsRow["StartTime"].ToString().TrimEnd
());

if (!Convert.IsDBNull(dsRow
["EndTime"]))
endTime =
Convert.ToDateTime(dsRow["EndTime"].ToString().TrimEnd());

StreamWriter sWriter6 = new
StreamWriter(@"C:\CMSOutlook.log", true);
sWriter6.WriteLine(" ** Doing
Outlook Stuff **");
sWriter6.Close();

// Add it OUTLOOK

Microsoft.Office.Interop.Outlook.Application oApp
= new Microsoft.Office.Interop.Outlook.Application();

StreamWriter sWriter10 = new StreamWriter
(@"C:\CMSOutlook.log", true);

sWriter10.WriteLine(" ** Getting Namespace
**");
sWriter10.Close();
Microsoft.Office.Interop.Outlook.NameSpace oNS =
oApp.GetNamespace("mapi");

StreamWriter sWriter11 = new StreamWriter
(@"C:\CMSOutlook.log", true);

sWriter11.WriteLine(" ** Setting Profile **");
sWriter11.Close();

if (password == "")
oNS.Logon(profile, Missing.Value, false,
true);
else
oNS.Logon(profile, password, false, true);

StreamWriter sWriter12 = new StreamWriter
(@"C:\CMSOutlook.log", true);

sWriter12.WriteLine(" ** Getting Appointment
**");
sWriter12.Close();

Microsoft.Office.Interop.Outlook.AppointmentItem
oAppt = (Microsoft.Office.Interop.Outlook.AppointmentItem)
oApp.CreateItem
(Microsoft.Office.Interop.Outlook.OlItemType.olApp ointment
Item);
oAppt.Subject = subject;
oAppt.Body = content;
oAppt.Location = location;
oAppt.Start = startTime;
oAppt.End = endTime;

oAppt.ReminderSet = true;

oAppt.ReminderMinutesBeforeStart = 5;
oAppt.BusyStatus =
Microsoft.Office.Interop.Outlook.OlBusyStatus.olBu sy ; //
olBusy

oAppt.IsOnlineMeeting = false;

StreamWriter sWriter13 = new StreamWriter
(@"C:\CMSOutlook.log", true);

sWriter13.WriteLine(" ** Saving Appointment
**");
sWriter13.Close();

oAppt.Save();

StreamWriter sWriter14 = new StreamWriter
(@"C:\CMSOutlook.log", true);

sWriter14.WriteLine(" ** Logging off **");
sWriter14.Close();

oNS.Logoff();

oApp = null;
oNS = null;
oAppt = null;

StreamWriter sWriter7 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter7.WriteLine(" ** Finished Outlook
Stuff **");
sWriter7.Close();
StreamWriter sWriter8 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter8.WriteLine(" ** Update Database **");
sWriter8.Close();

// everything ok so update Database
UpdateDatabase(id);

StreamWriter sWriter9 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter9.WriteLine(" ** Finished Update
Database **");
sWriter9.Close();
// Add it to a log file
StreamWriter sWriter = new StreamWriter
(@"C:\CMSOutlook.log", true);

sWriter.WriteLine("Profile : " +
profile);
sWriter.WriteLine("Subject : " +
subject);
sWriter.WriteLine("Content : " +
content);
sWriter.WriteLine("Location : " +
location);
sWriter.WriteLine("StartTime : " +
startTime.ToString());
sWriter.WriteLine("EndTime : " +
endTime.ToString());

sWriter.Close();
}
}
catch(Exception e)
{
// Add it to a log file
StreamWriter sWriter = new StreamWriter
(@"C:\CMSOutlook.log", true);

sWriter.WriteLine("----------------------------");
sWriter.WriteLine("----------------------------");
sWriter.WriteLine(e.Message.ToString());
sWriter.WriteLine("----------------------------");
sWriter.WriteLine("----------------------------");

sWriter.Close();
}
}

static void UpdateDatabase(int id)
{

try
{
SqlConnection sqlConn;
string connString = "data source=L004\\L004;initial
catalog=NamesAndAddresses;persist security info=True;user
id=sa;password=;workstation id=L004\\L004;packet
size=4096;Max Pool Size=500";

sqlConn = new SqlConnection(connString);
sqlConn.Open();

DataSet ds = new DataSet("Users");

// Create a SqlDataAdapter.
SqlDataAdapter myAdapter = new SqlDataAdapter();

string sqlString = "UPDATE [Outlook_Visit_Polling] SET
[SET] = 1 WHERE [ID] = @ID"; //FROM Names WHERE ";

SqlCommand cmCommand = new SqlCommand(sqlString, sqlConn);
cmCommand.CommandType = CommandType.Text;
cmCommand.Parameters.Add(new SqlParameter("@ID", id));
cmCommand.ExecuteNonQuery();

sqlConn.Close();
}
catch(Exception e)
{
// Add it to a log file
StreamWriter sWriter = new StreamWriter
(@"C:\CMSOutlook.log", true);

sWriter.WriteLine("----------------------------");
sWriter.WriteLine("----------------------------");
sWriter.WriteLine(e.Message.ToString());
sWriter.WriteLine("----------------------------");
sWriter.WriteLine("----------------------------");

sWriter.Close();
}
}
Nov 16 '05 #3
The problem is that "Outlook" (or any other Office application) is not
built to be automated from "Windows Services".
Don't know which identity is used to run your service, but try with
"LocalSystem" and set "Interact with Desktop" and watch the nice dialog box
wich remains invisible when you run under another identity.

Willy.

"W Akthar" <an*******@discussions.microsoft.com> wrote in message
news:12****************************@phx.gbl...
Hi

I am trying to create a windows service which queries SQL
Server on timed intervals and depending on the results
send appointments to Outlook.

The problem lies when I try to create an outlook
application object.
StreamWriter sWriter6 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter6.WriteLine(" ** Doing Outlook Stuff **");
sWriter6.Close();

// Add it OUTLOOK
Microsoft.Office.Interop.Outlook.Application oApp = new
Microsoft.Office.Interop.Outlook.Application();

StreamWriter sWriter10 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter10.WriteLine(" ** Getting Namespace **");
sWriter10.Close();

Microsoft.Office.Interop.Outlook.NameSpace oNS =
oApp.GetNamespace("mapi");

StreamWriter sWriter11 = new StreamWriter
(@"C:\CMSOutlook.log", true);
sWriter11.WriteLine(" ** Setting Profile **");
sWriter11.Close();
I have a try catch block around this but it never goes
into the catch block.

The log file has the following

** DO DATABASE **
** Connecting **
** Connected OK **
** Closing Connection **
** Closed OK **
** Count = 5 **
** Doing Outlook Stuff **

** DO DATABASE **
** Connecting **
** Connected OK **
** Closing Connection **
** Closed OK **
** Count = 5 **
** Doing Outlook Stuff **

** DO DATABASE **
** Connecting **
** Connected OK **
** Closing Connection **
** Closed OK **
** Count = 5 **
** Doing Outlook Stuff **
Does anyone have any ideaq why this is happening ?????
PLEASE HELP !!!!!!!!

Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Mike | last post: by
6 posts views Thread by Leonardo Curros | last post: by
8 posts views Thread by Blaine Manyluk | last post: by
7 posts views Thread by Giulio Petrucci | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.