none
Late binding controls for ribbon callbacks (Word 2007) RRS feed

  • Question

  • Hi,

    I'm developing a template in VBA that must run on Word 2003 and up (excepting 2008). I've found that including early bound iRibbonUI objects in my code causes compile errors in Word 2003, even though Word 2003 never touches the module where these live. So, for instance, this isn't possible:

    Sub getLabelforRibbon(control As iRibbonControl, ByRef returnedval)
        Select Case control.id
             Case "id1"
                 returnedval = "First"
             Case "id2"
                 returnedval = "Second"
        End Select
    End Sub
    

    The solution was, I thought, to use late binding:

    Sub getLabelforRibbon(control As Object, ByRef returnedval)
        Select Case control.id
             Case "id1"
                 returnedval = "First"
             Case "id2"
                 returnedval = "Second"
        End Select
    End Sub
    

    but I've discovered that this raises an "Object required" error in Word 2007 (works fine in 2010 and 2013). The control object is never set. 

    Currently the only way I can think of around this (other than removing all callbacks and losing dynamism) is to manually remove the module that contains my callbacks from the .dot version of the template (I create the .dotm, then save it to .dot for transmission to Word 2003 and 2004 users). That's not preferable, as the desire is for the final product to be, in essence, one template for all versions. If it's the only solution, I'll take it, but I'm wondering if anyone has found any other ways around this? 

    Thanks!

    Thursday, October 10, 2013 10:05 PM

All replies

  • Hi,
    As far as I know, Ribbon is introduced since Office 2007. So I don’t think you could use Ribbon feature in Office 2003, and what’s meaning of the word 2004 and 2008?
    The onAction callback function has a specific signature like below.

    Sub MyMacro(ByVal control as IRibbonControl)
    End Sub


    I don’t recommend you changing the IRibbonControl class to Object,
    About the “Object required” error: the error is caused when you assign the Object value to a Variant variable. You can try to use the Set statement when assigning an object reference.
    Modify your code like this.

    Sub getLabelforRibbon(control As Object, ByRef returnedval)
    Dim IRCtl As IRibbonControl
    Set IRCtl = control
        Select Case IRCtl.id
             Case "id1"
                 returnedval = "First"
             Case "id2"
                 returnedval = "Second"
        End Select
    End Sub


    Please refer to the following links to get more information.

    # Customizing the 2007 Office Fluent Ribbon
    http://msdn.microsoft.com/en-us/library/aa338202(v=office.12).aspx
    # Object required (Error 424)
    http://msdn.microsoft.com/en-us/library/office/gg251554.aspx

    Regards &Thanks,


    • Edited by Mercop002 Monday, October 14, 2013 3:02 AM
    Monday, October 14, 2013 3:00 AM
  • Hi cchimi

    When do these errors occur? When loading the template, or when code is called in the template?

    Do you use any kind of distribuation / installation process for this template, or is it a "the user copies it to folder X" type of distribution? (As in: could you perform any (code) actions during installation)


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, October 14, 2013 6:34 AM
    Moderator
  • Mercop, thanks for the response. I'm not trying to use ribbon functionality in pre-2007 versions of Word, I just don't want to have to change the code. I tried your fix but Word 2007 never sets the control object so there's nothing to set IRCtl to. Word 2004 and Word 2008 are Mac versions of Word (2008 is moot here as it doesn't include VBA).

    Cindy, in Word 2007 I get errors as soon as the template loads (so, if macros need to be enabled, right after I click Enable). I actually can't recreate the problem in Word 2003, so now I'm thinking I might have mistyped and meant 2004 (so many versions to debug, it gets confusing). I can't test on 2004 right now, so I'm not positive when the error crops up. I will check.

    I'm actually not creating the installer, but I can definitely make recommendations if there's something that might help. At the end of the day I suppose it's only the difference of one step (opening the .dot file and removing the module); I was just hoping that I could keep it to one step to avoid any confusion down the line if I'm not the one managing the code.

    Monday, October 14, 2013 5:14 PM
  • Hi cchimi

    Initially, I was going to await the results of your version test, but I fear I may lose my train of thought if I do, so...

    If the error didn't trigger so soon, it might have been possible to use the VBE extensibilty library to add/remove the module in question. But it appears this is not a possibility.

    The other idea I had was, if this were a 2007 file format (which of course it is not, but yesterday that hadn't occurred to me) that the VBA project could be exchanged in the Office Open XML file before installation, based on the version of Word available.

    I'm not sure I'd want an installer doing this inside the Word application - rather not!

    Which would bring us back to two files: a *.doc and a *.docm. Or possibly three files if updating the code is the concern. It would be possible to have "code stubs" in the *.doc/*.docm and a third *.doc file as a "code library" with the common code...?


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, October 14, 2013 6:17 PM
    Moderator