Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How detect if shared file already in use?

Status
Not open for further replies.

rudo

Programmer
Jul 3, 2002
72
NL
My VBA-programs use shared files for reading and writing.
When shared file is already in use, application stops, micosoft window appears with remark "file already in use" and question "would you like to open a read-only copy".

In my context file should not be opened at all if already in use.

Does anyone know the code to detect if shared file is already in use by someone-else?

Thank you very much in advance
 
Have you tried "Trapping" the error and examining the Err.Number.
Dim lngErr as long
On Error Resume next
Open ......
lngErr = Err.Number
On Error Goto 0
if lngErr = .... then
...in use
End if Generate Forms/Controls Resizing/Tabbing Class
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Ratman's "Canada is on fire=sun is red" special of the day!

I'm not sure if it's a semantics issue here or if there's some confusion with file sharing because true file sharing in Excel was intended for Multi-User functionality.

Consider this....

This code determines if the workbook is:

1. Shared....if so are there more users than yourself ?...if so close it without any alerts or changes.

2. Unshared...as in exclusive access.....if another user has it opened it will return a readonly property.....if so then same close routine.

------------------------------------------------
Sub IsFileShareOrNotOrOpened()

ChDir "C:\Inventory Files"
'----validate path----
FileInQuestion = Dir("C:\Inventory Files\Inventory.xls")
If FileInQuestion <> &quot;&quot; Then

'----open file to test properties----
Workbooks.Open filename:=&quot;C:\Inventory Files\Inventory.xls&quot;
Workbooks(&quot;Inventory.xls&quot;).Activate

'----handles shared workbooks----
If ActiveWorkbook.MultiUserEditing = True Then
'----if shared and other users have opened then close without save----
users = ActiveWorkbook.UserStatus
numofusers = UBound(users, 1)
If numofusers > 1 Then
GoTo closeit
End If
'shared code stuff here
GoTo closeit

'----handles unshared exclusive access workbooks----
End If
If ActiveWorkbook.ReadOnly Then
closeit:
Application.DisplayAlerts = False
Workbooks(&quot;Inventory.xls&quot;).Close
End If
'exclusive access code goes here----
End If
endo:
End Sub
-----------------------------------------------------




If you need more help with this just reply....
:)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top