none
How can I add a Button to a Word 2007 Document ? RRS feed

  • Question

  • I have searched high & low, I have tried loads of different ideas and code sampes that I have found via Google (including on this site), but after 2 solid days of getting nowhere, it's time to ask ...

    I execute a VB.net program, created in VS2010, that opens a Word document, if it exists, and creates it from a Skeleton version if it doesn't exist, that Word Document is then Opened for Editing & I do whatever editing I need to do ... up to this point, everything works just fine.

    Now, is there a way, within that VB.net program of mine, of writing code that will add a new Button to the Word Document, to which I can then assign an executable task ?

    The Button can be in the Quick Access Toolbar, at the end of the Home Ribbon or it can be Free Floating on the Document. When I click the Button, it will Save the Document, E:Mail the document to a specified Recipient, Close the Document & do some clean-up housework. It is the fact that it does this E:Mailing that I don't want to use a VBA Macro in the QAT, because as far as I can see, that Macro Button would be there for Every Word Document, not just these specific Documents, created via my program.

    I have custom Buttons on my Outlook program & I am certain I have done this in the past on Excel, but I just cannot see / find how to do this in Word 2007 !!!

    • Moved by Youen Zen Wednesday, January 23, 2013 3:06 AM Appropriate forum for this issue
    Tuesday, January 22, 2013 4:20 PM

Answers

  • I can't say I've ever seen a free floating button in Office 2007 or newer versions. Are you sure this can be done?

    I know that the toolbars in prior versions could be un-docked and float but I don't belive this is supported by the ribbons.

    See below link regarding the QAT:

    http://social.msdn.microsoft.com/Forums/zh/vsto/thread/4cfed23f-0cf3-41bb-b809-e14d6057bd10

    You may also want to post to the VSTO forum:

    http://social.msdn.microsoft.com/Forums/zh/vsto/threads


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, January 22, 2013 7:05 PM
  • Hi vodkasoda

    Mmm, I'm not quite clear on the entire requirement, but perhaps I can clear up some points...

    Buttons in the Ribbon or the QAT require a Ribbon XML customization and the code to which they link must be running in-process with the Word application. This can be: VBA code in the document or an attached template or a global template (template add-in); code in an IDTExtensibility2 COM Add-in (including VSTO Add-in).

    This would give you the options:

    - loading the add-in (whether template or COM) for this task, then unloading it when it's no longer required

    - attaching a template to the document you create to provide the interface and the code. In this case, the customization is only available to that document (and others attached to the same template)

    - create everything within the document

    Other kinds of buttons:

    1. ActiveX control or MacroButton field on the document surface. The former can be linked to outside code; the latter must be linked to VBA. Both will be part of the document and travel with it.

    2. "Floating": a Custom Task Pane can be used for this purpose. A CTP is available only in a COM Add-in.

    <<I execute a VB.net program, created in VS2010, that opens a Word document, if it exists, and creates it from a Skeleton version if it doesn't exist, that Word Document is then Opened for Editing & I do whatever editing I need to do>>

    It appears that you're currently doing all this in the Word application. If we're talking docx kinds of documents (as opposed to the old *.doc file format) you might do better to generate and edit the document (including adding a Ribbon XML customization and VBA code) using the Open XML file format. You can do this without opening the document in Word - it will be faster and no "flashing" on-screen, etc. The end-product can then be presented to the user.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 1:14 PM
    Moderator
  • It's not really so bad - it just appears complicated because it's all new for you :-) Best is a step at a time.

    In your place, I'd start with the template and making a VBA macro that emails the document.

    Once you have that working, add the Ribbon customization so that you have a button to run the macro.

    Next step is to test loading and unloading it by hand as a "global template add-in" so that you get a feel for that process. And test that the code will Email the currently opened document in Word.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 5:10 PM
    Moderator
  • <<I actually find the terminology more difficult to get my head around than the logic>>

    Sorry about that :-) Ask for clarification if anything is too murky. I'm always careful to use the "real" Word / Office terminology as it's what's needed to do an on-line search and also what I need when people ask me question in order to be certain what they're asking about.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 5:42 PM
    Moderator

