+ Reply to Thread
Results 1 to 10 of 10

Running VBA macro when workbook opens but only AFTER the ribbon and custom tab has loaded.

  1. #1
    Registered User
    Join Date
    10-14-2013
    Location
    canada
    MS-Off Ver
    Excel 2010
    Posts
    4

    Running VBA macro when workbook opens but only AFTER the ribbon and custom tab has loaded.

    Hi,

    So this is a symptom of a very common issue with a program/add-in called IBM Cognos TM1. Basically it installs and it's an add-in for Excel which allows you to pull data into your sheets.

    The issue is that when the extra tab initializes/loads it turns off the numlock on the computer. This is a known bug that they've tried to fix in newer versions and yet it still happens. The cause is believed to be multiple send key commands within the add-in coding which disables the numlock, which is also a common Excel bug in 2007/10.

    So, I've tried to fix this issue but I need a little bit more help.

    I inserted some VBA code into the workbooks_open event to get Excel to turn the numlock on whenever the specific file opens, and this works great in blank files with no add-ins; however, when it is used with the actual intended file with the TM1 add-in enabled, it doesn't work.

    The reason is that the custom tab from TM1 which ends up disabling the numlock, only loads once all the workbooks_open code has been executed. If I attempt to delay or sleep and then have the numlock command run, this doesn't work either; the custom tab's load is also delayed until the workbooks_open code has run and then loads, turning the numlock back off.

    So basically any code in the workbooks_open event is undone because the custom tab which causes the numlock to turn off, always loads after that code and causes the issue.

    Unfortunately, the add-in is password protected so I can't access it's code to fix the actual cause.


    So here's my question: Is there a workbook event which I can use or something I can do to make a specific macro or piece of code run when a workbook is opened but only AFTER the ribbon has fully loaded with any custom tabs?

    Thanks for the help,

    Richard

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,431

    Re: Running VBA macro when workbook opens but only AFTER the ribbon and custom tab has loa

    What about using the OnTime method to run a macro in a few seconds, once you workbook_open event fires. This should then give the other code time to execute before restoring numlock.
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    10-14-2013
    Location
    canada
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Running VBA macro when workbook opens but only AFTER the ribbon and custom tab has loa

    I tried that but then the custom tab also loads later, as if its waiting for all workbook_open code execution to be finished.

  4. #4
    Registered User
    Join Date
    09-28-2012
    Location
    Earth
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Running VBA macro when workbook opens but only AFTER the ribbon and custom tab has loa

    Please Login or Register  to view this content.
    Put that in a module or the workbook and call runUntilNumLockOff() from your onload. It will check once every second to see if the numlock key is off. When it turns off it will turn it back on once and then stop running until it is called again. If for some reason the sendkey doesn't turn on the numlock it will try again 1 second later until it does. Also, you can change how many times it will try max. Above I have it set to 120, which is 2 minutes.


    edit: if this really doesn't work because it is waiting on the code to stop or whatever you can try calling it from a worksheet selection change event instead. Just check for the state of the numlock key every time you change selections and force it back on if it is off. Then the user acts as a kind of timer every time they click a new cell.
    Last edited by slomer; 07-11-2014 at 01:24 PM.

  5. #5
    Registered User
    Join Date
    10-14-2013
    Location
    canada
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Running VBA macro when workbook opens but only AFTER the ribbon and custom tab has loa

    So I just realized another problem now. Thanks for the code it works great except in this case for a reason I'm not sure I can solve. Apparently, if you click on any cell before the custom tab has finished loading, then the getkeystate function reverses the stated state of the key. So for example, if I let it fully load, then it turns the numlock on. If I pre-empt it finishing and click on a cell, then when it gets the key state, it says the key is off even when it is actually on and so while it keeps trying to turn it on, it actually turns it off. I hope this makes sense although the actual thing happening makes no sense to me. Is there a way to reset the keyboard state? If I leave excel and then open the file again and give it time to load then it won't give me any problems.

    Richard

  6. #6
    Registered User
    Join Date
    09-28-2012
    Location
    Earth
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Running VBA macro when workbook opens but only AFTER the ribbon and custom tab has loa

    That is weird. I have no answer for that since I can't test it. Perhaps delay the initial on launch call for a little while. You could test if the addon is loaded and send an assumptive numlock press instead of actually testing the key state. The other thing that might work would would be a really dirty hack. You could have it select a cell you know is blank, sendkey a specific numpad key, and test if the cell value is the number expected or not. There are cleaner ways to send a keypress than sendkey but it should work.

  7. #7
    Registered User
    Join Date
    09-28-2012
    Location
    Earth
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Running VBA macro when workbook opens but only AFTER the ribbon and custom tab has loa

    Actually.. maybe it is counting a pressed down state for some weird reason. Try changing the function to the following one.

    Please Login or Register  to view this content.
    This will only return true if it is both on and NOT being held down.

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,431

    Re: Running VBA macro when workbook opens but only AFTER the ribbon and custom tab has loa

    Try increasing the delay on the OnTime method to confirm the 2 things are not clashing

  9. #9
    Registered User
    Join Date
    10-14-2013
    Location
    canada
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: Running VBA macro when workbook opens but only AFTER the ribbon and custom tab has loa

    Hey guys,

    So I tried a number of things and different methods which did not work. I downloaded a 3rd party program that allowed me to open the TM1 add-in without the password so I could look at the code. The issue was as I thought that several sendkeys were used resulting in turning off the numlock. Also, unfortunately, the last sendkey also included a wait command which pause any and all additional vba coding and thus defeated the ontime command as well because it seemed to stop the timer (I used debug.print and a counter to see the loops happening after each second and they would stop mid way until you click somewhere and then would restart).

    So anyway, my final solution was this:
    Using the 3rd party software to temporarily disable the password protection, I went in, found the sendkeys commands, took out the 'True' which told it to wait for input and then added a sendkey "{numlock}" at the end so that it enabled the numlock after all those sendkeys. Now it works great, which is hilarious that one line of code has fixed an issue with this software that people have been complaining of for over a year at different companies and forums.

    The only caveat is that I have to get permission to push this version of the add-in file to all users that have it installed and into their install directories (it relies on other files within that directory), so we'll see if IT will let me do this :P.

    Thanks for all your help!

  10. #10
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,431

    Re: Running VBA macro when workbook opens but only AFTER the ribbon and custom tab has loa

    hacking the addin aside, why not report the problem to the 3rd party vendor and get it fixed at source?

+ 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. [SOLVED] Activate custom Ribbon Tab on Workbook Open
    By jewelsharma in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 03-20-2014, 09:10 PM
  2. How to create new workbook with custom ribbon in VBA
    By MichalJB in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 11-25-2013, 02:52 PM
  3. Slow opening for workbook with custom ribbon.
    By ericsastud in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-22-2011, 11:09 AM
  4. Fire macro in add-in from custom ribbon
    By 65bit in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-17-2010, 12:30 AM
  5. Assigning a macro to a custom button in 2007 ribbon
    By dcgrove in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-14-2009, 05:50 AM

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