5

I am working on a project which essentially looks like this:

if workbooks.canCheckOut(filename) = true then    
  workbooks.CheckOut(filename)
  set workbookVariable = workboooks.Open(filename)
else    
   ' Pesudocode: Display error message
   '             Quit
end if

' Pseudocode: Do some stuff with workbookVariable.    
If workbookVariable.saved = false then     
    workbookVariable.save
end if

If workbookVariable.canCheckIn then    
    workbookVariable.checkIn
    Set workbookVariable = Nothing
else    
    msgbox "Error message goes here", vbCritical
end if    
' Pseudocode: quit

(Edit: I had "If workbookVariable.Saved = True"; which was incorrect. No point in saving a file if it's already saved... So this is now updated to reflect what I really have.)

If the workbook is modified and gets saved, everything runs fine. It saves, gets checked back in to SharePoint, and the program ends normally.

However, if the workbook does not get modified, when it goes to the check back in portion of the program, excel crashes, and I get an error message: "Automation Error".

I even tried putting in a wait command after the checkIn command, with no luck...

What is going on here? Can someone point me in the right direction?

1 Answers1

0

When no changes have been made (workbookVariable.saved = true) use workbookVariable.CheckIn(False). You don't want to CheckIn a revision that's exactly the same as the original. Waste of space at the very least. This should discard your "CheckOut". See MSDN.

Thanks to this page for pointing out what should have been obvious.

Final point of curiosity: Why check out the workbook when you might not make changes to it?

Michael Richardson
  • 4,213
  • 2
  • 31
  • 48
  • As it turned out, I ended up not checking it out, instead, i checked for a lock on the file. My main reason for checking it out was that the macro _might_ have made changes, but until it ran, we didn't know if it would. – Steven C. Britton Feb 12 '13 at 06:24
  • Incidentally, I tried workbookVariable.checkIn False (without the parentheses) however that crashed as well. – Steven C. Britton Feb 12 '13 at 06:27