+ Reply to Thread
Results 1 to 5 of 5

BeforeSave code makes excel crash every time

  1. #1
    Registered User
    Join Date
    08-02-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    3

    BeforeSave code makes excel crash every time

    I'm trying to prevent someone from saving the file I've created and destroying the original, so here is the code I put in the ThisWorkbook sheet.

    Please Login or Register  to view this content.
    Every time I try savnig the file though when the name does include KCALC, it always needs to close, then recovers the document with the correct name, so it is accomplishing its purpose, but I'd like to do that without forcing the window to crash and recover each time someone saves the file for the first time.

    Also, while I'm on here, if anyone knows how to prevent certain macros from affecting other documents that you don't have the password to I'd appreciate it. There is a "software" we use that has a lot of macros built in that makes using excel a pain, and even when the file isn't open, the macros still interfere, but I can't edit the macros or get rid of them because I don't have a password for them. If there is a way around that I would appreciate suggestions.

    Thanks for any help!

    *I should also probably add I'm very new to using VBA...just kind of learning as I go...
    Last edited by keb; 08-03-2011 at 05:43 PM.

  2. #2
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,255

    Re: BeforeSave code makes excel crash every time

    Welcome to the forum.

    The event handler you're using is Workbook_BeforeSave. This will be called every time you try to save the workbook. Inside the event handler, you have this code which tries to save the workbook:
    Please Login or Register  to view this content.
    So you can see that this will cause the procedure to call itself and you'll end up in an unintended loop:

    Please Login or Register  to view this content.
    To avoid this loop, you need to temporarily disable application events by setting Application.EnableEvents to False. The key to this is you must explicitly set it back to True again, even if an error occurs. Taking this into account, I think your code would look something like this:
    Please Login or Register  to view this content.
    However, I'm not keen on the solution for a couple of reasons. The main reason is that if the user disables macros then they'll be able to save over the file without interference, so it's not very robust. Wouldn't it be better to password protect the workbook, giving the relevant people read-only access?
    Last edited by Colin Legg; 08-03-2011 at 06:55 PM. Reason: OP added code tags
    Hope that helps,

    Colin

    RAD Excel Blog

  3. #3
    Registered User
    Join Date
    08-02-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: BeforeSave code makes excel crash every time

    I will try putting that in. I want the person to be able to edit the document, but I don't want them to be able to destroy the original copy by saving over the original file without saving it under a different name first (preferably the value in A5). Is there an easier way to do that?

  4. #4
    Forum Expert Colin Legg's Avatar
    Join Date
    03-30-2008
    Location
    UK
    MS-Off Ver
    365
    Posts
    1,255

    Re: BeforeSave code makes excel crash every time

    Yes, protect the workbook with a "Password To Modify" (aka read-only) password.

    Tools > Options > Security Tab > Password To Modify

    This means that people will only be able to open the workbook as read-only (unless they know the password) and if they want to save their changes, they will have to save the file with a different name.

  5. #5
    Registered User
    Join Date
    08-02-2011
    Location
    Chicago, IL
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: BeforeSave code makes excel crash every time

    Quote Originally Posted by Colin Legg View Post
    Yes, protect the workbook with a "Password To Modify" (aka read-only) password.

    Tools > Options > Security Tab > Password To Modify

    This means that people will only be able to open the workbook as read-only (unless they know the password) and if they want to save their changes, they will have to save the file with a different name.
    Perfect! Thanks for the help!

+ 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