Querying status of file

JohnLee

Board Regular
Joined
Oct 24, 2006
Messages
80
Hi,

I have a macro that opens a workbook. However, if the workbook is currently being used by another person, I would like to display a message to the effect that "the workbook is in use - please try later.", and preempt the standard messagebox that is shown "xxxx.xls is locked for editing by AN Other. Open 'Read-Only' or, click 'Notify' to open read-only and receive notification when the document is no longer in use."

Is there a way to query whether a workbook is in use before actually trying to open it?

This is the code as it stands:

Code:
Set wbMbr = Workbooks.Open("T:\Mbr.xls", 3, False))

If wbMbr.ReadOnly = True Then
    MsgBox "Data is currently being maintained by another person. " _
        & vbNewLine & vbNewLine _
        & "Please try again later!", _
        vbCritical, "Data maintenance"
        
    wbMbr.Close savechanges:=False
    Exit Sub
End If

Thanks in anticipation

John
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi John

You can do this with the following function (which I got from Acw/Jindon):

Code:
 Sub Your_Macro()
Select Case IsFileOpened("T:\Mbr.xls")
   Case 1
      Msgbox "File in use! Please try again later": exit sub
   Case 2
       Msgbox "File Not Found!": Exit sub
End Select

Set wbMbr = Workbooks.Open("T:\Mbr.xls", 3, False)) 

'rest of our code....

end sub

Function IsFileOpened(StrFilePath As String) As Integer
    Dim FileNum As Integer
     
     'First check filepath exists
    If Len(Dir(StrFilePath)) > 0 Then
        FileNum = FreeFile()
        On Error Resume Next
        Open StrFilePath For Input Lock Read As #FileNum  ' Open file and lock it.
        If Err.Number <> 0 Then
            IsFileOpened = 1 'File open
        Else
            IsFileOpened = 0 'File Closed
        End If
        Close FileNum
    Else
        IsFileOpened = 2 'File not found
    End If
     
End Function
 
Upvote 0

Forum statistics

Threads
1,214,657
Messages
6,120,776
Members
448,991
Latest member
Hanakoro

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top