Outlook.Application and Outlook.ApplicationClass

S

Sunny

Hi,
I'm writing COM addin for Outlook 2000 in C#/VS.Net 2003
Now I know how hard is this, but as far as most of the work is already done
.... I have to finish it.
So, just now I found a new problem:
I can not cast Outlook.Application to Outlook.ApplicationClass using:
appClass = (Outlook.ApplicationClass)applicationObject;

The same thing works with Outlook.Explorer/ExplorerClass and
Inspector/InspectorClass.

The problem is, that some events (like Quit) exists only in
ApplicationClass, and not in Application.

Any workaround?

I tried to look in the .IL which is generated by ildasm, and can not see any
different between the Quit event and StartUp event, but the second one is in
..Application, and .Quit - not ?!?!?



Thanks in advance

Sunny

P.S. I need the Quit event as a last try to remove buttons I've created. If
I create buttons as temporary on a Explorer, they don't exist if user opens
a new explorer. And I can not create them in the NewExplorer event, because
there the Explorer is not fully created, and referencing to its commandbars
fails.

I have trapped the Activate event of the newly created explorer, but ... :)
strange, the temporary created buttons (created during the addin start) are
there in that moment, so there is no need to recreate them, and ... they
disappear just after I exit the event handler.

It seems that there is no other way, than to create the buttons as
permanent, and delete them on exit. So I have to keep a reference to every
explorer opened in order to catch it's Close event, and if I'm trying to
close the last one, to delete buttons. I don't want to do this (a lot of
coding, where something can go wrong, trying to guess which explorer is
closing, to release the reference, etc.), that's why I just decided to try
to capture the application.Quit event ... and there is the above problem.

If there are some ideas around :) (except that this is very exotic to create
such a thing in .net :)) please, push me in the right direction.



Thanks again

SUnny
 
P

Peter Huang [MSFT]

Hi Sunny,

I have tried to handle the close evetn of Outlook.Explorer. You may have a
try a let me know if it does the job for you.
Outlook.Explorer el = fd.GetExplorer((object)0);
el.Display();
((Outlook.ExplorerEvents_10_Event)el).Close+=new
Microsoft.Office.Interop.Outlook.ExplorerEvents_10_CloseEventHandler(el_Expl
orerEvents_10_Event_Close);


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
 
P

Peter Huang [MSFT]

Hi Sunny,

I think you can catch the quit event. Here is my test code.

1) You may need to install this patch first
http://www.microsoft.com/downloads/details.aspx?FamilyId=C41BD61E-3060-4F71-
A6B4-01FEBA508E52&displaylang=en

2) Here is my test code.
static void Main()
{
Application.Run(new Form1());
}
void hello()
{
MessageBox.Show("GoodBye");
}
Microsoft.Office.Interop.Outlook.Application olapp;
private void button1_Click(object sender, System.EventArgs e)
{
olapp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
((Microsoft.Office.Interop.Outlook.ApplicationEvents_10_Event)olapp).Quit
+= new
Microsoft.Office.Interop.Outlook.ApplicationEvents_10_QuitEventHandler(hello
); // you can handle the quit event of application in this method
}

private void button2_Click(object sender, System.EventArgs e)
{
olapp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(olapp); //Please
note the line.
olapp = null;
}

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
X-Tomcat-ID: 287003515
References: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
From: (e-mail address removed) (Peter Huang [MSFT])
Organization: Microsoft
Date: Thu, 28 Aug 2003 12:21:34 GMT
Subject: Re: Outlook.Application and Outlook.ApplicationClass
X-Tomcat-NG: microsoft.public.office.developer.com.add_ins
Message-ID: <[email protected]>
Newsgroups: microsoft.public.office.developer.com.add_ins
Lines: 164
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.office.developer.com.add_ins:4443
NNTP-Posting-Host: TOMCATIMPORT2 10.201.218.182

Hi Sunny,

I am researching the issue. I will update you with new information ASAP.


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
Subject: Re: Outlook.Application and Outlook.ApplicationClass
Date: Wed, 27 Aug 2003 09:14:55 -0500
Lines: 139
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.office.developer.com.add_ins
NNTP-Posting-Host: 216.17.90.91
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.office.developer.com.add_ins:4427
X-Tomcat-NG: microsoft.public.office.developer.com.add_ins

Hi Peter,
Thanks for the reply.
I have tried such a thing before I post. I'm capturing the NewExplorer
event, and in there I set the Close event handler. But ...:
1. In the close event handler there is no way to catch which explorer is
closing :)
2. I tried to inspect Application.Explorers.Count, but it does not decrease,
even I have closed already some explorers. Maybe this is because they are
not closed completely, or ... I don't know. Maybe I have to release the
event handler for the closing explorer, in order for it to close completely,
but there is no way to capture which one is closing.
3. When the last explorer closes, it does not fire Close event :(

Because of all this I tried to capture the Application.Quit event, but so
far no success.

I do not need to create an explorer. But if user clicks some more times on
Outlook icon, there is no new instance of Outlook, but a new explorers are
created. And I want my buttons there. If I have created them as Temporary
when Outlook starts, they do not appear on newly opened explorers(look at my
orig. post what happens). If I create them as permanent, they are there, but
now I have to find a way to delete them when Outlook shuts down. I can leave
them, but then if the user uninstalls the addin (it is installed with setup
project) after that he/she have to remove my custom commandbars and buttons
manually, which I think is not a good practice. That's why I want to remove
them on exit.
As I read in docs, GetExplorer creates a new explorer (or I'm missing
something) ?

Thanks again
Sunny
have
p
l created.
If ...
:)
 
