Consulting

Results 1 to 12 of 12

Thread: Retaining the Macro code for disabling Save option in Excel

  1. #1
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    4
    Location

    Retaining the Macro code for disabling Save option in Excel

    Hi,
    I can disable the save option using below code in ThisWorkbook.
    After I close the current excel sheet, the Macro code disappears when I open new excel sheet.
    I tried applying breakpoint, but this is not user friendly.
    Please get back soon for above query.

    Option Explicit
    
    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
        Cancel = True
    End Sub
    Last edited by Aussiebear; 06-18-2025 at 03:32 PM.

  2. #2
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,959
    Open the file while ensuring macros are disabled.
    Write the code (exactly correctly) (best to paste it in from an earlier copy from say, notebook).
    Save and close the file.
    Open the file, allowing macros.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  3. #3
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    4
    Location
    when I open the Excel, macros are disabled by default. I didn't get the option Open the file, allowing macros as you mentioned above.
    The macro won't be there the next time when I open the excel.
    Please let me know.

  4. #4
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    After you paste the code in, when you try and save the workbook what happens?

  5. #5
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,959
    With the spreadsheet active (not the vbe) do alt+t+m+s.
    I get:
    2016-01-15_121750.jpg



    but what do you see?
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  6. #6
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,959
    Another way, which will test whether macros are enabled, code in the right place etc.:
    Put a break point at the first line of the BeforeSave macro (put your cursor on any one of the lines, including the very first line (but it has to be on or before the Cancel = True), and press F9 on the keyboard).
    Save the file. At this point it should stop and highlight the line where it has stopped.
    In the vbe, click the Reset button (a small square). At this point the file should save.
    Remove the break point (or just close the file).
    Test.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  7. #7
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    4
    Location
    I enabled all macros from trust centres. And the adding breakpoint works.
    The files saves as .xlsb and when ever I open excel "Save" option is disabled but "Save As" still allows me to save the file.
    Below is the code I entered in VBA :

    Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


    MsgBox "You can't save this workbook!"
    Cancel = True


    End Sub

  8. #8
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,959
    I don't know why the Save option is disabled on an .xlsb file - have you been playing with disabling that feature elsewhere?
    Do you get the MasgBox coming up?
    A few links you should be able to sort things out with:
    http://stackoverflow.com/questions/1...e-as-using-vba
    http://www.mrexcel.com/forum/excel-q...acro-save.html
    http://answers.microsoft.com/en-us/o...09?db=5&auth=1
    Last edited by p45cal; 01-15-2016 at 09:50 AM. Reason: corrected future to feature
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  9. #9
    VBAX Newbie
    Joined
    Jan 2016
    Posts
    4
    Location
    I won't get any message, but "Save" and "Ctrl+s" options are disabled. I.e it won't allow me to save excel. Only Save As options allows me to save.
    Strange behaviour from Excel.
    Btw may I know any API to call this above MACRO from different tools, to be precise I want to call this MACRO from Visual Studio 2013 or from Qt to disable SAVE option in EXCEL.

  10. #10
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,959
    If you get no msgbox coming up then the code is not being triggered and you are either stopping save etc. in some other way, or the code is not in the right place, that is the Thisworkbook code-module of the workbook you want this to work on.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  11. #11
    VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,709
    Location
    when ever I open excel "Save" option is disabled
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  12. #12
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,453
    Location
    Use Save As rather than Save
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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