I can open a normal File using code below, but it wont open a sharepointfile as it fails on the If IsFileOpen(myFile) Then
If I remove the check to see if file is already open it will work, but I want to check if it exists or is already open before opening
If I remove the check to see if file is already open it will work, but I want to check if it exists or is already open before opening
VBA Code:
Sub testuk()
Dim myFile As String
myFile = "https://mysharepoint/text.xlsx" ' Get Error Run-time error '52': Bad file name or number
myFile = "c:\My\Docs\text.xlsx" ' This Works
If IsFileOpen(myFile) Then
MsgBox ("File Open")
Exit Sub
End If
Set myWorkbook = Workbooks.Open(myFile)
End Sub
Function IsFileOpen(filename As String)
Dim filenum As Integer, errnum As Integer
On Error Resume Next ' Turn error checking off.
filenum = FreeFile() ' Get a free file number.
' Attempt to open the file and lock it.
Open filename For Input Lock Read As #filenum
Close filenum ' Close the file.
errnum = Err ' Save the error number that occurred.
On Error GoTo 0 ' Turn error checking back on.
' Check to see which error occurred.
Select Case errnum
' No error occurred.
' File is NOT already open by another user.
Case 0
IsFileOpen = False
' Error number for "Permission Denied."
' File is already opened by another user.
Case 70
IsFileOpen = True
' Another error occurred.
Case Else
Error errnum
End Select
End Function