VBA中的结帐工作簿

时间:2014-03-11 16:15:54

标签: excel vba sharepoint

我有一些VBA我试图在进行更改之前检查工作簿。 VBA代码位于其自己的工作簿中,并由用户打开其他工作簿并选择执行代码以修改工作簿的按钮来激活。一开始我想检查工作簿是否已签出。我得到的问题是Workbooks.CanCheckOut(ActiveWorkbook.FullName)总是返回false,即使没有签出工作簿。

If Workbooks.CanCheckOut(ActiveWorkbook.FullName) = True Then
    Workbooks.CheckOut (ActiveWorkbook.FullName)
    MsgBox "This workbook has been checked out"
    Process = True
ElseIf ActiveWorkbook.CanCheckIn = False Then 'if not checked out
    Process = False
    MsgBox ("The Document may not be checked out, Import Process is ending.")
Else
    Process = True
End If 'If CanCheckin = False

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

我通过反复试验发现Workbooks.CanCheckOut (Filename:= FullName)其中FullName是SharePoint文件的URL 仅适用于当前Excel实例中未打开的文件。

如果您在Excel的当前实例中打开文件,该方法将始终返回False。显然就是这种情况。

Workbooks.CheckOut (ActiveWorkbook.FullName)打开文件,检查出来,然后莫名其妙地关闭文件。因此,打开和签出SharePoint文件将变为一个3步骤。

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

这有点反直觉,因为当手动使用SharePoint文件时,您必须打开它们以查看它们是否可以签出然后执行签出操作。

如果在当前Excel实例中打开文件,MSDN或Excel VBA都没有提及Workbooks.CanCheckOut (Filename:= FullName)方法始终返回False。

答案 1 :(得分:0)

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