Word macros in the custom template for the document library disappears once document saved - SPS 2003 / MOSS

I have came across an interesting integration of Smart Documents with the SharePoint Document libraries through the custom template. I thought of sharing this with you guys. As long as you are using the OOB template.docx then we never worried about the document templates and its behavior but when we are using the custom templates with Macros on it then you will be encountering the issue on synchronizing the template with the client.

Whenever you add any custom Macros in the custom template of the document library then its mandatory to add two more default macros which are documented in the following article

https://msdn.microsoft.com/en-us/library/bb421314(office.11).aspx . But this article has a missing part which I covered in this post.

I have given the issue description and the resolution below and it applies for both SPS 2003 and MOSS and just the difference lies in the Office version. 

 

Problem Description

=================

When you create a document based on the custom template (with Macros in it) in your SharePoint document library, the macros in the document are disappears when you checked out and saved the document.

Root Cause

=========

The Reason for this behavior is that the document is referring the document template in the temporary internet files and the macros are no longer exists in those template.

Creating Custom template for WSS 2.0 Document Library

==========================================

When you open a document template from a Windows SharePoint Services document library, Word downloads a temporary copy of the template to the Temporary Internet Files folder on the local computer. After you close the document, Word deletes the local copy of the template. However, the document continues to reference the local file instead of the template located on the server. When we are creating custom template for SharePoint document library, we need to make sure that every time the user creates or opens the document ,the template from the server is update with the local template.

To achieve this, we need to create a XML expansion pack and make the document template as part of the XML expansion pack. Whenever the user creates or opens the document he will be asking to install the XML expansion pack and he will be downloading the document template from the server to user’s local machine.

Steps to create the XML expansion pack

============================

1. The XML expansion pack consist the following files:

a. A XML schema file

b. A manifest file with information about the XML schema and the custom template

2. Creating Schema for XML expansion pack:

-------------------------------------------------------

The following code shows the structure for schema file for XML expansion pack. Create this file and save it as schema.xml

<?xml version="1.0" encoding="utf-8" ?>

<?mso-solutionextension URI="BarbiTest" manifestPath="https://sigr2-16a:6000/BarbiTest/Forms/manifest.xml"?>

<xs:schema targetNamespace="BarbiTest" xmlns:xs="https://www.w3.org/2001/XMLSchema"></xs:schema>

Note : Change the manifest path to your sharepoint site. The “URI” and the “targetnamespace” must be the same (Case sensitive ).

3. Creating Manifest file for XML expansion pack:

------------------------------------------------------------

The following code shows the structure for manifest file for XML expansion pack. Create this file and save it as manifest.xml

<manifest xmlns="https://schemas.microsoft.com/office/xmlexpansionpacks/2003">

  <version>1.0</version>

  <uri>BarbiTest</uri>

<?mso-solutionextension URI="BarbiTest" manifestPath="https://sigr2-16a:6000/BarbiTest/Forms/manifest.xml"?> // missing part in the article

  <solution>

    <solutionID>testSolution</solutionID>

    <type>other</type>

    <alias lcid="*">Template</alias>

    <file>

      <type>template</type>

      <version>1.0</version>

      <filePath>https://sigr2-16a:6000/Barbitest/Forms/custom.dot</filePath> (change the path to your sharepoint site)

      <templateID>{BAE06134-7C7E-4216-835F-6A0A63993919}</templateID>

    </file>

  </solution>

  <solution>

    <solutionID>testSolution-Schema</solutionID>

    <type>schema</type>

    <alias lcid="*">Template</alias>

    <file>

      <type>schema</type>

      <version>1.0</version>

      <filePath>https://sigr2-16a:6000/Barbitest/Forms/schema.xml</filePath> (change the path to your sharepoint site)

    </file>

  </solution>

          Note : Make sure that the URI and the namespace name are same as the schema.xml file created above

                                                                  

4. Code Signing the XML expansion pack manifest file:

-----------------------------------------------------------------

It is mandatory to code sign the XML expansion pack manifest file. The code signing tool for XML expansion pack comes with the Smart Document SDK. Download the smart document SDK from the following location https://www.microsoft.com/downloads/details.aspx?FamilyId=24A557F7-EB06-4A2C-8F6C-2767B174126F&displaylang=en

           

