Open Sharepoint File Check if Open

uk747

Well-known Member
Joined
Jul 20, 2011
Messages
832
Office Version
  1. 365
Platform
  1. Windows
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

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
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
If there is an issue using the Function IsFileOpen(filename As String) as it is an old code

Is there a better way to check if a sharepoint file exists or is already open before opening it ?
 
Upvote 0
anybody have experience of sharepoint and what is the difference
 
Upvote 0
uk747 have you figured this out? My issue is similar. I'm trying to use VB to open a sharepoint file in the desktop app mode. I get an error because when I try to use the workbook after the code to open it executes, the workbook won't open until the VB code stops execution... I click the debug button, the workbook opens, and then I can continue running the rest of the code.
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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