Macro to alert you when a network file is open by another user

  • Thread starter Michael M via OfficeKB.com
  • Start date
M

Michael M via OfficeKB.com

The feature in Word XP(2002) does not seem to be working for us, that alerts
you to the fact that another user has a network file open and lists options
to open as Read-Only, etc.

I've gotten constant calls from users that it just opens as Read-Only and
never notifies them, I have confirmed this on my own system when another user
had the file open?

Can someone please help me create a macro to run when the file is open, that
checks to see if the file is open already, and if so, displays a message that
it is open by [the username] and does not allow them to open it at all?

Thank you,
Michael M
 
C

Chuck Henrich

Try this - you need to use the old WordBasic command to get the full path and
file name from the file open dialog (the .Name property only returns the
name, not the path as well). The FileLocked function is courtesy the Word
MVPS site (http://word.mvps.org/faqs/macrosvba/CheckIfFileOpen.htm).

Sub OpenFileNetCheck()

Dim strFileName As String

With Dialogs(wdDialogFileOpen)
If .Display <> 0 Then
strFileName = WordBasic.FilenameInfo$(.Name, 1)
End If

End With

If FileLocked(strFileName) = False Then
Documents.Open (strFileName)
End If

End Sub

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 "Another user has " & strFileName & " open. " & _
"Please choose another file.", _
vbExclamation + vbOKOnly, _
"File already open"
End If

End Function
 

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