+ Reply to Thread
Results 1 to 33 of 33

Disable buttons in custom ribbon before opening a vbModeless UserForm

Hybrid View

  1. #1
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Disable buttons in custom ribbon before opening a vbModeless UserForm

    Hello everyone,

    I have a custom ribbon, in which one button triggers opening a UserForm, which is not modal.
    Then I would like to disable certain other buttons in my ribbon before the UserForm is shown.

    I have the following code:
    Call ribbonRefresh
    Call UserFormFoo.Show(vbModeless)
    "ribbonRefresh" does call
    ribbon.Invalidate
    to update the ribbon. Also, I have the necessary "GetEnabled" sub in my ribbon handling module (this works as expected).
    But the buttons in the ribbon do not get disabled when the code is run.

    However, if I set a breakpoint in my "GetEnabled" sub the buttons get disabled.

    Any help would be appreciated.
    Thanks in advance.

  2. #2
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    BTW, it doesn't make any difference if the UserForm is opened with vbModeless or not.
    However, if I comment out the part
    Call UserFormFoo.Show(vbModeless)
    the ribbon state is correctly updated, i.e. buttons are disabled.

  3. #3
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Perhaps the display of the user form interrupts the ribbon update process. You go try inserting a DoEvents in between the ribbon update instruction and the display of the form, or call the RibbonRefresh from within the form_initialise routine. Just guessing.
    If you like my contribution click the star icon!

  4. #4
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Unfortunately, neither adding a DoEvents nor moving the ribbonRefresh to UserForm_Initialize or UserForm_Activate did the trick

  5. #5
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    BTW, it doesn't make any difference if the UserForm is opened with vbModeless or not.
    However, if I comment out the part
    Call UserFormFoo.Show(vbModeless)
    the ribbon state is correctly updated, i.e. buttons are disabled.

  6. #6
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    I put a breakpoint in my ribbon's GetEnabled sub. And it seems it is simply not called at all.

  7. #7
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    But is does get triggered when you close the form, or is the event lost altogether?

  8. #8
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Yes, GetEnabled is triggered if I close the form.

  9. #9
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    The only problem I can find on GOOGLE, refers to a form being displayed as modal. I know you stated that you displayed the form modeless. What about the actual form properties, does that say modal=false, if not then can you try that one as well?

  10. #10
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Set "ShowModal" property on "UserFormFoo" to "False". No Change

  11. #11
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    No idea, what happens if you call the ribbon_routine and then query the status of the button you have just disabled (all before displaying the form). Would that perhaps force the update of the ribbon?

  12. #12
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Ok. I tried, but I am out of ideas/options,unless you have set application.screenupdating=false somewhere

    You need someone smarter than me to solve this problem.

  13. #13
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Quote Originally Posted by OllieB View Post
    Ok. I tried, but I am out of ideas/options,unless you have set application.screenupdating=false somewhere

    You need someone smarter than me to solve this problem.
    I don't think so but I'll check again for application.screenupdating=false
    Thanks for your efforts.

  14. #14
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    PS. also check for Application.EnableEvents= False when you are at it.

  15. #15
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Quote Originally Posted by OllieB View Post
    PS. also check for Application.EnableEvents= False when you are at it.
    Ok, there's neither an Application.ScreenUpdating=False nor Application.EnableEvents=False anywhere in the code.
    However, the problem seems to be that I start a kind of infinite loop after initializing the UserForm:
    Do While
    Loop
    This is because I'm waiting for some events from an external system, whose result is to be displayed in the UserForm.

    The code looks like this now:

    Call UserFormFoo.Show(vbModeless) ' UserForm_Activate contains the call to ribbonRefresh, as well as Application.ScreenUpdating = True and DoEvents
    Call UserFormFoo.startLoop        ' this is where the "infinite loop" starts
    If I comment out the loop in startLoop, the ribbon gets updated as desired.

    Any ideas how to improve this would be welcome.

  16. #16
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    perhaps have a separate routine to load the userform and call it using ontime so the button code would be
    Call ribbonRefresh
    application.ontime now(), "macro_to_load_form"
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  17. #17
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Nope, didn't work.

  18. #18
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Ok, simple. In the DoWhile loop include a DoEvents statement. I think the ribbon update is not getting a chance to occur, because you are looping in this manner. You could also all a sleep in the DoWhile loop again to allow your PC to catch up.

    I would code (in this case as I do not believe in that sort of loop

    '# declare this at the top of the module
        Public Declare Sub Sleep Lib "Kernel32" (ByVal pMilliseconds As Long)
    
    '# this goed into the code
    Do while
        DoEvents
        Sleep(100)
    Loop

  19. #19
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Yeah, the loop already has a DoEvents and a Sleep...

    BTW, I'm not a VBA expert and this loop seemed to be the only way to get what I wanted. If you have any suggestions for improvement, I'm all ears.

  20. #20
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    how does the getenabled callback determine that the controls should be disabled?

  21. #21
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    RibbonRefresh adds the button name to a collection. GetEnabled checks whether control.tag is in that collection.

  22. #22
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    I think the problem is definitely in the loop part, in which (userform) routine is the loop executed, and what is coded inside the loop?

  23. #23
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    The loop is now in its own sub in UserFormFoo.
    Within the loop I ask a custom dll for some result values (the details of which I am not allowed to reveal). If a result exists, these values are then displayed in the UserForm.

  24. #24
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    - From which UserForm routine do you call this sub?
    - Is the call Asynchronious or Synchronious? (in other words does the dll called immediately return back a value, or wait for the value to become available)

  25. #25
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    the sub is called from the callback for the ribbon button that starts UserFormFoo.

    Call UserFormFoo.Show(vbModeless)
    Call UserFormFoo.startLoop
    the dll call is synchronous

  26. #26
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Could you try starting the loop via a button?

  27. #27
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Quote Originally Posted by OllieB View Post
    Could you try starting the loop via a button?
    I tried just starting the loop from the button, with the same result: Ribbon state does not get updated.

    As I said before, the problem must be somewhere within the loop because the ribbon state gets updated if I comment out the loop...

    However, I'm so sick of stupid VBA now that I'm thinking of just leaving all buttons enabled and add a guard to their callbacks that checks if the UserFormFoo is currently running.
    Not as nice but I hope that will work.

  28. #28
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    what was the code you used with the ontime method-were you still calling the startloop directly?

  29. #29
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Quote Originally Posted by JosephP View Post
    what was the code you used with the ontime method-were you still calling the startloop directly?
    I tried:
    Application.OnTime Now(), "theloop"
    
    sub theloop()
        Call UserFormFoo.startLoop
    end sub

  30. #30
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    did you try using ontime to call a routine that both shows the form and starts the loop? that way the button onAction callback can finish running before you load the form and start the loop

  31. #31
    Registered User
    Join Date
    04-06-2009
    Location
    Germany
    MS-Off Ver
    2010
    Posts
    18

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    Quote Originally Posted by JosephP View Post
    did you try using ontime to call a routine that both shows the form and starts the loop? that way the button onAction callback can finish running before you load the form and start the loop
    Holy ****. That seems to be working. Thanks a lot!

  32. #32
    Forum Expert OllieB's Avatar
    Join Date
    12-20-2012
    Location
    Netherlands
    MS-Off Ver
    Excel 2007 (home) & 2010 (office)
    Posts
    1,542

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    So you are not starting the loop from within the form?

  33. #33
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Disable buttons in custom ribbon before opening a vbModeless UserForm

    you're welcome :-)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

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