+ Reply to Thread
Results 1 to 5 of 5

WorkbookActivate Event stopping with "code interrupted" message after Alt-Tab

Hybrid View

  1. #1
    Registered User
    Join Date
    09-02-2009
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    77

    WorkbookActivate Event stopping with "code interrupted" message after Alt-Tab

    Hi all,

    Strange one this. I have the following code built in to one of my add-ins, which enables or disables certain add-in functionality based on the currently active workbook.

    Private WithEvents App As Application
    
    Private Sub App_WorkbookActivate(ByVal Wb As Workbook)
    
        If Wb.CodeName = "wbkWTDC" Then
            App.OnKey "%p", "showPrevData"
            Toolbar.setState True
        Else
            App.OnKey "%p"
            Toolbar.setState False
        End If
            
    End Sub
    I have no problem with the code itself - it works fine.

    However... sometimes I accidentally press Alt-Tab instead of Ctrl-Tab when I want to cycle through open workbooks. When this happens I press the Escape key before letting go of the Alt key to abandon the Alt-Tab process and stay in Excel. At this point, if I then immediately press Ctrl+Tab - as I had originally intended - I get the "Code execution has been interrupted: Continue/End/Debug/Cancel" dialogue box coming up.

    Anyone know why this happens? It's obviously something to do with the state Excel is left in after the abandoned Alt-Tab action, or perhaps something to do with the "Escape" keypress remaining in some buffer? I don't know. At the moment my add-in is just in development, so it's just a nuisance to me, but I don't want this still going on after I release it.

    I have tried Application.EnableCancelKey = xlDisabled, however the break occurs before that property has a chance to be set (i.e. on the "Private Sub App_WorkbookActivate(ByVal Wb As Workbook)" line) and the property gets reset back to xlInterrupt when the code completes and Excel returns to the idle state.

  2. #2
    Forum Expert JapanDave's Avatar
    Join Date
    06-10-2008
    Location
    The grid, I got in!
    MS-Off Ver
    Excel 2010/13
    Posts
    1,696

    Re: WorkbookActivate Event stopping with "code interrupted" message after Alt-Tab

    Wouldn't pressing the Esc key be the culprit?
    Be fore warned, I regularly post drunk. So don't take offence (too much) to what I say.
    I am the real 'Napster'
    The Grid. A digital frontier. I tried to picture clusters of information as they moved through the computer. What did they look like? Ships? motorcycles? Were the circuits like freeways? I kept dreaming of a world I thought I'd never see. And then, one day...

    If you receive help please give thanks. Click the * in the bottom left hand corner.

    snb's VBA Help Files

  3. #3
    Registered User
    Join Date
    09-02-2009
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    77

    Re: WorkbookActivate Event stopping with "code interrupted" message after Alt-Tab

    Yes, I think so - although the keypress itself should be cleared BEFORE the code runs, as the process is:

    1) Press Alt-Tab
    2) Windows task switcher dialogue appears
    3) Realise I didn't want to use it, press Escape
    4) Windows task switcher disappears, focus returns to Excel
    5) Wait any length of time (seconds, minutes, hours, it makes no difference)
    6) Press Ctrl-Tab as originally intended - at this point my App_WorkbookActivate event fires, code execution commences
    7) VBA "Code execution interrupted" dialogue appears

    Further info:

    It may or may not be relevant, but I have the "Show windows in taskbar" setting disabled.

    Also, if I do something in Excel, like changing a cell value, between steps 5 an 6 (i.e. before pressing Ctrl-Tab) then the problem does not occur.
    Last edited by blackworx; 03-02-2012 at 05:21 AM.

  4. #4
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: WorkbookActivate Event stopping with "code interrupted" message after Alt-Tab

    You say you tried setting the Application.EnableCancelKey = xlDisabled. Does it work if you set this as the add-in is opened (ie and don't re-enable it)?

  5. #5
    Registered User
    Join Date
    09-02-2009
    Location
    Edinburgh, Scotland
    MS-Off Ver
    Excel 2007
    Posts
    77

    Re: WorkbookActivate Event stopping with "code interrupted" message after Alt-Tab

    Quote Originally Posted by Firefly2012 View Post
    You say you tried setting the Application.EnableCancelKey = xlDisabled. Does it work if you set this as the add-in is opened (ie and don't re-enable it)?
    Hadn't previously tried this, but I have now tried and unfortunately it doesn't work. According to the VBA help file, this property gets reset to xlInterrupt whenever code execution completes, which in this case is as soon as the Workbook_Open event finishes.

+ 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