Documents(0) - what is it?

G

gregor2324

Hi,

I have a problem with my VB app using VBA for Word 97, sometimes it
goes crazy when I
try to localize my document name using Documents(i) when i iterates
from 1 to Documents.Count.

I noticed that usually the object Documents(0) does not exist, but if
it does, my application goes wrong (it can't find my document and so
can't close it, the file is open and I get Permission Denied error when

I try to save it.) These problems exist only when Documents(0) exists
and usually holds the document I can't locate.
In the documentation I read that the first document is however,
Documents(1). Moreover, in that case two names are usually the same!
For example Documents(2).Name = Documents(3).Name is True! What's the
problem?


Thanks in advance.
 
T

Tony Jollans

Documents(0) should never exist and, even if it did, iterating from 1 to
Documents.Count would never stumble across it.

Can you be a bit more precise in stating what your problem is, what the
error is, in what code, and under what circumstances it occurs. What makes
you think you have a documents(0)?

Secondly, Documents(2).Name = Documents(3).Name can be true if the two
documents are in different folders - use FullName or Path to differentiate
them.
 
G

gregor2324

Tony Jollans napisal(a):
Documents(0) should never exist and, even if it did, iterating from 1 to
Documents.Count would never stumble across it.

Can you be a bit more precise in stating what your problem is, what the
error is, in what code, and under what circumstances it occurs. What makes
you think you have a documents(0)?

Secondly, Documents(2).Name = Documents(3).Name can be true if the two
documents are in different folders - use FullName or Path to differentiate
them.
Here's my code
......................
'I 've created a new document under new name Document4
set docs = mobjWord1.Documents
'Now I check if Word has opened the old document
For Each doc In docs
If doc.Name = offer.oname + ".doc" Then
doc.Close ' this never executes
End If
Next


strFileName = "c:\offers\files\" & offer.oname & ".doc"
If Len(Dir(strFileName)) Then
If MsgBox("Overwrite existing offer?", vbYesNo) = vbYes Then
Kill strFileName ' this causes Permission Denied (the file
is blocked by Word)

Now a look in the Immediate Window shows the following:
? offer.oname
Geschaft
? docs.Count
2
? docs(0).Name
Geschaft.doc
? docs(1).Name
Document4
? docs(2).Name
Document4
 
T

Tony Jollans

Hi, Gregor,

A partial explanation (perhaps): unless you have "Option Compare Text"
specified, comparisons in VBA will be case-sensitive, so if your document is
called geschaft.doc it would not be closed but it would be found by Dir.

The Documents(0) is very odd. Where are you running this code from that you
have an object reference to the Word application?
 

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