P

Peter Huang [MSFT]

Hi Sunny,

I am gald that the your project works. :)
Cheers.

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
<[email protected]>
<#[email protected]>
Subject: Re: Outlook.Application and Outlook.ApplicationClass
Date: Fri, 29 Aug 2003 17:04:09 -0500
Lines: 335
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.office.developer.com.add_ins
NNTP-Posting-Host: 216.17.90.91
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!tk2msftngp13.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.office.developer.com.add_ins:4455
X-Tomcat-NG: microsoft.public.office.developer.com.add_ins

Hi Peter,
Thanks for the test code and for the help at all.
Now I have succeeded to solve the problem. There was no way for me to guess
how to cast the event, my missing knowledge was the line:

((Microsoft.Office.Interop.Outlook.ApplicationEvents_10_Event)olapp).Quit +=
new
Microsoft.Office.Interop.Outlook.ApplicationEvents_10_QuitEventHandler(hell
o
);

Even I target Outlook 2000, and I do not use the PIAs, provided by MS, when
I made a cast:

((Outlook.ApplicationEvents_Event)applicationObject).Quit += new
ApplicationEvents_QuitEventHandler(hello);


everything was OK.
Thanks again for the help.
Sunny

P.S. this example should be included somewhere in documents, it took me a
lot of time to find a solution.
Actually it seems that this is the only good way for all of us (trying
exotic coding with .NET against Outlook :) ) where one can release all event
handlers and Marshaling in order to prevent Outlook to stay in memory (well
known problem).

P.S.2: Hmmm, another question: I have noticed that Intellisense of VS.Net
shows 2 overloaded method for creating most of event handlers for Outlook
objects. As an example for the above Quit handler it shows:
ApplicationEvents_QuitEventHandler(void() target)
and
ApplicationEvents_QuitEventHandler(object param, SystemUIntPtr param).
Do you have any idea what is this about? Is there a possibility to pass to
event handler the object, which rises the event, or any other data?
I'm asking, because I deal with a lot of AppointmentItem objects, for which
I have to capture the Open event, and the actions that have to be performed
differs slightly only depending on one of the properties of the Item. So,
it'll be nice if I have only one handler and when I hook the Items to it, to
pass the necessary info, instead to try to get ActiveInspector.Item....

P.S.3: I have found that in Open event handler of an Item (Item_Open(ref
bool Cancel)) even you set a Cancel to true, it still opens the Item. The
only workaround I found so far was to find which Item is it, and to call
it's Close method. But this solution brings me back to the previous question
:)

I do not know if this is the right way to post a new questions, so if I have
to, I'll start new threads with these last questions.

And ... OK, I'll stop for now :)

Thanks again and good luck
Sunny


Peter Huang said:
Hi Sunny,

I think you can catch the quit event. Here is my test code.

1) You may need to install this patch first
http://www.microsoft.com/downloads/details.aspx?FamilyId=C41BD61E-3060-4F71
-
A6B4-01FEBA508E52&displaylang=en

2) Here is my test code.
static void Main()
{
Application.Run(new Form1());
}
void hello()
{
MessageBox.Show("GoodBye");
}
Microsoft.Office.Interop.Outlook.Application olapp;
private void button1_Click(object sender, System.EventArgs e)
{
olapp = new Microsoft.Office.Interop.Outlook.ApplicationClass();
((Microsoft.Office.Interop.Outlook.ApplicationEvents_10_Event)olapp).Quit
+= new
Microsoft.Office.Interop.Outlook.ApplicationEvents_10_QuitEventHandler(hell
o
); // you can handle the quit event of application in this method
}

private void button2_Click(object sender, System.EventArgs e)
{
olapp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(olapp); //Please
note the line.
olapp = null;
}

Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.

--------------------
X-Tomcat-ID: 287003515
References: <[email protected]>
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: 7bit
From: (e-mail address removed) (Peter Huang [MSFT])
Organization: Microsoft
Date: Thu, 28 Aug 2003 12:21:34 GMT
Subject: Re: Outlook.Application and Outlook.ApplicationClass
X-Tomcat-NG: microsoft.public.office.developer.com.add_ins
Message-ID: <[email protected]>
Newsgroups: microsoft.public.office.developer.com.add_ins
Lines: 164
Path: cpmsftngxa06.phx.gbl
Xref: cpmsftngxa06.phx.gbl microsoft.public.office.developer.com.add_ins:4443
NNTP-Posting-Host: TOMCATIMPORT2 10.201.218.182

