excelvba

How to Not prompt "save changes" without using Thisworkbook.saved = true?


When I open my workbook a macro runs a Yes/No question, which basically asks if I want to continue or not. If I say NO, I want the macro to close the book, without saving changes en also without asking if I want to save changes. I can't seem to get the <Application.DisplayAlerts = False> in the right place or something, because it keeps asking...

Tried different stuff, this is what I have right now:

Private Sub workbook_open()

Application.DisplayAlerts = True

    'controlevraag drive gesynced?
        Dim Antwoord1
        Antwoord1 = MsgBox("Heb je gecheckt of drive gesynced is en je daardoor dit doc kunt gebruiken?", vbQuestion + vbYesNo, "Drive sync gecheckt?")
    
            If Antwoord1 = vbYes Then
                GoTo Start1
            ElseIf Antwoord1 = vbNo Then
                MsgBox "Dan sluit ik nu dit doc zodat je dat eerst kunt checken, tot zo!", 
                vbOKOnly, "Eerst checken aub"
                Application.DisplayAlerts = False
                ThisWorkbook.Close Savechanges:=False
 End   
            End If

Start1:

(Yes part is going fine so I won't post that)

I think I can't use <ThisWorkbook.saved = true > , because of this other code in the workbook module:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

    If ThisWorkbook.Saved = True Then
        Call AlleGSheet_netjes
        Call Juiste_Beveiliging
        ThisWorkbook.Save
    Else
    End If

'Application.DisplayAlerts = True

End Sub

Solution

  • Close Workbook When Before Close

    ThisWorkbook Module

    Option Explicit
    
    Private NietGesynced As Boolean
    
    Private Sub Workbook_Open()
    
        'controlevraag drive gesynced?
        Dim Antwoord1 As Long: Antwoord1 = MsgBox( _
            "Heb je gecheckt of drive gesynced is en je daardoor dit doc kunt gebruiken?", _
            vbQuestion + vbYesNo + vbDefaultButton2, "Drive sync gecheckt?")
        
        If Antwoord1 = vbNo Then
            MsgBox "Dan sluit ik nu dit doc zodat je dat eerst kunt checken, tot zo!", _
                vbExclamation, "Eerst checken aub"
            NietGesynced = True
            Me.Close Savechanges:=False
        End If
    
        MsgBox "Open... staying open!", vbInformation
    
    End Sub
    
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    
        If NietGesynced Then Exit Sub
        If Me.Saved Then ' was saved
            'Call AlleGSheet_netjes
            'Call Juiste_Beveiliging
            MsgBox "Before closing... closing!", vbInformation
            Me.Save ' save again
        Else ' was not saved
            MsgBox "Before closing... not closing!", vbExclamation
            Cancel = True ' not closing
        End If
    
    End Sub