Workbook.CheckIn在第一次被调用时总会产生错误?
我有一些工作簿存储在Sharepoint 2007上的文档库中。我想检出一个工作簿,对其进行修改并重新检入。
使用下面的代码:
Option Explicit Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) Sub test() Dim bk As Workbook Dim path As String path = "http://sharepoint/sites/test/TEST_Relink.xlsm" If Workbooks.CanCheckOut(path) Then Application.DisplayAlerts = False Workbooks.CheckOut path DoEvents Set bk = Workbooks.Open(path, False) bk.Sheets("test").Range("h1").Value = "modified " & Now DoEvents Sleep 10000 bk.checkIn True Application.DisplayAlerts = True End If End Sub
bk.checkIn
调用总是会产生以下运行时错误:
Method 'CheckIn' of object '_Workbook' failed
进入Debug之后,我按F5
继续,签入始终成功。
我在Sleep 10000
添加了10秒的延迟,因为我想这可能是要花一些时间才能传播到服务器。 但是无论我为Sleep
设定了多less时间,这个问题也一直在发生。 有什么想法吗?
编辑:
我尝试使用.CanCheckIn
循环检查如下:
While Not bk.CanCheckIn DoEvents Wend bk.checkIn True
这给了同样的错误。
对于那些像我这样发现的人,我有
Workbooks(logFileName).CheckIn SaveChanges:=True, Comments:="New row added from " & mainFile
这产生了像你的错误消息,但是在进入debugging和按f5会采取行动。 所以这里是我复杂的解决scheme…..我只是将代码拆分为以下
Workbooks(logFileName).Save Workbooks(logFileName).CheckIn Comments:="New row added from " & mainFile
希望这可以帮助别人。
用这个:
Dim xl As Excel.Application Set xl = CreateObject("Excel.Application") xl.AutomationSecurity = msoAutomationSecurityForceDisable xl.EnableEvents = False xl.DisplayAlerts = False 'code to checkin/checkout xl.EnableEvents = True xl.DisplayAlerts = True
你可能已经知道了,但是我想我会把它发给其他任何来这里寻找答案的人。
如果您将SaveChanges
设置为True
那么您还必须将Comments
设置为一个string(null值不会)
所以在你的例子中,你需要这样做:
bk.CheckIn True, ""