Results 1 to 20 of 20

Thread: Solved: Can't suppress "Do you want to save..."

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    VBAX Regular
    Joined
    Sep 2008
    Posts
    8
    Location
    Thanks, S.

    I don't want to close unconditionally, as I could be saving and not closing, so I ended up doing this in Workbook_BeforeSave (some of the extra code is courtesy of Ken Puls, w w w dot excelguru dot ca, included hopefully for clarity):

    [vba]
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    '
    ' Code to check conditions
    '
    If Not Conditions_Met Then
    If MsgBox("... Do you still wish to save?", vbYesNo) = vbNo Then
    If bClosing Then ThisWorkbook.Close SaveChanges:=False
    Cancel = True
    Exit Sub
    End If
    End If
    'Turn off screen flashing
    With Application
    .EnableEvents = False
    .ScreenUpdating = False
    End With
    'Record active worksheet
    Set wsActive = ActiveSheet
    'Save workbook directly or prompt for SaveAs filename
    If SaveAsUI = True Then
    vFilename = Application.GetSaveAsFilename( _
    fileFilter:="Excel Files (*.xls), *.xls")
    If CStr(vFilename) = "False" Then
    bSaved = False
    Else
    'Save the workbook using the supplied filename
    Call HideAllSheets
    ThisWorkbook.SaveAs vFilename
    Application.RecentFiles.Add vFilename
    Call ShowAllSheets
    bSaved = True
    End If
    Else
    'Save the workbook
    Call HideAllSheets
    ThisWorkbook.Save
    Call ShowAllSheets
    bSaved = True
    End If
    'Restore file to where user was
    wsActive.Activate
    'Restore screen updates
    With Application
    .ScreenUpdating = True
    .EnableEvents = True
    End With

    'Set application states appropriately
    If bClosing Then
    ThisWorkbook.Close SaveChanges:=False
    Else
    If bSaved Then ThisWorkbook.Saved = True
    Cancel = True
    End If
    End Sub
    [/vba]
    and this in Workbook_BeforeClose:
    [vba]
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
    '
    bClosing = True
    '
    '
    [/vba]
    (bClosing is defined at the top of ThisWorkBook module).
    The only niggle is that closing the workbook within the BeforeSave sub appears to cause Excel to report a problem and that it needs to close. Not a great problem in itself, but now looks messy. (This appears to be a known bug in MS Office, but as all updates are provided centrally by the company I can't just go and get the update from Microsoft.) Any ideas on whether that can be avoided by some code changes?

    Thanks
    Last edited by losinj; 09-24-2008 at 04:28 AM. Reason: Submitted accidentally too soon

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •