+ Reply to Thread
Results 1 to 3 of 3

Code for "SAVE-AS" works good but not perfect

Hybrid View

  1. #1
    Registered User
    Join Date
    09-10-2014
    Location
    Netherlands
    MS-Off Ver
    2007 on Win 7
    Posts
    50

    Code for "SAVE-AS" works good but not perfect

    Hello all.

    I hope that somebody can help me with this :

    I have a code (received through this forum) in my template and it looks like this :

    
    'Action makes sure the user saves as XLSM file type.
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      Dim FileNameVal As String
    
        FileNameVal = Sheets("Gegevens").Range("E31").Value '& ".xlsm"
    
        Application.DisplayAlerts = False
    
        ThisWorkbook.SaveAs Filename:=FileNameVal, FileFormat:=52
        
        Application.DisplayAlerts = True
        
    End Sub
    When the user clicks on the built in excel method "save-as", the code gets the generated filename from a cell (cell E31 on tab "Gegevens")
    and makes sure the template is being saved as XLSM.
    This works very good.

    Little problem is that when the user doesn't choose "save-as" but "save" (or the little disk-icoon in excel to save) the whole sheet freezes and Excel stops working.
    Also when the user clicks the "X" (right-upper corner) to end the sheet (and normally asks to save the sheet) it goes wrong.
    Only way to work further is close Excel the hard way and hope that the just filled in details (and that can be a lot of details ...) can be restored again.

    My question :
    Is there a way to make upper mentioned code working perfect by building something in so the "save" function in Excell can not be used and only the "save-as" function is possible ?

    In advance .... many thanks for your help.

  2. #2
    Registered User
    Join Date
    09-10-2014
    Location
    Netherlands
    MS-Off Ver
    2007 on Win 7
    Posts
    50

    Re: Code for "SAVE-AS" works good but not perfect

    Can anybody please please please help me in this ?

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Code for "SAVE-AS" works good but not perfect

    Maybe ...

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
      Cancel = True
      On Error GoTo Oops
    
      With Application
        .EnableEvents = False
        .DisplayAlerts = False
        Me.SaveAs Filename:=Worksheets("Gegevens").Range("E31").Value, _
                  FileFormat:=xlOpenXMLWorkbookMacroEnabled
        MsgBox "Saved as " & Me.FullName
    Oops:
        .DisplayAlerts = True
        .EnableEvents = True
      End With
    End Sub
    Entia non sunt multiplicanda sine necessitate

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Receiving "enable content" warning after executing "save as" code
    By phbryan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-11-2014, 12:02 PM
  2. Code only runs when a user hits "Save", not "Save As"
    By cjstewart8 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-04-2014, 07:41 PM
  3. [SOLVED] pointers on a vba code that works like a "mail merge" to keep it all in excel
    By Iexcelatblanc in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-09-2013, 12:19 PM
  4. create links to check boxes marked "good" fair"and "bad"
    By pjb in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-19-2006, 09:25 PM

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