﻿Attribute VB_Name = "ThisWorkbook"
Attribute VB_Base = "0{00020819-0000-0000-C000-000000000046}"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = True
Attribute VB_TemplateDerived = False
Attribute VB_Customizable = True
Option Explicit

' Paste into THISWORKBOOK of a new SquareClickStandard13.xlam.
Private WithEvents App As Excel.Application
Attribute App.VB_VarHelpID = -1

Private Sub Workbook_Open()
    SC13_Start
End Sub

Private Sub Workbook_AddinInstall()
    SC13_Start
End Sub

Private Sub SC13_Start()
    Dim wb As Excel.Workbook
    Set App = Application
    On Error Resume Next
    Set wb = Application.ActiveWorkbook
    On Error GoTo 0
    If wb Is Nothing Then Exit Sub
    If wb Is ThisWorkbook Then Exit Sub
    SC13_HandleWorkbook wb
End Sub

Private Sub SC13_HandleWorkbook(ByVal wb As Excel.Workbook)
    Dim activeObj As Object
    On Error GoTo Done
    If SquareClickSession.SCSession_IsBusy Then Exit Sub
    If SquareClickSession.SCSession_IsSaving(wb) Then Exit Sub
    SquareClickStandard.SCStd_ProcessWorkbook wb
    Set activeObj = wb.ActiveSheet
    If Not activeObj Is Nothing Then
        If TypeOf activeObj Is Excel.Worksheet Then
            If SquareClickStandard.SCStd_AllowListForWorkbook(wb) Then
                SquareClickCore.SquareClick_PrepareSheet activeObj
            End If
        End If
    End If
Done:
End Sub

Private Sub App_WorkbookOpen(ByVal wb As Excel.Workbook)
    If wb Is ThisWorkbook Then Exit Sub
    SC13_HandleWorkbook wb
End Sub

Private Sub App_WorkbookActivate(ByVal wb As Excel.Workbook)
    If wb Is ThisWorkbook Then Exit Sub
    SC13_HandleWorkbook wb
End Sub

Private Sub App_SheetActivate(ByVal Sh As Object)
    On Error GoTo Done
    If SquareClickSession.SCSession_IsBusy Then Exit Sub
    If TypeOf Sh Is Excel.Worksheet Then
        If Sh.Parent Is ThisWorkbook Then Exit Sub
        If SquareClickSession.SCSession_IsSaving(Sh.Parent) Then Exit Sub
        SquareClickStandard.SCStd_ApplySheet Sh
        If SquareClickStandard.SCStd_AllowListForWorkbook(Sh.Parent) Then
            SquareClickCore.SquareClick_PrepareSheet Sh
        End If
    End If
Done:
End Sub

Private Sub App_SheetChange(ByVal Sh As Object, ByVal Target As Excel.Range)
    On Error GoTo Done
    If SquareClickSession.SCSession_IsBusy Then Exit Sub
    If TypeOf Sh Is Excel.Worksheet Then
        If SquareClickStandard.SCStd_AllowListForWorkbook(Sh.Parent) Then
            SquareClickCore.SquareClick_RefreshEditedRange Target
        End If
    End If
Done:
End Sub

Private Sub App_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
    On Error Resume Next
    If SquareClickSession.SCSession_IsBusy Then Exit Sub
    If TypeOf Sh Is Excel.Worksheet Then
        SquareClickSession.SCSession_OnUserActivity Sh.Parent
    End If
End Sub

Private Sub App_WorkbookBeforeSave(ByVal wb As Excel.Workbook, _
                                   ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If wb Is ThisWorkbook Then Exit Sub
    SquareClickSession.SCSession_BeforeSave wb, Cancel
End Sub

Private Sub App_WorkbookAfterSave(ByVal wb As Excel.Workbook, ByVal Success As Boolean)
    If wb Is ThisWorkbook Then Exit Sub
    SquareClickSession.SCSession_AfterSave wb, Success
End Sub

Private Sub App_WorkbookBeforeClose(ByVal wb As Excel.Workbook, Cancel As Boolean)
    On Error Resume Next
    If wb Is ThisWorkbook Then Exit Sub
    SquareClickStandard.SCStd_ForgetSession wb
End Sub


