Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

How can I know if  a specified document is opened

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
animator - 22 Dec 2004 11:58 GMT
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?
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.