473,237 Members | 1,086 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,237 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 2321
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: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.