简体   繁体   中英

VBA - Open & Check out Excel workbook from SharePoint

I'm trying to write some data into an Excel workbook that is hosted in our SharePoint document library. I'm instantiating Excel from Microsoft Project.

I tried using the following procedure:

  1. Check if file can be checked out
  2. If it can be checked out, then open it

Here's the code snippet:

If ExcelApp.Workbooks.CanCheckOut (FileURL) = True Then
    Set NewBook = ExcelApp.Workbooks.Open(FileName:=FileURL, ReadOnly:=False)
    ExcelApp.Workbooks.CheckOut (FileURL)
Else
    MsgBox "File is checked out in another session."
End If

For some reason the CanCheckOut function always return FALSE for me, and I'm not able to tell when a file can be checked out by the Excel instance. Is it not working because I'm calling the VBA code from MS Project?

My app should be able to check if a file is not checked out, then check it out, update it, and save + check it back in. It should be incredibly simple, but apparently the Check-Out / Check-In is not implemented correctly in VBA...?

I've found through trial and error that Workbooks.CanCheckOut (Filename:= FullName) where FullName is the URL for the SharePoint file only works for files that are not open in the current instance of Excel.

The method will always return False if you have the file open in the current instance of Excel which is obviously the case here. Workbooks.CheckOut (ActiveWorkbook.FullName) opens the file, checks it out and then inexplicably, closes the file. So opening and checking out a SharePoint file becomes a 3 step process.

Sub CheckOutAndOpen()
Dim TestFile As String

TestFile = "http://spserver/document/Test.xlsb"
If Workbooks.CanCheckOut(TestFile) = True Then
    Workbooks.CheckOut(TestFile)
    Workbooks.Open (TestFile)
Else
    MsgBox TestFile & " can't be checked out at this time.", vbInformation
End If
End Sub

This is all a bit counter intuitive because when working manually with SharePoint files you have to open them to see if they can be checked out and then perform the check-out operation. Neither MSDN or Excel VBA help mention that the Workbooks.CanCheckOut (Filename:= FullName) method always returns False if you have the file open in the current instance of Excel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM