+ Reply to Thread
Results 1 to 8 of 8

Force "Save As" in Prompt Before Close - X Button

Hybrid View

  1. #1
    Registered User
    Join Date
    05-30-2013
    Location
    São Paulo, Brazil
    MS-Off Ver
    Excel 2010
    Posts
    12

    Question Force "Save As" in Prompt Before Close - X Button

    Hi everyone,

    I need a macro that will replace the function SAVE in close prompt dialog ("Do you want to save the changes you made to book?"). It's possible to replace the function to SAVE AS, instead of SAVE (when a user modify a existing spreadsheet an then close it in X button, the file will be overwritten, and we want to change it). If isn't possible, can i disable the button SAVE from this prompt and keept the others (DON'T SAVE and CANCEL)?

    And:

    It's possible to open documents of certain paths as read-only? (example: if a documents is opened from My Documents and Desktop it will be read-only, but from others locations not).

    Thank you in advance

  2. #2
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Force "Save As" in Prompt Before Close - X Button

    For the saveas issue try:
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        Dim vSaveAs, sInfo1(1 To 2), sInfo2(1 To 2)
        On Error GoTo ErrHandler
        
        Application.EnableEvents = False
        Application.DisplayAlerts = False
        
        Select Case MsgBox("Want to save your change to '" & Me.Name & "' to a file?", vbYesNoCancel + vbExclamation, "Microsoft Excel")
            Case vbCancel
                Cancel = True
                GoTo ErrHandler
            Case vbNo
                Cancel = True
                ActiveWorkbook.Close False
            Case vbYes
                sInfo1(1) = Me.Name
                sInfo1(2) = Me.Path
                vSaveAs = Application.Dialogs(xlDialogSaveAs).Show(arg1:=Me.Name)
                Select Case vSaveAs
                    Case False
                        Cancel = True
                    Case Else
                        sInfo2(1) = ActiveWorkbook.Name
                        sInfo2(2) = ActiveWorkbook.Path
                        ActiveWorkbook.SaveAs Filename:=vSaveAs
                        ActiveWorkbook.Close False
                End Select
        End Select
        Err.Clear
    ErrHandler:
        With Err
            If .Number <> 0 Then
                MsgBox "Nr.: " & .Number & vbLf & .Description
            End If
        End With
        Application.DisplayAlerts = True
        Application.EnableEvents = True
    End Sub
    For the other one: I don't see a possibility to tell the excel application to check the file location of a file you double clicked to open and set readOnly appropriately in VBA. Maybe somebody else has an idea?
    Please use [CODE]-TAGS
    When your problem is solved mark the thread SOLVED
    If an answer has helped you please click to give reputation
    Read the FORUM RULES

  3. #3
    Registered User
    Join Date
    05-30-2013
    Location
    São Paulo, Brazil
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Force "Save As" in Prompt Before Close - X Button

    Really thank you tehneXus. That was exactly what I needed.

    But i found a issue. I created a XLAM addin, and added to THIS WORKBOOK (not to a module). When I close the spreadsheet (saving or not, the application remains opened (the XLAM addin - Want to save your change to Book1.XLAM to a File).
    Then a I try to close it and the the save prompt is shown again.

    If I choose YES, I get a error: Nr.:91 Object variable or With block variable not set.

    If a choose NO, I got: N.:1004. Unable to get the Show property of the Dialog class.

    I replaced ActiveWorkbook to ThisWorkbook, and the error was gone, but if I choose NO in the SAVE AS prompt, i get the SAVE prompt again (and then, can overwrite the file).

  4. #4
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Force "Save As" in Prompt Before Close - X Button

    HI,

    I got it to work in an add-in but didn't found a solution to quit the application properly...

    Add-In "ThisWorkbook" module
    Option Explicit
    
    Private MySheetHandler As clsWbEvents
    
    Private Sub Workbook_Open()
        Set MySheetHandler = New clsWbEvents
    End Sub
    Add-In ClassModule called: "clsWbEvents"
    Option Explicit
    
    Public WithEvents xlApp As Application
    
    Private Sub Class_Initialize()
        Set xlApp = Application
    End Sub
    
    Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
        
        Dim vSaveAs
        On Error GoTo ErrHandler
        
        If Wb.IsAddin Then Exit Sub
        
        xlApp.EnableEvents = False
        xlApp.DisplayAlerts = False
        
        Select Case VBA.MsgBox("Want to save your change to '" & Wb.Name & "' to a file?", vbYesNoCancel + vbExclamation, "Microsoft Excel")
            Case vbCancel
                Cancel = True
                GoTo ErrHandler
            Case vbNo
                Cancel = True
                Wb.Close False
                GoTo AppQuit
            Case vbYes
                vSaveAs = xlApp.Dialogs(xlDialogSaveAs).Show(arg1:=Wb.Name)
                Select Case vSaveAs
                    Case False
                        Cancel = True
                    Case Else
                        Wb.SaveAs Filename:=vSaveAs
                        Wb.Close False
                        GoTo AppQuit
                End Select
        End Select
        Err.Clear
    ErrHandler:
        If Err.Number <> 0 Then
            MsgBox "Nr.: " & Err.Number & vbLf & Err.Description
        End If
        Err.Clear
        xlApp.DisplayAlerts = True
        xlApp.EnableEvents = True
        Exit Sub
    AppQuit:
        Set vSaveAs = Nothing
        Set Wb = Nothing
        If xlApp.Workbooks.Count = 0 Then xlApp.Quit
        Set xlApp = Nothing
    End Sub

  5. #5
    Registered User
    Join Date
    05-30-2013
    Location
    São Paulo, Brazil
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Force "Save As" in Prompt Before Close - X Button

    Quote Originally Posted by tehneXus View Post
    HI,

    I got it to work in an add-in but didn't found a solution to quit the application properly...

    Add-In "ThisWorkbook" module
    Option Explicit
    
    Private MySheetHandler As clsWbEvents
    
    Private Sub Workbook_Open()
        Set MySheetHandler = New clsWbEvents
    End Sub
    Add-In ClassModule called: "clsWbEvents"
    Option Explicit
    
    Public WithEvents xlApp As Application
    
    Private Sub Class_Initialize()
        Set xlApp = Application
    End Sub
    
    Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
        
        Dim vSaveAs
        On Error GoTo ErrHandler
        
        If Wb.IsAddin Then Exit Sub
        
        xlApp.EnableEvents = False
        xlApp.DisplayAlerts = False
        
        Select Case VBA.MsgBox("Want to save your change to '" & Wb.Name & "' to a file?", vbYesNoCancel + vbExclamation, "Microsoft Excel")
            Case vbCancel
                Cancel = True
                GoTo ErrHandler
            Case vbNo
                Cancel = True
                Wb.Close False
                GoTo AppQuit
            Case vbYes
                vSaveAs = xlApp.Dialogs(xlDialogSaveAs).Show(arg1:=Wb.Name)
                Select Case vSaveAs
                    Case False
                        Cancel = True
                    Case Else
                        Wb.SaveAs Filename:=vSaveAs
                        Wb.Close False
                        GoTo AppQuit
                End Select
        End Select
        Err.Clear
    ErrHandler:
        If Err.Number <> 0 Then
            MsgBox "Nr.: " & Err.Number & vbLf & Err.Description
        End If
        Err.Clear
        xlApp.DisplayAlerts = True
        xlApp.EnableEvents = True
        Exit Sub
    AppQuit:
        Set vSaveAs = Nothing
        Set Wb = Nothing
        If xlApp.Workbooks.Count = 0 Then xlApp.Quit
        Set xlApp = Nothing
    End Sub
    Quote Originally Posted by tehneXus View Post
    HI,

    I got it to work in an add-in but didn't found a solution to quit the application properly...

    Add-In "ThisWorkbook" module
    Option Explicit
    
    Private MySheetHandler As clsWbEvents
    
    Private Sub Workbook_Open()
        Set MySheetHandler = New clsWbEvents
    End Sub
    Add-In ClassModule called: "clsWbEvents"
    Option Explicit
    
    Public WithEvents xlApp As Application
    
    Private Sub Class_Initialize()
        Set xlApp = Application
    End Sub
    
    Private Sub xlApp_WorkbookBeforeClose(ByVal Wb As Workbook, Cancel As Boolean)
        
        Dim vSaveAs
        On Error GoTo ErrHandler
        
        If Wb.IsAddin Then Exit Sub
        
        xlApp.EnableEvents = False
        xlApp.DisplayAlerts = False
        
        Select Case VBA.MsgBox("Want to save your change to '" & Wb.Name & "' to a file?", vbYesNoCancel + vbExclamation, "Microsoft Excel")
            Case vbCancel
                Cancel = True
                GoTo ErrHandler
            Case vbNo
                Cancel = True
                Wb.Close False
                GoTo AppQuit
            Case vbYes
                vSaveAs = xlApp.Dialogs(xlDialogSaveAs).Show(arg1:=Wb.Name)
                Select Case vSaveAs
                    Case False
                        Cancel = True
                    Case Else
                        Wb.SaveAs Filename:=vSaveAs
                        Wb.Close False
                        GoTo AppQuit
                End Select
        End Select
        Err.Clear
    ErrHandler:
        If Err.Number <> 0 Then
            MsgBox "Nr.: " & Err.Number & vbLf & Err.Description
        End If
        Err.Clear
        xlApp.DisplayAlerts = True
        xlApp.EnableEvents = True
        Exit Sub
    AppQuit:
        Set vSaveAs = Nothing
        Set Wb = Nothing
        If xlApp.Workbooks.Count = 0 Then xlApp.Quit
        Set xlApp = Nothing
    End Sub
    Thank you again tehneXus. It worked very well. When I close the spreadsheet, the application remain opened, but without the error that I mentioned before. In this case, I can manually close the application after closing the spreadsheet.
    One last question: if I change the workbook references in the code to Document, is there possible to create a Word template, with this same code? (sorry for asking it in a Excel forum)

  6. #6
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Force "Save As" in Prompt Before Close - X Button

    For the other issue where a file from a certain folder should be opened as readyonly paste this into
    Add-In ClassModule called: "clsWbEvents"
    Private Sub xlApp_WorkbookOpen(ByVal Wb As Workbook)
        Dim strWbPath$, strWbName$
        If Wb.ReadOnly Then Exit Sub
        If InStr(1, Wb.Path, "support", vbTextCompare) Then
            strWbPath = Wb.Path
            strWbName = Wb.Name
            xlApp.EnableEvents = False
            Wb.Close False
            Workbooks.Open strWbPath & "\" & strWbName, , True
            xlApp.EnableEvents = True
        End If
    End Sub
    Where every file that has "support" in the path will be reopened readonly

    As far as I can say maybe you don't need the beforeclose anymore as you cannot overwrite the file anyway then, and the application quit problem would be solved.

  7. #7
    Valued Forum Contributor tehneXus's Avatar
    Join Date
    04-12-2013
    Location
    Hamburg, Germany
    MS-Off Ver
    Work: MS-Office 2010 32bit @ Win8 32bit / Home: MS-Office 2016 32bit @ Win10 64bit
    Posts
    944

    Re: Force "Save As" in Prompt Before Close - X Button

    One last question: if I change the workbook references in the code to Document, is there possible to create a Word template, with this same code? (sorry for asking it in a Excel forum)
    I have no clue when it's about Word VBA. You can try something similar based on that code.

  8. #8
    Registered User
    Join Date
    05-30-2013
    Location
    São Paulo, Brazil
    MS-Off Ver
    Excel 2010
    Posts
    12

    Re: Force "Save As" in Prompt Before Close - X Button

    I'll try it.
    Thank you.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1