Automating Office using C++: Closing a Word Document using Word::

G

Gustyn

OK, I am trying to write an event handler that will close a new document in
word when it is called by MsWord. The problem is that when I try to call
newDoc->Close(), I get the following error:

..\MSWordEmbedControl.cpp(92) : error C2385: ambiguous access of 'Close'

could be the 'Close' in base 'Microsoft::Office::Interop::Word::_Document'

or could be the 'Close' in base
'Microsoft::Office::Interop::Word::DocumentEvents2_Event'

If I specifically call this->Word::_Document->Close() it then tells me it
will fail at runtime.

How am I supposed to close a document?

My Word object is instantiated as follows:

Using namespace Microsoft::Office:Interop::Word

Word::ApplicationClass msWord;

this->msWord = gcnew Word::ApplicationClass;

My Eventhandler is set up as follows:

void OnNewDoc(Document ^ newDoc) {

if (!this->bDeactivateEventProtection){

Object ^ myMissing=Type::Missing;

thisDoc=this->msWord->ActiveDocument;

newDoc->Close(myMissing,myMissing,myMissing);

else{

this->bDeactivateEventProtection=false;

};

It is Attached to the event as follows:

msWord->ApplicationEvents4_Event_NewDocument += gcnew
Word::ApplicationEvents4_NewDocumentEventHandler(this,&MSWordEmbed::MSWordEmbedControl::OnNewDoc);
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?R3VzdHlu?=,
..\MSWordEmbedControl.cpp(92) : error C2385: ambiguous access of 'Close'

could be the 'Close' in base 'Microsoft::Office::Interop::Word::_Document'

or could be the 'Close' in base
'Microsoft::Office::Interop::Word::DocumentEvents2_Event'

If I specifically call this->Word::_Document->Close() it then tells me it
will fail at runtime.
and if you use the other one - documentEvents2? I've only worked with events
in C# (and other places, but that's the closest to C++), and similar things
show up (through DocumentEvents4). One's supposed to use these, and not the
_Document stuff.

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 :)
 
G

Gustyn

Yes, I would get the same response, that calling Close directly would fail in
runtime.

I solved the problem by using a DocumentClass ^ instead of a Document ^.

I would either use ->Documents["docname"] or ->Documents->Current (after
calling the interator) and typecast the return to a DocumentClass ^.

Then I could call the Close() function. It seems that the Document->Close()
and the various event forms of it are meant to be used indirectly by the
->Documents container. Can't be sure though.
 
C

Cindy M -WordMVP-

Hi Gustyn,
I solved the problem by using a DocumentClass ^ instead of a Document ^.
Thanks for posting the resolution here :) There currently aren't a lot of
C++ folks here, but I'm sure there will be others come by with the same
question!

Cindy Meister
 

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