Any solution yet for ... menu objects losing their connection

D

David Thielen

Hi;

I have an Add-In in C#, not VSTO.

Is there a known work-around yet for the problem where suddenly a menu
object is no longer attached to the underlying COM menu object?

Note: This is not the issue where the .NET menu object must exist - I have
them declared as class fields in my IDTExtensibility2 object.

Note 2: This usually (not always) happens when opening an additional
document or closing a document/closing Word.

I put a try/catch around my access of the objects and re-attach them in the
catch. But I think having an exception trigger when code is running correctly
is very bad so I'd like a better work-around.
 
P

Peter Huang [MSFT]

Hi

So far for the problem similar with your scenario, we use the try..catch to
detected the error and then handle then in the catch block to retie the
event.
Here is the similar issue with you.
http://groups-beta.google.com/group/microsoft.public.office.developer.com.ad
d_ins/browse_thread/thread/31cf6a06b4f8b2ff/92c70fb930b49c2c?q=%22v-phuang%2
2+try+catch+group:microsoft.public.office.developer.com.add_ins&rnum=2&hl=en
#92c70fb930b49c2c

If I have any misunderstanding, please feel free to let me know.

Best 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.
 
D

David Thielen

Hi;

Cindy's comment in the link you gave me says in COM they check if it is
null. Is there something equivilent we can do in .NET (the object is not null
- but is there a call in the object to check the COM object)?

I just dislike exceptions, both because of the processing time and because
to me they connotate sloppy programming.
 
A

Alex

Hello David,

David Thielen said:
Cindy's comment in the link you gave me says in COM they check if it is
null. Is there something equivilent we can do in .NET (the object is not null
- but is there a call in the object to check the COM object)?

I just dislike exceptions, both because of the processing time and because
to me they connotate sloppy programming.

As the person who started the thread peter pointed you to, I can tell you that I did not yet find a working solution that didn't involve try/catch.

A Word object may be deleted while I still hold handles to it in the add-in.
Unfortunately, there is no indication that

My current code looks like this:

try
{
DoStuff();
}

catch (Exception ex)
{
// "Object has been deleted" COM exception is processed silently
if (ex is COMException && ex.ErrorCode == unchecked((int) 0x800A16C1))
DoCleanupOfComObjects();
else
DefaultExceptionProcessing(ex);
}

(Or, you could catch COMException separately, but it does not change the solution).

Best wishes,
Alex.

P.S.,

David, from what I gather on Usenet, you seem to have been battling for some time with the same ones I struggle with.
Will it be possible for you to keep sharing your solutions (over and beyond what you have on your web site)?
 
D

David Thielen

I try to post everything I've learned that is useful to either my website or
blog. If I missed anything let me know but I want to put everything there so
anyone else doing a google search will find it.

I do the same try/catch stuff. But it makes me feel yucky.
 
A

Alex

There's a mistake in my code, corrected version included below.

Hello David,
David Thielen said:
I try to post everything I've learned that is useful to either my website or
blog. If I missed anything let me know but I want to put everything there so
anyone else doing a google search will find it.

There is a shortage of useful info on the subject on the Web so I usually search Google's Usenet archive instead.
I encountered several questions by you which did not seem to be fully answered.
Assuming you do (did?) find satisfactory solutions to those problems, a short post along the lines of: "I solved the problem (or found a workaround), see my blog at ... for a snippet" will be greatly appreciated.
I do the same try/catch stuff. But it makes me feel yucky.

I know how you feel but I could not find any way to test whether a reference points to a COM object that was not deleted.
Perhaps the Marshal class or some similar thing exposes a method to do that but I am pretty sure it will have a non trivial performance cost if called in a tight loop.


As promised, the fixed code (for future Googling):

try
{
DoMyStuffWithComObjects();
}

catch (COMException ex)
{
// "Object has been deleted" COM exception is processed silently
if (ex.ErrorCode == unchecked((int) 0x800A16C1))
DoMyCleanupOfComObjects();
else
DoMtDefaultExceptionProcessing(ex);
}

catch (Exception ex)
{
DoMyDefaultExceptionProcessing(ex);
}


Best wishes,
Alex.
 
D

David Thielen

One way to get this to occur 100% of the time, close the file so word has no
document - then try to call "CommandBarControl.Enable = false;" - it will
always throw an exception.
 
P

Peter Huang [MSFT]

Hi David,

The problem seems similar with the problem in my last post.
So far I think we need to use the try...catch to workaround the problem.
If you still have any concern, please feel free to post here.

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

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