Hi Sunny,

I am researching the issue. I will update you with new information ASAP.


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
--------------------
From: "Sunny" <[email protected]>
References: <[email protected]>
<[email protected]>
Subject: Re: Outlook.Application and Outlook.ApplicationClass
Date: Wed, 27 Aug 2003 09:14:55 -0500
Lines: 139
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <#[email protected]>
Newsgroups: microsoft.public.office.developer.com.add_ins
NNTP-Posting-Host: 216.17.90.91
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
Xref: cpmsftngxa06.phx.gbl
microsoft.public.office.developer.com.add_ins:4427
X-Tomcat-NG: microsoft.public.office.developer.com.add_ins

Hi Peter,
Thanks for the reply.
I have tried such a thing before I post. I'm capturing the NewExplorer
event, and in there I set the Close event handler. But ...:
1. In the close event handler there is no way to catch which explorer is
closing :)
2. I tried to inspect Application.Explorers.Count, but it does not
decrease,
even I have closed already some explorers. Maybe this is because they are
not closed completely, or ... I don't know. Maybe I have to release the
event handler for the closing explorer, in order for it to close
completely,
but there is no way to capture which one is closing.
3. When the last explorer closes, it does not fire Close event :(

Because of all this I tried to capture the Application.Quit event, but so
far no success.

I do not need to create an explorer. But if user clicks some more times on
Outlook icon, there is no new instance of Outlook, but a new explorers are
created. And I want my buttons there. If I have created them as Temporary
when Outlook starts, they do not appear on newly opened explorers(look at
my
orig. post what happens). If I create them as permanent, they are there,
but
now I have to find a way to delete them when Outlook shuts down. I can
leave
them, but then if the user uninstalls the addin (it is installed with setup
project) after that he/she have to remove my custom commandbars and buttons
manually, which I think is not a good practice. That's why I want to remove
them on exit.
As I read in docs, GetExplorer creates a new explorer (or I'm missing
something) ?

Thanks again
Sunny

Hi Sunny,

I have tried to handle the close evetn of Outlook.Explorer. You may have
a
try a let me know if it does the job for you.
Outlook.Explorer el = fd.GetExplorer((object)0);
el.Display();
((Outlook.ExplorerEvents_10_Event)el).Close+=new
Microsoft.Office.Interop.Outlook.ExplorerEvents_10_CloseEventHandler(el_E
x
p
l
orerEvents_10_Event_Close);


Regards,
Peter Huang
Microsoft Online Partner Support
Get Secure! www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no
rights.
--------------------
From: "Sunny" <[email protected]>
Subject: Outlook.Application and Outlook.ApplicationClass
Date: Tue, 26 Aug 2003 14:52:39 -0500
Lines: 55
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
Message-ID: <[email protected]>
Newsgroups: microsoft.public.office.developer.com.add_ins
NNTP-Posting-Host: 216.17.90.91
Path: cpmsftngxa06.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
Xref: cpmsftngxa06.phx.gbl
microsoft.public.office.developer.com.add_ins:4421
X-Tomcat-NG: microsoft.public.office.developer.com.add_ins

Hi,
I'm writing COM addin for Outlook 2000 in C#/VS.Net 2003
Now I know how hard is this, but as far as most of the work is already
done
... I have to finish it.
So, just now I found a new problem:
I can not cast Outlook.Application to Outlook.ApplicationClass using:
appClass = (Outlook.ApplicationClass)applicationObject;

The same thing works with Outlook.Explorer/ExplorerClass and
Inspector/InspectorClass.

The problem is, that some events (like Quit) exists only in
ApplicationClass, and not in Application.

Any workaround?

I tried to look in the .IL which is generated by ildasm, and can not see
any
different between the Quit event and StartUp event, but the second one
is
in
.Application, and .Quit - not ?!?!?



Thanks in advance

Sunny

P.S. I need the Quit event as a last try to remove buttons I've created.
If
I create buttons as temporary on a Explorer, they don't exist if user
opens
a new explorer. And I can not create them in the NewExplorer event,
because
there the Explorer is not fully created, and referencing to its
commandbars
fails.

I have trapped the Activate event of the newly created explorer, but ..
:)
strange, the temporary created buttons (created during the addin start)
are
there in that moment, so there is no need to recreate them, and ... they
disappear just after I exit the event handler.

It seems that there is no other way, than to create the buttons as
permanent, and delete them on exit. So I have to keep a reference to
every
explorer opened in order to catch it's Close event, and if I'm
trying
to
close the last one, to delete buttons. I don't want to do this (a
lot
of
coding, where something can go wrong, trying to guess which explorer is
closing, to release the reference, etc.), that's why I just decided to
try
to capture the application.Quit event ... and there is the above
problem.

If there are some ideas around :) (except that this is very exotic to
create
such a thing in .net :)) please, push me in the right direction.



Thanks again

SUnny
 

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