+ Reply to Thread
Results 1 to 11 of 11

Prevent closing of one workbook when two are open

  1. #1
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Prevent closing of one workbook when two are open

    Hi,

    I'm trying to prevent a user from closing a perticular workbook.

    Here is some important factors.

    Workbook(A) = "LA-01-04-10" This is only one of 52 other but only one is open at a time.

    Workbook(B) = "LA.xls" Always the same name and is always opened when one of the workbooks(A) above it opened.


    When workbook (A) is opened it will then open another workbook (B) at that time the window is hidden workbook(B) So the user can only see workbook (A) I have a button on workbook(A) that unhides the window to show workbook(B) After the user completes his work in Workbook (B) they need to click another button that sorts the list and returns them to workbook(A) (Which also hides the window for workbook(B).

    The problem is I have users that are trying to close workbook(B) instead of pressing the sort list button. So what I want is to prevent the user from closing workbook(B) by displaying a msgbox saying they need to press the button.

    Ive tried several approches to this but I'm still having problems. The below script works if workbook(B) is active or shown. The message is displayed and the workbook will not close. However if the user is in workbook (A) and trys to close the message is displayed and both workbooks close like they are suppose to. But why is the message displayed?

    One other note I should say is that not only does workbook(A)Open workbook (B) but it also closes it. Which may be why I'm having problems?

    I have also included both workbooks so you can see all the code.

    Thanks for any help or advice... Mike


    I have this code in Thisworkbook of workbook(B)

    Please Login or Register  to view this content.
    Attached Files Attached Files
    Last edited by realniceguy5000; 10-02-2009 at 11:45 AM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Prevent closing of one workbook when two are open

    Hello Realniceguy5000,

    This is a very strange problem. I have tried many different approaches as well but have not been able to stop the message box from been displayed when both files close. Even setting flags in the workbooks has failed. Apparently, it has to do with how Excel closes the workbooks. Exactly what is happening and what sequence of events takes place, I can't say. Is it a bug, a snafu, or normal behaviour? It will take more research to get an answer.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

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

    Re: Prevent closing of one workbook when two are open

    Please Login or Register  to view this content.
    Go, RNG!

    The workbook close event fires when the workbook the code is in is about to close, so there's no point in checking activeworkbook.name to see who's closing; it's Me.Name
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Prevent closing of one workbook when two are open

    Hello Realniceguy,

    Persistence pays off. I found a Microsoft Knowledge Base article that details why this happens.

    You cannot use the Workbook_BeforeClose event to close another Excel workbook


    Additional Information:

    BeforeClose Event

    Occurs before the project closes. If the project has changed since it was opened, but has not been saved, this event occurs before the user is prompted to save the project.

    Syntax

    Private Sub Project_BeforeClose(ByVal pj As MSProject.Project)

    pj The project that is being closed.

    Remarks

    Project events do not occur when the project is embedded in another document or application.
    Last edited by Leith Ross; 09-24-2009 at 12:44 AM. Reason: Added additional information

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

    Re: Prevent closing of one workbook when two are open

    Microsoft Project, Leith?

  6. #6
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Prevent closing of one workbook when two are open

    Shg...

    PHP Code: 
    Code:
    Option ExplicitGoRNG!

    The workbook close event fires when the workbook the code is in is about to closeso there's no point in checking activeworkbook.name to see who's closingit's Me.Name 
    I had thought I went though the workbooks and added the option explicit

    Anyway I have now completed adding this in. I now understand that checking the activeworkbook is useless since the code is being ran in that workbook.

    Thank You,
    Hmmm....

    Anyway

    Leith, Thanks for spending the time to look into this...

    However:

    Quote Originally Posted by Leith Ross View Post
    Hello Realniceguy,

    Persistence pays off. I found a Microsoft Knowledge Base article that details why this happens.

    You cannot use the Workbook_BeforeClose event to close another Excel workbook


    Additional Information:

    BeforeClose Event

    Occurs before the project closes. If the project has changed since it was opened, but has not been saved, this event occurs before the user is prompted to save the project.

    Syntax

    Private Sub Project_BeforeClose(ByVal pj As MSProject.Project)

    pj The project that is being closed.

    Remarks

    Project events do not occur when the project is embedded in another document or application.

    I understand now why the message repeats when trying to close. But what I don't understand is what you are saying here.

    PHP Code: 
    [B]Syntax[/B]

    [
    B][COLOR="Blue"]Private Sub Project_BeforeClose(ByVal [I]pj[/I] As MSProject.Project)[/COLOR][/B]

    [
    I]pj[/I]   The project that is being closed.

    [
    B]Remarks[/B
    Are you saying that this is another way of doing the procedure?

    or it just isn't gonna happen?

    Do you guys have any other suggestions on something else that may work?

    Thanks again for your time. Mike...

    You Guys are always a great help!!!!

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

    Re: Prevent closing of one workbook when two are open

    I had thought I went though the workbooks and added the option explicit
    You did. That's what the attaboy was for.

  8. #8
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Prevent closing of one workbook when two are open

    Quote Originally Posted by shg View Post
    You did. That's what the attaboy was for.
    Must have missed the attaboy part...lol Anyway it only took a year or so to sink in...


  9. #9
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Prevent closing of one workbook when two are open

    Good Morning,

    Cross posted for additional help or advice at the following:

    http://www.mrexcel.com/forum/showthr...33#post2076833

    Thank You, Mike

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Prevent closing of one workbook when two are open

    Hello Mike,

    A few months back, I wrote a macro to close other instances of Excel from VBA and decided I needed code to close workbooks within Excel. Your problem pushed me into reviving that project. This macro takes the name of the workbook to closed and closes it. If the workbook has been saved then the extension is included, but if it has been saved yet then don't add the extension to the name. Copy this code into a separate VBA module.
    Please Login or Register  to view this content.
    Example
    This closed a workbook I had open. Change the name of the workbook to one you want to close.
    Please Login or Register  to view this content.

  11. #11
    Valued Forum Contributor realniceguy5000's Avatar
    Join Date
    03-20-2008
    Location
    Fl
    MS-Off Ver
    Excel 2003 & 2010
    Posts
    951

    Re: Prevent closing of one workbook when two are open

    Thank You Leith,

    I'm not sure how you pulled that off but it works like a charm... Great Work...

    Thanks Again: Mike

    For anyone intersted:

    In WorkbookA:
    Thisworkbook mod
    Please Login or Register  to view this content.
    In MOD 1 Leiths Function Code
    In MOD 2 Leiths Sub Testit

    In Workbook B
    Thisworkbook
    Please Login or Register  to view this content.
    Once again Big Thanks to Leith!!!!!!!!!

    Mike
    Last edited by shg; 10-02-2009 at 11:49 AM. Reason: deleted spurious quote

+ 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