The tool XMLSign.exe will be installed in the following path C:\Program Files\Microsoft Office 2003 Developer Resources\Microsoft Office 2003 Smart Document SDK\Tools. We need to create a digital certificate to sign the manifest.xml file using this tool. After creating the digital certificate, Use the XMLSign tool through command prompt to code sign the manifest.xml file.

The following is the command for signing. Xmlsign –cn <digital certificate name> <the manifest file with path>

Steps to create custom document template

=================================

1. Create a word document and save it as document template (.dot)

2. Add the following two macros to the template. These macros will make sure that the document refers the local template downloaded by the XML expansion pack and not from the temporary internet file folder.

Private Sub Document_New()

  ' Temporarily unlink this file from the template.

  ActiveDocument.AttachedTemplate = ""

  ' Then update the template (the UpdateTemplate function is a separate function).

  UpdateTemplate

End Sub

Private Sub Document_Open()

  ' Do nothing if the solution author opens the template directly.

  If (InStr(1, ActiveDocument.FullName, ".dot", vbTextCompare) = 0) Then

    ' Temporarily unlink this file from the template.

    ActiveDocument.AttachedTemplate = ""

    ' Update template.

    UpdateTemplate

  End If

End Sub

Sub UpdateTemplate()

  ' If the user has not downloaded the XML expansion pack at least once,

  ' this will fail; On Error Resume Next allows the code to continue.

  On Error Resume Next

  Dim strTemplatePath As String

  ' Get file name for local template - to do this, we need this path:

  ' %USERPROFILE%\Local Settings\Application Data\Microsoft\Schemas\

  ' <namespace>\<solutionId>\< templateFilename >.

  ' IMPORTANT: You need to specify the <namespace>, <solutionId>,

  ' and < templateFilename > for your custom template.

  strTemplatePath = Environ("USERPROFILE") & _

    "\Local Settings\Application Data\Microsoft\Schemas\" & _

    "BarbiTest" & "\" & _

    "TestSolution" & "\" & _

    "custom.dot"

  ' Re-link document to local template.

  ActiveDocument.AttachedTemplate = strTemplatePath

End Sub

Place the Schema.xml , manifest.xml (code signed) and the custom template (with two important macros) to the Forms directory of your SharePoint document library.

Attaching the XML expansion pack with the Custom template

================================================

1. Open the custom document template from the forms directory and go to toolsàTemplates and Add ins

2. Select the XML Schema tab

3. Add the schema.xml file from the Forms directory of SharePoint document library

4. Select the XML expansion pack tab

5. Add the manifest.xml from the Forms directory of SharePoint document library

6. Make sure that all the schemas attached have been checked

7. After attaching the schemas, go to toolsàOptions

8. Select the security tab and select digital signature

9. Select the Digital certificate which we created earlier and signed the template ( It is mandatory to digitally sign the template so that it will automatically run the Document_New and Document_Open Macros)

With these steps your custom template become Smart Document. When user creates a new document they will be asked to install the XML expansion pack , because the custom template is the part of the XML expansion pack. When the user installs the XML expansion pack the document template is downloaded from the server and installed in the user’s machine. Then every time when he opens the document the template will be referred from the following path C:\Documents and Settings\<user>\Local Settings\Application Data\Microsoft\Schemas\<Namespace>\<solutionId> and not from the internet files folder. This makes the Macros alive even after the document is checked out and saved.

Hope this helps someone in future J

Comments

  • Anonymous
    February 10, 2010
    Hi, I have been following your instructions. I hit a block however when I get to this step:
  1. Add the manifest.xml from the Forms directory of SharePoint document library After clicking the Enable Macros button, Word tries to download the template file. I then get the message "A file that's part of this XML expansion pack could not be downloaded. The XML expansion pack may not work properly. ..." I'm using Word 2003. I have checked all the paths in the manifest file and they are correct. Any idea what the problem could be? Thanks, Dave
  • Anonymous
    November 01, 2011
    Hmm don't know where the post disappeared :S. What do you mean by forms directory of sharepoint?

  • Anonymous
    November 04, 2011
    Hi , I have a question. In this case of custom document template, what will happen if we migrated the site to a new site with sharepoint 2010? I am asking this, because when we migrated, now when we download the documents created by the template, they are still pointing to the old manifest file, thus it tries to ask for credentials for the current server (which is ok) and for the  previous server as well (which is definitely now ok)