1
votes

I am trying to create a VSTO Word Add-In to display some buttons in the ribbon, linked to templates stored on our file server.

I was adding the buttons to the Ribbon through the xml file.

<?xml version="1.0" encoding="UTF-8"?>
<customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui" onLoad="Ribbon_Load">
  <ribbon>
    <tabs>
      <tab idMso="TabAddIns">
        <group id="ContentGroup" label="YP Council Templates">
          <button id="textButtonLetter" label="Letter"
               screentip="Letter" onAction="OnTextButton"
               supertip="Open the Letter Template."/>
          <button id="textButtonMemorandym" label="Memorandum"
               screentip="Memorandum" onAction="OnTextButton"
               supertip="Open the Memorandum Template."/>
          <button id="textButtonFacsimile" label="Facsimile"
               screentip="Facsimile" onAction="OnTextButton"
               supertip="Open the Facsimile Template."/>
          <button id="textButtonPolicy" label="Policy Template"
               screentip="Policy Template" onAction="OnTextButton"
               supertip="Open the Policy Template."/>
        </group>
      </tab>
    </tabs>
  </ribbon>
</customUI>

Through the above code, I've successfully added the buttons in a group, but am wondering how I create the Action to open a Word Template file? Is it through a Macro, if so, where does the macro go? (NB - the onAction="OnTextButton" code is grabbed from a sample I was using)

The end goal, is that once the template file is opened (a new document based on the template), they should then use it as a basis for a new document.

Regards, Stewart

1

1 Answers

1
votes

When working with Word, template files should not be opened. (Which usually means the plan is to use "save as" to make the new document - that's frought with danger.)

Word has the Documents.Add() method to create a new document as a "copy" of a template file. Any boiler-plate in the template file is brought into the new document, along with the styles defined in the template.

If this is a true template (*.dotx or *.dotm) the document retains an active link to the template, so the user can access content stored in it:

  • Building Blocks
  • keyboard shortcuts
  • macros
  • ribbon customizations

To generate a new document from a template (or another document) use the Documents.Add method:

object missing = System.Type.Missing;
Globals.ThisAddin.Application.Documents.Add("pathToTemplateFile", ref missing, 
                                             ref missing, ref missing);

Note that it's seldom necessary to use the three optional parameters of the method, but you should look them up in the Word object model language reference just so you know what they are.