All replies

  • I can't say I've ever seen a free floating button in Office 2007 or newer versions. Are you sure this can be done?

    I know that the toolbars in prior versions could be un-docked and float but I don't belive this is supported by the ribbons.

    See below link regarding the QAT:

    http://social.msdn.microsoft.com/Forums/zh/vsto/thread/4cfed23f-0cf3-41bb-b809-e14d6057bd10

    You may also want to post to the VSTO forum:

    http://social.msdn.microsoft.com/Forums/zh/vsto/threads


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Tuesday, January 22, 2013 7:05 PM
  • Paul, I have no idea if a Free Floating Button is possible in Word 2007, I was just emphasising that I need help in this and as there are normally a hundred different ways of doing things in VB.net, I was just emphasising that I don't care which method somebody suggests, I just need help !!!

    I will check your links, thank you ...

    Tuesday, January 22, 2013 11:40 PM
  • Hi vodkasoda

    Mmm, I'm not quite clear on the entire requirement, but perhaps I can clear up some points...

    Buttons in the Ribbon or the QAT require a Ribbon XML customization and the code to which they link must be running in-process with the Word application. This can be: VBA code in the document or an attached template or a global template (template add-in); code in an IDTExtensibility2 COM Add-in (including VSTO Add-in).

    This would give you the options:

    - loading the add-in (whether template or COM) for this task, then unloading it when it's no longer required

    - attaching a template to the document you create to provide the interface and the code. In this case, the customization is only available to that document (and others attached to the same template)

    - create everything within the document

    Other kinds of buttons:

    1. ActiveX control or MacroButton field on the document surface. The former can be linked to outside code; the latter must be linked to VBA. Both will be part of the document and travel with it.

    2. "Floating": a Custom Task Pane can be used for this purpose. A CTP is available only in a COM Add-in.

    <<I execute a VB.net program, created in VS2010, that opens a Word document, if it exists, and creates it from a Skeleton version if it doesn't exist, that Word Document is then Opened for Editing & I do whatever editing I need to do>>

    It appears that you're currently doing all this in the Word application. If we're talking docx kinds of documents (as opposed to the old *.doc file format) you might do better to generate and edit the document (including adding a Ribbon XML customization and VBA code) using the Open XML file format. You can do this without opening the document in Word - it will be faster and no "flashing" on-screen, etc. The end-product can then be presented to the user.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 1:14 PM
    Moderator
  • Wow, I sometimes wonder if I am biting off more than I can chew :-( !!!

    The program is created in VB.net / VS2010 & is executed via a button from a Windows folder, the Word document is a 97-2003 compatible .doc file.

    Basically that program says ...

    1. Here is a form, make a choice from each of these 2 ComboBoxes.

    2. Verify the existence of a file, depending on those choices.

    3. If it doesn't exist, create it via the command ...

    File.Copy(myTemplate, myNewsLetter) 


    4. Open the file (which now definitely exists) for editing using the commands ...

            MSDoc = MSWord.Documents.Open(myNewsLetter)
            MSWord.WindowState = Word.WdWindowState.wdWindowStateNormal
            MSWord.Visible = True
            MSWord.Activate()

    I then have a lot of writing / editing to do, which when finished, needs to be saved & E:Mailed. I want a Button to be sitting there that I can click & add my automation to.

    It all seems so easy when it's put like that :-) !!!

    From that rather simplistic explanation, would you recommend one of the options you have suggested over the others ?


    Wednesday, January 23, 2013 2:57 PM
  • Hi vodkasoda

    Yes, the category of "easier said than done" - that's a big pigeon hole...

    You mentioned "Word 2007" in your original message, but as this is a 97-2003 *.doc file, that changes things somewhat...

    This limits your choices to an add-in or attaching a template. Since the file should be emailed, attaching a template is probably not a good idea; that leaves an add-in of one kind or another.

    I'd be inclined to use a *.dotm template as an add-in, myself. It can contain the button (NOT floating) and the VBA code for it. Better to keep things in-process than to use a Click event on an old-fashioned CommandBarButton to call back into your VB.NET form.

    As far as the VB.NET side of things are concerned, it would use the WordApp.Addins collection to manage the "presence" of the template. Look at the Add, Item and Unload members to get a feel how to dynamically make the template's interface available in Word / remove it. Your VB.NET app can monitor Word's DocumentBeforeClose event to figure out when to unload the template add-in.

    You can use the Custom Ui Editor (a free download) to create a 2007 Ribbon XML customization in the template. Probably best to add a Group to the Home tab (TabStart) and put your button on that. Reference a Public Sub method in the template's VBA project that sends the document as an email. You'll find more about working with Ribbon customizations at
      http://msdn.microsoft.com/en-us/office/aa905530.aspx

    Especially good is the three part series http://msdn.microsoft.com/en-us/library/aa338202(v=office.12)


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 3:14 PM
    Moderator
  • Thank you Cindy, I at least know that it's possible now, even if it seems a lot more complicated than I originally envisaged !!! It is Word 2007 that I am using, but the person who receives the file, wants it in 97-2003 format, so many things to remember and include in descriptions ...

    OK, I'll have a look at some of this and see how long it takes before I have to go looking for a stiff drink :-o ... I miss the simplicity of COBOL so much !!!
    Wednesday, January 23, 2013 3:22 PM
  • It's not really so bad - it just appears complicated because it's all new for you :-) Best is a step at a time.

    In your place, I'd start with the template and making a VBA macro that emails the document.

    Once you have that working, add the Ribbon customization so that you have a button to run the macro.

    Next step is to test loading and unloading it by hand as a "global template add-in" so that you get a feel for that process. And test that the code will Email the currently opened document in Word.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 5:10 PM
    Moderator
  • I actually find the terminology more difficult to get my head around than the logic, but I will get there eventually !!! Thank you for your help, & I will follow your test plan, it makes a lot of sense :-) ...
    Wednesday, January 23, 2013 5:29 PM
  • <<I actually find the terminology more difficult to get my head around than the logic>>

    Sorry about that :-) Ask for clarification if anything is too murky. I'm always careful to use the "real" Word / Office terminology as it's what's needed to do an on-line search and also what I need when people ask me question in order to be certain what they're asking about.


    Cindy Meister, VSTO/Word MVP, my blog

    Wednesday, January 23, 2013 5:42 PM
    Moderator