none
Set the focus back to the home ribbon RRS feed

  • Question

  • I need to set the focus back to the home ribbon at the end of one of my macros.

    This used to be commandbars in my 2003 code so I tried it using that.  However, it is not setting the focus back to the home ribbon but instead throwing an error.

    Any help is greatly appreciated

    Christine


    Christine Garrett
    Monday, October 31, 2011 6:03 PM

Answers

  • Hi Christine

    This is all making sense, yes :-)

    As far as Word 2007 goes, the only (simple) option you have is to use SendKeys. If you press Alt in Word you'll see letters under all the Ribbon tabs? And "Home" is H in the English language UI. So...

    SendKeys "%H"

    at the end of the macro could work. SendKeys is always a bit "iffy", as it basically emulates user actions. So if the focus were somewhere else other than the Word document window the result could be "unexpected". (This would also work for 2010)

    But any other approach quickly gets more complicated. For Word 2010, if you include a Ribbon customization in the document and macro code for the onLoad attribute of the Ribbon, then you can use an ActivateTabMso method on the Ribbon object to "select" any tab. But this isn't available for Word 2007. (I know, a lot of what I wrote my be gibberish to you, but just wait - it gets worse!)

    The only other possibility is to use the Ribbon Accessibility APIs, which means getting right down into the guts of things. See for example:


    Cindy Meister, VSTO/Word MVP
    Tuesday, November 1, 2011 2:57 PM
    Moderator
  • Hi Christine,
     
    The humour was unintentional. I have litttle good to say about the Ribbon but it really works in a different way from Command Bars and there aren't space-consuming elements that you can show or hide.
     
    If, as a result of your code, Word is changing the visible tab, and there is nothing you can do to stop it, then I can see that it would be good behaviour on your part to want to return the user's Ribbon to the state it was in before you began (not necessarily the Home tab - not necessarily even visible).
     
    I see Cindy has given you links to the relevant information.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Tuesday, November 1, 2011 5:32 PM

All replies

  • Hi Christine

    What you can or cannot do depends very much on which version of Word you're targeting?

    Also, please describe the circumstances under which you'd want to revert focus to the Home ribbon? (What's the macro doing, in general terms?)


    Cindy Meister, VSTO/Word MVP
    Tuesday, November 1, 2011 7:54 AM
    Moderator
  • I need to set the focus back to the home ribbon at the end of one of my macros
     
    Why? One of the many annoying things about the Ribbon is the way that tabs change unexpectedly.
     
    That said, you say you want to change the focus _back_ to the Home tab, implying that you moved it away from there in the first place. Again, I would ask why, but, more to the point, if you did it you know how to do it - just do again what you did to start with.
     
     

    Enjoy,
    Tony
    www.WordArticles.com
    Tuesday, November 1, 2011 9:38 AM
  • So sorry Cindy.  I wrote this letter macro using a merge technique for the list of names in the firm in Word 2003.   At the end of the macro it sets the command bars for mail merge as false.  This does not respond in Word 2007/2010 however.  It simply leaves the user with the mailing ribbon with the focus.  I want to move the focus to the home ribbon instead.  The two lines of code that is in there now which doesn't work (and I tried using "Home") is:

     

    oDoc.MailMerge.MainDocumentType = wdNotAMergeDocument
    CommandBars("Mail Merge").Visible = False

    The NotAMergeDocument does work but the CommandBars do not. 

    Does this make sense?

    Thank you in advance for any help


    Christine Garrett
    Tuesday, November 1, 2011 2:06 PM
  • Tony, you are funny!  My users do complain about this moving around stuff too.  I wrote the letter macro that uses a merge technique for the names in the firm and I wrote it way back in early Word 2003.  All of the code works except the CommandBars command.  The command I am using is below:

     

    oDoc.MailMerge.MainDocumentType = wdNotAMergeDocument
    CommandBars("Mail Merge").Visible = False

    I tried using Home instead of Mail Merge and it throws an error for obvious reasons.  The wdNotAMergeDcoument works fine and it is just the focus issue now.

    Thanks for your humor - it is appreciated.

    Christine


    Christine Garrett
    Tuesday, November 1, 2011 2:09 PM
  • Hi Christine

    This is all making sense, yes :-)

    As far as Word 2007 goes, the only (simple) option you have is to use SendKeys. If you press Alt in Word you'll see letters under all the Ribbon tabs? And "Home" is H in the English language UI. So...

    SendKeys "%H"

    at the end of the macro could work. SendKeys is always a bit "iffy", as it basically emulates user actions. So if the focus were somewhere else other than the Word document window the result could be "unexpected". (This would also work for 2010)

    But any other approach quickly gets more complicated. For Word 2010, if you include a Ribbon customization in the document and macro code for the onLoad attribute of the Ribbon, then you can use an ActivateTabMso method on the Ribbon object to "select" any tab. But this isn't available for Word 2007. (I know, a lot of what I wrote my be gibberish to you, but just wait - it gets worse!)

    The only other possibility is to use the Ribbon Accessibility APIs, which means getting right down into the guts of things. See for example:


    Cindy Meister, VSTO/Word MVP
    Tuesday, November 1, 2011 2:57 PM
    Moderator
  • Hi Christine,
     
    The humour was unintentional. I have litttle good to say about the Ribbon but it really works in a different way from Command Bars and there aren't space-consuming elements that you can show or hide.
     
    If, as a result of your code, Word is changing the visible tab, and there is nothing you can do to stop it, then I can see that it would be good behaviour on your part to want to return the user's Ribbon to the state it was in before you began (not necessarily the Home tab - not necessarily even visible).
     
    I see Cindy has given you links to the relevant information.
     

    Enjoy,
    Tony
    www.WordArticles.com
    Tuesday, November 1, 2011 5:32 PM
  • Cindy,

    You were right on the money when you said send keys might not work.  It doesn't do a thing.  No errors are pulled but nothing happens.  You would think it would be easier to do.  Tony's information is great for someone who does VBA a bit but I am not one of them.  My client will have to live with it, I guess.  I can't find an answer to this at all.  Too bad this has become so hard to do now. 


    Christine Garrett
    Monday, November 7, 2011 5:00 PM
  • Hi Christine

    Usually, something should happen, although perhaps not what you expect. Some things to check

    1. Was the macro running with focus in the Word document? If you're testing in VBA debug mode, it won't work...

    2. Have you checked what happens in UI (no macros running) if Alt+H is pressed? Perhaps that's not the right keyboard combination for the client machine.

    3. What else is the macro doing? Perhaps SendKeys needs to be moved in the code.


    Cindy Meister, VSTO/Word MVP
    Tuesday, November 8, 2011 11:19 AM
    Moderator