BUG? Word/VBA is sometimes unabled to see files opened as hidden

B

Bill Brotherton

I've been searching for the answer to this on various news
groups. The following method, which opens an RTF file with
the "visible" attribute set to false, will sometimes fail
on the line following Documents.Open. It fails where I try
to then activate the document with Documents.Item
(dataDoc).Activate. The method uses On Error to trap the
error and retry by opening it in a visible manner. This
always succeeds when opening it hidden fails. Like I said
this works fine on several instances of word 2000 and on
others it does not. Some have SR1 and some have no service
patch. Any clues would be much appreciated...

BTW: The parameter p_workDoc is obtained prior to the call
like this: workDoc = ActiveDocument.name

Private Function activateDataDoc(p_workDoc As String)
Dim workPath, dataDoc As String
Dim isVisible As Boolean

dataDoc = makeDataDocPath(p_workDoc)

'NOTE: It looks like using "Visible:=False" does not
always work.
On Error GoTo TryVisible

Documents.Open FileName:="""" + dataDoc + """", _
ConfirmConversions:=False, ReadOnly:=False,
AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="",
Revert:=False, _
WritePasswordDocument:="",
WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, Visible:=False
Documents.Item(dataDoc).Activate
isVisible = False
GoTo done

TryVisible:

On Error GoTo err
Documents.Open FileName:="""" + dataDoc + """", _
ConfirmConversions:=False, ReadOnly:=False,
AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="",
Revert:=False, _
WritePasswordDocument:="",
WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, Visible:=True
Documents.Item(dataDoc).Activate
isVisible = True
GoTo done

err:
MsgBox "Unable to open the associated data file (" +
dataDoc + ").", , constants.productName
done:
If isVisible Then
MsgBox "Word was unable to open the data document
as a hidden file. We will open it as a visible file
instead. This means you will see some unnecessary flashing
when data is retrieved. ", , constants.productName
End If
End Function
 
C

Cindy M -WordMVP-

Hi Bill,
The following method, which opens an RTF file with
the "visible" attribute set to false, will sometimes fail
on the line following Documents.Open. It fails where I try
to then activate the document with Documents.Item
(dataDoc).Activate.
In Word 2000, it was basically "impossible" to pick up a
document with .Visible=False by referencing the Documents
collection. That may have been alleviated with a Service Pack
- sounds as if this is so, based on what you describe,
anyway. But I know it was not with the original release.

The problem does not occur in Word 2002, and such documents
also appear in the Window menu's list.

But in Word 2000, to be sure you don't run into the problem,
you need to assign the document to an *object variable* and
work with that.

Note that you should also use & and not + to concatenate
strings in VB.

Example:

Private Function activateDataDoc(p_workDoc As String)
Dim workPath, dataDoc As String
Dim isVisible As Boolean
Dim doc as Word.Document 'or: As Object

dataDoc = makeDataDocPath(p_workDoc)

Set doc = Documents.Open(FileName:="""" & _
dataDoc & """", _
ConfirmConversions:=False, ReadOnly:=False, _
AddToRecentFiles:=False, _
PasswordDocument:="", PasswordTemplate:="", _
Revert:=False, _
WritePasswordDocument:="", _
WritePasswordTemplate:="", Format:= _
wdOpenFormatAuto, Visible:=False
doc.Activate

'Note that you probably no longer need to use .Activate,
since you should no longer be using ActiveDocument. Use the
doc object variable instead of ActiveDocument throughout the
remainder of your code.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Sep
30 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow
question or reply in the newsgroup and not by e-mail :)
 

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