none
Including a word template with my Word Addin RRS feed

  • Question

  • I am developing with VSTO and Office 2010.  I am trying to implement "How to: Add Commands to Shortcut Menus in Word"

    http://msdn.microsoft.com/en-us/library/dd554969.aspx#Y0

    Part of the implementation is a reference to a "custom template" to act as the context for customizing the new CommandBarButton controls.  If I manually create a .dot file at the path the How to article requests, (Environment.SpecialFolder.MyDocuments) + "\MyCustomTemplate.dotx", then the Addin from the How to article works fine.

    I need to distribute this Addin to users, and I have two questions:

    1. the path for the .dotx file is in plain view in the user's MyDocuments directory.  If the user deletes that file, then my Word Addin will no longer run.  This is bad.  I want the .dotx file somewhere that will be kept out of sight, somewhere the user is unlikely to delete it, and I want it to go away if the user uninstalls my Addin.

    2. How do I include this  "\MyCustomTemplate.dotx" file as part of my project?  I don't want to have to tell every user to make sure they have saved this file somewhere.

    What is considered best practice for dealing with the template file that my Word Addin needs to safely manipulate the shortcut menu?

    Tuesday, March 6, 2012 3:19 PM

Answers

  • I recommend the user templates folder, which is typically the same folder path as where Word's Normal template is stored.  To access it you can use Environment.Special.ApplicationDate & "\Microsoft\Templates\" or Application.Options.DefaultFilePath(Word.WdDefaultFilePath.wdUserTemplatesPath) & "\".  The later is probably a bit better in case the user has redirected it.

    By default the user's AppData path is hidden though it does not take special permissions to unhide it.  With that said, my experience is that users don't typically go there and if they do, they're usually of a type that knows better than to just start deleting files.

    Finally, I have found that the best way to get your template, or any files for that matter where you need them is to have a "first time" start-up rountine that copies the necessary files from your Program Files subfolder directory and places them into the user's directory. You could upon Add-in load check if the template exists where it should be and if not there, copy it in.

    Hope that helps


    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, March 6, 2012 5:58 PM
  • OK, I ended up with the following.  I include the template file (myTemplate.dotx) within the Setup project as an included file.  The file does indeed get copied over to the client machine during Setup, ending up in the same directory as the Word Add-In executables.

    The Add-In then accesses the template file with the code:

        Private Sub GetCustomTemplate()
    
            Dim path As String = System.Reflection.Assembly.GetExecutingAssembly().CodeBase
            If path.StartsWith("file") Then
                path = path.Substring(8)
            End If
            path = System.IO.Path.GetDirectoryName(path)
            Dim TemplatePath As String = path + "\MyTemplate.dotx"
            Dim install As Boolean = True
    
            For Each installedTemplate As Word.Template In MyApplication.Templates
                If installedTemplate.FullName = DirectCast(TemplatePath, String) Then
                    install = False
                End If
            Next
    
            If install = True Then
                MyApplication.AddIns.Add(TemplatePath.ToString(), True)
            End If
            customTemplate = MyApplication.Templates.Item(TemplatePath)
    
        End Sub
    

    If the Client uninstalls, then the template file gets deleted along with the executables.

    Wednesday, March 7, 2012 4:37 PM

All replies

  • I recommend the user templates folder, which is typically the same folder path as where Word's Normal template is stored.  To access it you can use Environment.Special.ApplicationDate & "\Microsoft\Templates\" or Application.Options.DefaultFilePath(Word.WdDefaultFilePath.wdUserTemplatesPath) & "\".  The later is probably a bit better in case the user has redirected it.

    By default the user's AppData path is hidden though it does not take special permissions to unhide it.  With that said, my experience is that users don't typically go there and if they do, they're usually of a type that knows better than to just start deleting files.

    Finally, I have found that the best way to get your template, or any files for that matter where you need them is to have a "first time" start-up rountine that copies the necessary files from your Program Files subfolder directory and places them into the user's directory. You could upon Add-in load check if the template exists where it should be and if not there, copy it in.

    Hope that helps


    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, March 6, 2012 5:58 PM
  • Application.Options.DefaultFilePath(Word.WdDefaultFilePath.wdUserTemplatesPath)

    That path is perfect, thank you.  I guess we copy the file over during installation in the manner shown here, under "Additional Requirements for Document-level Solutions"?

    http://msdn.microsoft.com/en-us/library/ff937654.aspx

    Or is there an easier method?

    Tuesday, March 6, 2012 9:12 PM
  • Application.Options.DefaultFilePath(Word.WdDefaultFilePath.wdUserTemplatesPath)

    That path is perfect, thank you.  I guess we copy the file over during installation in the manner shown here, under "Additional Requirements for Document-level Solutions"?

    http://msdn.microsoft.com/en-us/library/ff937654.aspx

    Or is there an easier method?

    I can't answer that question.  Easy is what works for you and doesn't cause you issues. :-)

    Kind Regards, Rich ... http://greatcirclelearning.com

    Tuesday, March 6, 2012 10:24 PM
  • OK, I ended up with the following.  I include the template file (myTemplate.dotx) within the Setup project as an included file.  The file does indeed get copied over to the client machine during Setup, ending up in the same directory as the Word Add-In executables.

    The Add-In then accesses the template file with the code:

        Private Sub GetCustomTemplate()
    
            Dim path As String = System.Reflection.Assembly.GetExecutingAssembly().CodeBase
            If path.StartsWith("file") Then
                path = path.Substring(8)
            End If
            path = System.IO.Path.GetDirectoryName(path)
            Dim TemplatePath As String = path + "\MyTemplate.dotx"
            Dim install As Boolean = True
    
            For Each installedTemplate As Word.Template In MyApplication.Templates
                If installedTemplate.FullName = DirectCast(TemplatePath, String) Then
                    install = False
                End If
            Next
    
            If install = True Then
                MyApplication.AddIns.Add(TemplatePath.ToString(), True)
            End If
            customTemplate = MyApplication.Templates.Item(TemplatePath)
    
        End Sub
    

    If the Client uninstalls, then the template file gets deleted along with the executables.

    Wednesday, March 7, 2012 4:37 PM