Checkout workbook in VBA

后端 未结 2 647
青春惊慌失措
青春惊慌失措 2021-01-23 14:30

I have some VBA i\'m trying to get to check out a workbook before making changes. The VBA code sits in its own work book, and is activated by a user opening the other work book

相关标签:
2条回答
  • 2021-01-23 14:39
    Sub CheckOutAndOpenFromSharePoint()
    Dim FilePath As String
    Dim FileName as string
    
    FilePath = "http://Sharepoint site/Folder Name On SharePoint Site/"
    FileName = "Testing.xlsx"
    If Workbooks.CanCheckOut(FilePath & FileName) = True Then
        Workbooks.CheckOut "http://Sharepoint%20site/Folder%20Name%20On%20SharePoint%Site/Testing.xlsx"
        Workbooks.Open Filename:=FilePath & FileName
    Else
        MsgBox FileName & " can't be checked out at this time.", vbInformation
    End If
    End Sub
    
    0 讨论(0)
  • 2021-01-23 14:52

    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.

    0 讨论(0)
提交回复
热议问题