How to detect that an object was deleted?

A

Alex

Hello,

I have a Word add-in written in C#.

In it, I have a reference to a Word bookmark:

Microsoft.Office.Interop.Word.Bookmark bm = /* ... */;

However, the user may delete the bookmark at any moment and if that happens, an attempt to use the reference generates an "object has been deleted" COM exception.

Is it possible to pre-emptively test the variable to make sure that it points to a valid Word object without throwing an exception?
 
P

Peter Huang [MSFT]

Hi

I think we can not detect on the variable directly.
1. We can use Bookmarks.Exists Method to test if certain bookmark is
deleted.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbawd11/htm
l/womthExists1_HV05210716.asp

2. Use a try...catch block to test the variable.
try
{
string str =bookmark1.name;
}
catch(Exception ex)
{
;
}

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

Alex

Hello Peter,

Thank you for the suggestions.

"Peter Huang" said:
I think we can not detect on the variable directly.
(rearranged)

2. Use a try...catch block to test the variable.
try
{
string str =bookmark1.name;
}
catch(Exception ex)
{
;
}

I prefer to avoid the excepteion overhead.
1. We can use Bookmarks.Exists Method to test if certain bookmark is
deleted.
http://msdn.microsoft.com/library/d...n-us/vbawd11/html/womthExists1_HV05210716.asp

Since I have a reference to the object, not a name, getting the name can potentially trigger the exception,
which brings me back to option #2 above.

Isn't there any way to detect that the COM object pointed to is no longer there?
 
P

Peter Huang [MSFT]

Hi

Even if we are using VBA, i.e. eliminate the using of RCW layer that .NET
use to communicate with COM, we will find that the COM object (bookmark) is
still alive, while we can not access to its property. So I think this is
the intrinsic behavior of the bookmark object in word.

So I think use a try catch block when we try to access to a deleted object
will throw a exception, and we catch the exception to know that the
bookmark is unavailable.

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

Cindy M -WordMVP-

Hi Alex,
Since I have a reference to the object, not a name, getting the name can potentially trigger the exception,
which brings me back to option #2 above.

Isn't there any way to detect that the COM object pointed to is no longer there?
In COM, we check if it Is Nothing (=Null in C#, I believe)

An alternative to try...catch would be to loop the bookmarks collection and check if each member = your
object. That might throw an error, but I don't think it should.

And lastly, you could save the current bookmark object's name when you assign the boject to the variable.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 8 2004)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by
e-mail :)
 

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