Problem: Windows Service creating Outllook object

W

W Akthar

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 !!!!!!!!
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
W

W Akthar

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.olAppointment
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.olBusy ; //
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();
}
}
 
W

Willy Denoyette [MVP]

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.
 

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