1

My sub Workbook_BeforeClose runs twice, because in my Sub CloseWBFromSharePointFolder, I either check in my file, discard it or cancel and do nothing (see code below). Both the check in and the discarding of the file trigger Workbook_BeforeClose to run again.

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    CloseWBFromSharePointFolder
End Sub

Both snippets from CloseWBFromSharePointFolder which trigger Workbook_BeforeClose

Check in

ActiveWorkbook.CheckIn SaveChanges:=True, Comments:="Checked-In by " & Application.Username

Discard

Application.ActiveWorkbook.CheckIn False

Any help would be appreciated.

P.s. I also tried to use a public variable to track if it runs again. This does not work because the public variable got reset. The explanation I found is because Workbook_BeforeClose calls CloseWBFromSharePointFolder, which then triggers Workbook_BeforeClose. This resets everything and the public variable becomes empty

P.s.2 for more details.

CloseWBFromSharePointFolder Code

Sub CloseWBFromSharePointFolder()
    Dim myForm1 As UserForm1

    Set myForm1 = UserForm1

    myForm1.Caption = "Choose before closing:"
    myForm1.Show
End Sub

UserForm1 Code

Dim Buttons() As New BtnClass

Private Sub UserForm_Initialize()
    Dim ButtonCount As Integer
    Dim ctl As Control

     ' Create the Button objects
     ButtonCount = 0
     For Each ctl In UserForm1.Controls
        If TypeName(ctl) = "CommandButton" Then
        'Skip the OKButton
            If ctl.Name <> "OKButton" Then
                ButtonCount = ButtonCount + 1
                ReDim Preserve Buttons(1 To ButtonCount)
                Set Buttons(ButtonCount).ButtonGroup = ctl
            End If
        End If
    Next ctl
    Me.CommandButton1.Caption = "Check in"
    Me.CommandButton2.Caption = "Discard check-out"
    Me.CommandButton3.Caption = "Keep checked-out"
    Me.CommandButton4.Caption = "Cancel"
End Sub

BtnClass Code

Public WithEvents ButtonGroup As MsForms.CommandButton
Private Sub ButtonGroup_Click()

If UserForm1.Visible = True Then
    Select Case ButtonGroup.Name
        Case "CommandButton1" 'check in
            CheckIn
        Case "CommandButton2" 'Discard check-out
            Discard
        Case "CommandButton3" 'Keep checked-out
            KeepCheckedOut
        Case Else ' Cancel
            'Do Nothing
    End Select
    Unload UserForm1
ElseIf UserForm2.Visible = True Then
    Select Case ButtonGroup.Name
        Case "CommandButton1" 'check out
            CheckOut
        Case "CommandButton2" 'Read only
            'Do Nothing
        Case Else ' Cancel
            'Do Nothing
    End Select
    Unload UserForm2
End If
End Sub

Sub CheckIn()
    If ActiveWorkbook.CanCheckIn = True Then
        'Check In, Save and Close
        ActiveWorkbook.CheckIn SaveChanges:=True, Comments:="Checked-In by " & Application.Username
        MsgBox ("File sucessfully checked in")
    Else
        MsgBox ("File could not be checked in!")
    End If
End Sub
Thundereagle
  • 143
  • 1
  • 1
  • 10

0 Answers0