Word.ApplicationClass.ActiveDocument throws an execption

C

Colbert Zhou [MSFT]

Hi Dave,

This is strange. I do a lot of tests but cannot see the issue. In all of my
tests, the ActiveDocument always returns the COM object of the active
document. Even when I close an active document, another document becomes
active and fills the ActiveDocument property. So I guess this may related
to the calling context. In what codes, or event handles, you call the
ActiveDocument and get the error?

And is it possible to share with me a simplified reproduce sample and
detailed steps? After I can reproduce the issue, then I will do more
investigation in my side to see if we can come to a solution.


Best regards,
Ji Zhou
Microsoft Online Community Support
 
D

David Thielen

Got a way to get it:
1) Open Word twice
2) Close document but leave word open with no document in the first
instance
3) Close the second instance of Word

You then will get an event where there is no ActiveDocument. This
makes sense as the one instance has no document in it. The problem is
that for a short period the Documents.Count is 1 but there is no
ActiveDocument.

thanks - dave


Hi Dave,

This is strange. I do a lot of tests but cannot see the issue. In all of my
tests, the ActiveDocument always returns the COM object of the active
document. Even when I close an active document, another document becomes
active and fills the ActiveDocument property. So I guess this may related
to the calling context. In what codes, or event handles, you call the
ActiveDocument and get the error?

And is it possible to share with me a simplified reproduce sample and
detailed steps? After I can reproduce the issue, then I will do more
investigation in my side to see if we can come to a solution.


Best regards,
Ji Zhou
Microsoft Online Community Support


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
C

Colbert Zhou [MSFT]

I still cannot reproduce.

When I close the documents and leave Word without document opened, the
ActiveDocument is null, but the Documents.Count is always 0. So
ActiveDocument and Documents.Count are always consistent in my side.

I notice you mention,
"You then will get an event where there is no ActiveDocument. ".

What event you are exactly talking about? Could you please provide the code
context or a reproduce sample will help in this case.


Best regards,
Ji Zhou
Microsoft Online Community Support in Forum
 
D

David Thielen

Hi;

I've just tried to do this - and can't. Somehow before I had a case
where I had 2 copies of Word running and one had no document. In that
case, when the instance of Word with no document was the active
application the call to ActiveDocument threw an exception.

I think I was checking when updating the ribbon menu. I think the
ribbon update was due to Word but might have been my calling update on
it on the event of closing the document.

This also occurs if you have one instance of Word running and it has
no docoument. But to handle that I check the Documents.Count property
first and if it's 0 then don't check ActiveDocument.

??? - thanks - dave


I still cannot reproduce.

When I close the documents and leave Word without document opened, the
ActiveDocument is null, but the Documents.Count is always 0. So
ActiveDocument and Documents.Count are always consistent in my side.

I notice you mention,
"You then will get an event where there is no ActiveDocument. ".

What event you are exactly talking about? Could you please provide the code
context or a reproduce sample will help in this case.


Best regards,
Ji Zhou
Microsoft Online Community Support in Forum


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
C

Colbert Zhou [MSFT]

Hello Dave,

Sorry for following up late.

Yes, I can see ActiveDocument throws an exception if there is no document
opened in the Word. But I think that should be by designed because there is
exactly no active document opened. So the recommendded solution is before
every access to ActiveDocument, we check the Documents.Count to see if it
is 0.

I do a lot of tests in my side. In all of them, I find the ActiveDocument
and Documents.Count are consistent. That is to say,
1.When ActiveDocument throws exception, Documents.Count is 0
2.If Document.Count is 1 or bigger. I can use ActiveDocument correctly. No
exception is thrown.

So going back the original question, is it possible to provide a simplified
project that shows the problem as you described, "ActiveDocument throws
exception while Documents.Count is 1". If yes, it will help us to isolate
and investigate the issue. :)

Have a nice day!

Best regards,
Ji Zhou
Microsoft Online Community Support
 
D

David Thielen

Hi;

I've tried & tried and cannot re-create the situation. But I swear I
had it - two copies of Word open and the active one had no document..
When I hit it again I'll immediately write down how I made it happen
and post here.

thanks - dave


Hello Dave,

Sorry for following up late.

Yes, I can see ActiveDocument throws an exception if there is no document
opened in the Word. But I think that should be by designed because there is
exactly no active document opened. So the recommendded solution is before
every access to ActiveDocument, we check the Documents.Count to see if it
is 0.

I do a lot of tests in my side. In all of them, I find the ActiveDocument
and Documents.Count are consistent. That is to say,
1.When ActiveDocument throws exception, Documents.Count is 0
2.If Document.Count is 1 or bigger. I can use ActiveDocument correctly. No
exception is thrown.

So going back the original question, is it possible to provide a simplified
project that shows the problem as you described, "ActiveDocument throws
exception while Documents.Count is 1". If yes, it will help us to isolate
and investigate the issue. :)

Have a nice day!

Best regards,
Ji Zhou
Microsoft Online Community Support


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
C

Colbert Zhou [MSFT]

Sure. After reproducing it, just post it here and I will follow up and try
my best to provide further help on this.

Have a nice day!


Best regards,
Ji Zhou - MSFT
Microsoft
 
D

David Thielen

Hi;

I got it again, but only because of a bug in my code. Under normal
conditions it cannot happen. I've wrapped it in a try/catch and I
think that is good enough as this is so rare.

thanks - dave


Sure. After reproducing it, just post it here and I will follow up and try
my best to provide further help on this.

Have a nice day!


Best regards,
Ji Zhou - MSFT
Microsoft


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
C

Colbert Zhou [MSFT]

OK. Glad to hear it works as expected now. If you need more support on
this, just let me know. :) Have a nice day!


Best regards,
Ji Zhou - MSFT
Microsoft Online Community Support
 

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