In my VBA program, I must know if a document named "some.doc" is opened. If
the document is not opened, I should create a new file, which is "some.doc";
and if it is opened, I will do something else.
Sorry, I am a foreigner, my English is poor. Do you understand my
message? Can you help me?
Helmut Weber - 22 Dec 2004 12:30 GMT
Hi,
sure we do understand. We are used to foreigners,
especially me, as I am one myself, here. ;-)
There is a simple way, if you want to know, whether
a doc has been opened by the instance of word, you are working with.
Dim oDoc As Document
For Each oDoc In Application.Documents
If oDoc.Name = "Frank-02.DOC" Then
MsgBox "found"
End If
Next
Note, oDoc.Name is case sensitive, which means,
is distinguishes between small and capital letters, like "a" vs. "A"
so better use:
If LCase(oDoc.Name) = LCase("frank-02.DOC") Then
MsgBox "found"
End If
Using "fullname" instead of "name" might be a good idea, too
Some more questions? Special Christmas offer!
If it comes to several instances of word, or if you want to know,
whether a document has been opened by another user or another instance
of yourself or by another application, things get a bit more
complicated.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
Chuck - 22 Dec 2004 14:37 GMT
If you want to check whether a file is in use (any kind of file, opened by
anyone visible to you on your network) you can use the following function:
Function FileLocked(strFileName As String) As Boolean
On Error Resume Next
' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
Open strFileName For Binary Access Read Lock Read As #1
Close #1
' If an error occurs, the document is currently open.
If Err.Number <> 0 Then
FileLocked = True
Err.Clear
MsgBox strFileName & " is in use.", _
vbExclamation & vbOKOnly, _
"File already open"
End If
End Function
How to call FileLocked function: in any macro use the line
>FileLocked("c:\yourdochere.doc")<
For instance the following macro tests to see if the file C:\Temp\Temp.doc
is open. If it is not open then it opens it (if it is open you'd get the
error message from the FileLocked function, see above).
Sub test()
If Not FileLocked("C:\Temp\Temp.doc") Then
Documents.Open "C:\Temp\Temp.doc"
End If
End Sub
Hope this helps.
Chuck
> In my VBA program, I must know if a document named "some.doc" is opened. If
> the document is not opened, I should create a new file, which is "some.doc";
> and if it is opened, I will do something else.
> Sorry, I am a foreigner, my English is poor. Do you understand my
> message? Can you help me?
Chuck - 22 Dec 2004 14:39 GMT
PS: credit where due -- I got the FileLocked function off the MVP site,
http://word.mvps.org/faqs/macrosvba/CheckIfFileOpen.htm
> In my VBA program, I must know if a document named "some.doc" is opened. If
> the document is not opened, I should create a new file, which is "some.doc";
> and if it is opened, I will do something else.
> Sorry, I am a foreigner, my English is poor. Do you understand my
> message? Can you help me?