ActiveDocument & multiple open documents

G

Gordon

Hello,

I am have a number of macros written in a template that
reference ActiveDocument.

These macros seem to work fine when only one document is
open. However, my users report getting runtime error
5941: "The requested member of the collection does not
exist" without being able to pinpoint what is causing it.

I have a hunch it is because of the use of
ActiveDocument. Is that a likely cause?

If so, how do I avoid the use of ActiveDocument? Can I
create an document objective that all macros in my
template can refer to? If so, how?

Thanks,
Gordon
 
D

Dave Lett

Hi Gordon,

Well, it could be, but it really depends on the situation. I don't think you've given enough information to pinpoint the cause. The "collection does not exist" error occurs, generally, when you try to access a collection when there isn't a collection or you try to access an item in a collection that is higher than what the document actually has. For example, you'll raise the error if you run
ActiveDocument.Tables(1).Range.Selec
in a document with no tables
You'll also raise the error if you ru
ActiveDocument.Tables(10).Range.Selec
in any document with fewer than 10 tables

Therefore, we can't really say that ActiveDocument is the culprit when we don't have more to go on

HTH
Dave
 
J

Jonathan West

Hi Gordon,

If you are dealing with multiple documents in a macro, or your macro will
run for long enough that you might accidentally click on another dcument in
the taskbar while the macro is running, then you should assign an object
variable to the document(s) you are using.

To set a variable for the document which is active at the start of the acro,
use this

Dim oDoc1 as Document
Set oDoc1 = ActiveDocument

Then, wherever you were previously using ActiveDocument in your macro, you
can now use oDoc1, and there won't be a problem.

If you create a new document, and want to ensure that you don't get mixed up
between documents, then you can assign an object variable to it like this

Dim oNewDoc as Document
Set oNewDoc = Documents.Add(Template:=C:\Templates\My Template.dot)

Now, having a separate object variabkle for it, you don't have top bother
with using the Activate method to siwtch between one document and the other,
they both have their own object variable which uniquely identifies the
document.

If you want to open a document instead of create a new one, that can also be
done, like this

Dim oOpenedDoc asDocument
Set oOpenedDoc = Documents.Open(FileName:="C:\My Documents\This
document.doc")

The object variables will remain attached to the correct document until you
close the document. You can do anything with these document variables that
you can do with ActiveDocument.
 
G

Gordon

Dave,

Thank you for your response. I am still fairly new to
VBA, so I'm not 100% clear on the meaning of some of the
error messages. Here is a little more information.

First of all, I'm using Word 2000.

My template includes, amoung its macros, a procedure that
overrides the "save" and "save as" command. Most of this
has been developed using the MVP web site and Googling
these newsgroup. So I have

Sub FileSave()

'Check if the document has ever been saved before
If Len(ActiveDocument.Path) = 0 Then
'It's a new document that was never saved
CustomSaveAs
Else
'The document was saved before, just resave it
ActiveDocument.Save
End If
End Sub

The CustomSaveAs procedure is longer, but basically it
uses the content of a form field to create a default file
name.

My hypothesis is this:

1) When another document (not based on my template) is
open, this procedure is still being run during its save
process. Since the other document obviously will not have
the correct form fields, the error will occur.

2) If users utilize the autorecover feature, the other
document is saving without the user doing anything, so to
the user, the error seems to come out of nowhere.

Do either of these things seem probable?

Thanks

-----Original Message-----
Hi Gordon,

Well, it could be, but it really depends on the
situation. I don't think you've given enough information
to pinpoint the cause. The "collection does not exist"
error occurs, generally, when you try to access a
collection when there isn't a collection or you try to
access an item in a collection that is higher than what
the document actually has. For example, you'll raise the
error if you run
ActiveDocument.Tables(1).Range.Select
in a document with no tables.
You'll also raise the error if you run
ActiveDocument.Tables(10).Range.Select
in any document with fewer than 10 tables.

Therefore, we can't really say that ActiveDocument is the
culprit when we don't have more to go on.
 
G

Gordon

Jonathan,

Thank you for your reply.

So, just to clarify, I have to create this Document
object in _every_ macro in my template? I was hoping that
I could just create one Document object for my template
that each macro could refer to. But from your
description, that doesn't sound possible.

Gordon
 
D

Dave Lett

Hi Gordon

1) AFAIK, the document based on some other template should _not_ use the custom FileSave and FileSaveAs procedures (as long as those procedures are truly in the template and not Normal.dot). If the procedures are in Normal.dot, then they will most likely fire
2) I don't quite follow you here. You mention another document, but I'm not sure what you mean by it. Sorry

Dave
 
G

Gordon

Dave,

Thanks for debunking #1 for me. It didn't make sense to
me either, but I was grasping at straws.

If #1 is not possible, then #2 can't be the issue, so I
won't bother trying confuse you any more. ;-)

Thanks for all of your help!
Gordon
-----Original Message-----
Hi Gordon,

1) AFAIK, the document based on some other template
should _not_ use the custom FileSave and FileSaveAs
procedures (as long as those procedures are truly in the
template and not Normal.dot). If the procedures are in
Normal.dot, then they will most likely fire.
2) I don't quite follow you here. You mention another
document, but I'm not sure what you mean by it. Sorry.
 
J

Jonathan West

Gordon said:
Jonathan,

Thank you for your reply.

So, just to clarify, I have to create this Document
object in _every_ macro in my template? I was hoping that
I could just create one Document object for my template
that each macro could refer to. But from your
description, that doesn't sound possible.

You should do this for every macro that you decide needs it. The thing is
that the link between the object variable and the docuemnt is established at
the time you use the Set command to assign the document to the object. That
normally would happen at the start of the macro.
 

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