ActiveDocument close...Help!

D

Dian Chapman

Hi folks...

I'm going nuts with a project that keeps giving me a command fail and
I can't figure out why. The app is a custom word app <--> Access DB.

At some points, I want to allow the user to just close the current
document without saving. Due to security reasons, I have to capture
all the save/close commands to run through the custom code so the user
can't save a document outside the scope of the application/DB.

I've tried ever version of:

'Application.Documents.Close SaveChanges:=wdDoNotSaveChanges
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

to get the current document to just close and not save any changes,
should the user elect to toss away and current modifications. But I
continually get a Command Failed error at this point!?!?!?!?

The current doc may or may not have had changes to it. It could be a
locked doc, it could already have a name, it could have been saved
with a password, it could be a blank form doc...depending on when the
user decides to bail, if they do. I thought I've taken into
consideration every scenario...but something is causing this error and
I just can't figure out what/why.

Any thoughts?
TIA...
Dian ~
 
J

Jonathan West

Di D~

Dian Chapman said:
Hi folks...

I'm going nuts with a project that keeps giving me a command fail and
I can't figure out why. The app is a custom word app <--> Access DB.

At some points, I want to allow the user to just close the current
document without saving. Due to security reasons, I have to capture
all the save/close commands to run through the custom code so the user
can't save a document outside the scope of the application/DB.

I've tried ever version of:

'Application.Documents.Close SaveChanges:=wdDoNotSaveChanges
ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges

to get the current document to just close and not save any changes,
should the user elect to toss away and current modifications. But I
continually get a Command Failed error at this point!?!?!?!?

The first version has an error. You can't Close the entire documents
collection all in one go. You can only apply the Close method to individual
Document objects.

The second id closer, but if you are controlling Word from Access, then you
need to qualify the command with the object variable you have created which
makes the link to Word. What is more, if you are using late binding, Access
will have no idea of the value of the wdDoNotSaveChanges constant, because
this is defined within Word, not within Access. Try this.

wrdApp.ActiveDocument.Close SaveChanges:=0

where wrdApp is the name of the object variable created to control Word.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 
J

JGM

Hi Jonathan,

A quick question:

Instead of:
wrdApp.ActiveDocument.Close SaveChanges:=0
can we use:
wrdApp.ActiveDocument.Close SaveChanges:=wrdApp.wdDoNotSaveChanges
?
I think I have seen this before... am I mistaken?
If so, it can be useful if you do not want to look up that actual value of
the Word variable, such as wdDoNotSaveChanges, or wdBlue, and so on...

Thanks
 
D

Doug Robbins - Word MVP

Hi Dian,

I was faced with the same problem when automating Word from Access and got
around it by using

WordDoc.Saved = True
WordDoc.Close

where WordDoc is a document object.

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
J

Jonathan West

Hi Jean-Guy

In order to be able to use the constants you muist use early binding. That
way you do this is to go to Tools References in the VBA editor for Excel or
whichever app is controlling Word, and check the reference to "Microsoft
Word 10.0 Object Library"

Now, the constants are available. There are advantages and disadvantages to
doing this.

The advantage is that it is easier to write the cde, and performance is a
bit better with early binding.

The disadvantage is tat you are rather tied in to being comlatible with only
one version of Word. That may be OK, but if you are distributing your code
to others, then you have to be aware of the versions of office they are
using.

--
Regards
Jonathan West - Word MVP
MultiLinker - Automated generation of hyperlinks in Word
Conversion to PDF & HTML
http://www.multilinker.com
 

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