473,386 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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 2325
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: MrFez | last post by:
I have a simple .Net console program that sends an email message. The message can be specified as a string or a filename on the command line. Refer to the code below. The problem I am having is...
7
by: Mike | last post by:
I want to create a windows service that will monitor another window service. what i need for the service to do is, if a service is stopped I need it to start the service back up example: ...
6
by: Leonardo Curros | last post by:
Hello, I would like to know what's the best way to restart one service. I would like to do it from the service itself. Is this possible? I try it with ServiceController.stop()...
2
by: Val3 | last post by:
Hi all. I need to build dll(s) and windows services using VB .NET 2005 Express. When I make File/New project the windows contain only Windows application, Windows control library, Console...
6
by: Picho | last post by:
Hi all. I have a webservice and a windows app. both of them reference the same class library called WebServiceTest.Core that defines a class called Class1. the webservice exposes a method...
4
by: Johny_W | last post by:
hi guys, I have encountered a problem with IE automation (.NET 2.0, C#, mshtml, SHDocVw). I am writing a class for manipulating specific html pages with frames. It is supposed to be used from...
8
by: Blaine Manyluk | last post by:
Greetings... I have some strange problems involving: Access 2000 (9.0.4402 SR-1) Windows 2000 Server 5.00.2195 Service Pack 4 Access apps with Oracle/ODBC back ends One app is a reporting...
7
by: Giulio Petrucci | last post by:
Hi everybody, I've a big problem with a Windows Service I've created using C#. It's a very simple service: it just starts a TcpListener listening for incoming connections on a certain ports....
7
by: Diego F. | last post by:
Hello. I have a windows service running that listens to a port and makes insert queries in a database. I need to make an interface, so my idea is creating a simple windows application that just...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.