Workbooks.Open issue

Pyrgos

New Member
Joined
Feb 22, 2012
Messages
30
Hello there,

I have the following code written up in a userform:

Code:
Private Sub UserForm_Initialize()

Const MasterData As String = "MASTER Data.xlsx"
Dim AsNames As Variant
Dim Counter As Long

'CHECK IF FILE IS ALREADY OPEN OR NOT, IsFileOpen is Function that returns True or False

If Not IsFileOpen("C:\General\2013 Master Folder\" & MasterData) Then
    Workbooks.Open FileName:="W:\General\2013 Master Folder\" & MasterData, ReadOnly:=True 'Open Workbook
End If

and so on...

For several reasons, I need this file to be shared.

The issue is that, when the file is shared, it prompts me with the question about re-opening the file all the same. If I disallow multi-user changes, then the file opens (or not) as expected.

Is anyone aware of way to solve this?

Many thanks in advance.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
I suspect your issue is in the IsFileOpen code but since you haven't posted it... ;)
 
Upvote 0
Of course, silly me. :)

Code:
Function IsFileOpen(FileName As String)
    Dim iFilenum As Long
    Dim iErr As Long
     
    On Error Resume Next
    iFilenum = FreeFile()
    Open FileName For Input Lock Read As #iFilenum
    Close iFilenum
    iErr = Err
    On Error GoTo 0
     
    Select Case iErr
    Case 0:    IsFileOpen = False
    Case 70:   IsFileOpen = True
    Case Else: Error iErr
    End Select
    
End Function

I have copied and pasted this function from some place and since it never gave me problems before, I have constantly used without caring too much about what's in there.

I will have a look at the alternatives on the link, thank you very much for that!
 
Last edited:
Upvote 0
Yep, that's the issue if you share a workbook (which you really really really really really ought to avoid if at all possible). You'll need to loop through all the workbooks and test the FullName property of each against your file path.
 
Upvote 0
Yeah, I suspected so. I guess it's time to read more about ExecuteExcel4macro(). I guess that would be the best solution.
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,932
Members
449,480
Latest member
yesitisasport

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