{"id":171,"date":"2010-07-29T18:55:00","date_gmt":"2010-07-29T18:55:00","guid":{"rendered":"http:\/\/turtlellc.com\/index.php\/the-perfect-sharepoint-document-template\/"},"modified":"2010-07-29T18:55:00","modified_gmt":"2010-07-29T18:55:00","slug":"the-perfect-sharepoint-document-template","status":"publish","type":"post","link":"https:\/\/www.turtle.works\/knowledge\/the-perfect-sharepoint-document-template\/","title":{"rendered":"The Perfect SharePoint Document Template"},"content":{"rendered":"<p><span style=\"font-family: arial;font-family:arial;\" >SharePoint is a good Document Management System<\/span><span style=\"font-family: arial;font-family:arial;\" > until you need to embed properties within a document. Once properties are embedded they won&#8217;t update until you print, you manually update the fields or you add a macro to update them every time the document is opened. <\/span><\/p>\n<p><span style=\"font-family: arial;font-family:arial;\" >SharePoint as a Document Management System also falls down when you want to update the template used by documents and have that update affect all existing documents.<\/span><\/p>\n<p><span style=\"font-weight: bold; font-family: arial;font-family:arial;\" >The Perfect Document Template<\/span><\/p>\n<p><span style=\"font-family: arial;font-family:arial;\" >The perfect template will have a macro that updates embedded SharePoint properties whenever a document is opened. And the template will allow me to change the header and footer for all the documents whenever I want. To do this, I need a template that I can swap out whenever I want.<\/span><\/p>\n<p><span style=\"font-family: arial;font-family:arial;\" >Because of the default behaviour of SharePoint and Word, the template is downloaded once when you open a document. The template is never pulled again from SharePoint. See this <\/span><a style=\"font-family: arial;\" href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb421314%28office.11%29.aspx#Word2007_UsingCustomTemplates\">reference<\/a><span style=\"font-family: arial;font-family:arial;\" >. This means any updates you make to the template do not pass down to you.<\/span><\/p>\n<p><span style=\"font-family: arial;font-family:arial;\" >The article refered to from Microsoft above discusses building an XML Expansion Pack, which is overkill for what I need. And in fact doesn&#8217;t work with MOSS only WSS.<\/span><\/p>\n<p><span style=\"font-family: arial;font-family:arial;\" >The simpler solution is to build 2 templates: a driver template and an actual template.<\/span><\/p>\n<p><span style=\"font-weight: bold; font-family: arial;font-family:arial;\" >The Driver Template<\/span><\/p>\n<p><span style=\"font-family: arial;font-family:arial;\" >This is the template that is bound to the Content Type and is the one that gets downloaded and never updated. What this template does is binds the actual template you want to associate with the actual document.<\/span><\/p>\n<p><span style=\"font-weight: bold; font-family: arial;font-family:arial;\" >The Actual Template<\/span><\/p>\n<p><span style=\"font-family: arial;font-family:arial;\" >This template contains code to dynamically build the header and footer and update all the embedded properties.<\/span><\/p>\n<p><span style=\"font-weight: bold; font-family: arial;font-family:arial;\" >To Build This Solution<\/span><\/p>\n<ol style=\"font-family: arial;\">\n<li>Define your Document Library<\/li>\n<li>Define your Content Type<\/li>\n<li>Create a Driver Template in the Forms directory of your Document Library<\/li>\n<li>Create an Actual Template in the Forms directory as well<\/li>\n<li>Bind the Driver Template to the Content Type<\/li>\n<li>Test<\/li>\n<\/ol>\n<p><span style=\"font-family: arial;\">I won&#8217;t bother showing you how to create a Document Library or defining a Content Type, but I will show you steps 3 and 4.<\/span><\/p>\n<p><span style=\"font-weight: bold; font-family: arial;\">Create a Driver Template<\/span><\/p>\n<p><span style=\"font-family: arial;\">Open MS Word.<\/span><br \/><span style=\"font-family: arial;\">Alt + F8 will take you to macros<\/span><br \/><span style=\"font-family: arial;\">Create a macro called UpdateTemplate<\/span><br \/><span style=\"font-family: arial;\">Once you are in Visual Basic, paste this code in the code window:<\/span><\/p>\n<p><span style=\"font-family: arial;\">Private Sub Document_New()<\/span><\/p>\n<p><span style=\"font-family: arial;\">   &#8216;temporarily unlink this file from the template<\/span><br \/><span style=\"font-family: arial;\">   ActiveDocument.AttachedTemplate = &#8220;&#8221;<\/span><\/p>\n<p><span style=\"font-family: arial;\">   &#8216;and update<\/span><br \/><span style=\"font-family: arial;\">   UpdateTemplate<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Sub UpdateTemplate()<\/span><\/p>\n<p><span style=\"font-family: arial;\">    Dim strTemplatePath As String<\/span><br \/><span style=\"font-family: arial;\">    Dim doc As Document<\/span><\/p>\n<p><span style=\"font-family: arial;\">    &#8216; Get the path of the template file.<\/span><br \/><span style=\"font-family: arial;\">    strTemplatePath = &#8220;http:\/\/sharepoint-test\/Docs\/MyLibrary\/Forms\/ActualTemplate.dotm&#8221;<\/span><\/p>\n<p><span style=\"font-family: arial;\">    &#8216; Open the document template and save it to the local machine<\/span><br \/><span style=\"font-family: arial;\">    Set doc = Application.Documents.Open(strTemplatePath, Visible:=False)<\/span><br \/><span style=\"font-family: arial;\">    doc.SaveAs Environ(&#8220;Temp&#8221;) &amp; &#8220;ActualTemplate.dotm&#8221;<\/span><br \/><span style=\"font-family: arial;\">    doc.Close<\/span><\/p>\n<p><span style=\"font-family: arial;\">    &#8216; Add the template as an Add-in<\/span><br \/><span style=\"font-family: arial;\">    Application.AddIns.Add Environ(&#8220;Temp&#8221;) &amp; &#8220;ActualTemplate.dotm&#8221;<\/span><\/p>\n<p><span style=\"font-family: arial;\">    &#8216; Run the macro in the ActualTemplate template<\/span><br \/><span style=\"font-family: arial;\">    Application.Run &#8220;UpdateThisDocument&#8221;<\/span><\/p>\n<p><span style=\"font-family: arial;\">    &#8216; Rest of the lines do not execute.<\/span><br \/><span style=\"font-family: arial;\">    Application.AddIns.Unload True<\/span><br \/><span style=\"font-family: arial;\">    On Error Resume Next &#8216;In case another document is using the template<\/span><br \/><span style=\"font-family: arial;\">    Kill Environ(&#8220;Temp&#8221;) &amp; &#8220;ActualTemplate.dotm&#8221;<\/span><\/p>\n<p><span style=\"font-family: arial;\">    ActiveDocument.AttachedTemplate = strTemplatePath<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Private Sub Document_Open()<\/span><\/p>\n<p><span style=\"font-family: arial;\">   &#8216;do not do anything in the case where the solution author opens the template directly<\/span><br \/><span style=\"font-family: arial;\">   If (InStr(1, ActiveDocument.FullName, &#8220;.dotm&#8221;, vbTextCompare) = 0) Then<\/span><\/p>\n<p><span style=\"font-family: arial;\">       &#8216;temporarily unlink this file from the template<\/span><br \/><span style=\"font-family: arial;\">       ActiveDocument.AttachedTemplate = &#8220;&#8221;<\/span><\/p>\n<p><span style=\"font-family: arial;\">       &#8216;update template<\/span><br \/><span style=\"font-family: arial;\">       UpdateTemplate<\/span><\/p>\n<p><span style=\"font-family: arial;\">   End If<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Close the VB environment and save your template.<\/span><\/p>\n<p><span style=\"font-family: arial;\">NOTE: Make sure to save it as a .dotm (macro enabled template)<\/span><\/p>\n<p><span style=\"font-weight: bold; font-family: arial;\">Create an Actual Template<\/p>\n<p><\/span><span style=\"font-family: arial;\">Open MS Word.<\/span><br \/><span style=\"font-family: arial;\">Alt + F8 will take you to macros<\/span><br \/><span style=\"font-family: arial;\">Create a macro called UpdateThisDocument<\/span><br \/><span style=\"font-family: arial;\">Once you are in Visual Basic, paste this code in the code window:<\/span><\/p>\n<p><span style=\"font-family: arial;\">Sub Document_New()<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Sub Document_Open()<\/span><\/p>\n<p><span style=\"font-family: arial;\">   UpdateThisDocument<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Public Sub UpdateThisDocument()<\/span><\/p>\n<p><span style=\"font-family: arial;\">   Application.ScreenUpdating = False<\/span><\/p>\n<p><span style=\"font-family: arial;\">   Margins<\/span><\/p>\n<p><span style=\"font-family: arial;\">   &#8216;Header<\/span><br \/><span style=\"font-family: arial;\">   ClearHeaders<\/span><br \/><span style=\"font-family: arial;\">   AddHeader<\/span><\/p>\n<p><span style=\"font-family: arial;\">   &#8216;Footer<\/span><br \/><span style=\"font-family: arial;\">   ClearFooters<\/span><br \/><span style=\"font-family: arial;\">   AddFooter<\/span><\/p>\n<p><span style=\"font-family: arial;\">   &#8216;Update Fields<\/span><br \/><span style=\"font-family: arial;\">   UpdateFields<\/span><\/p>\n<p><span style=\"font-family: arial;\">   &#8216;Display Document Normally<\/span><br \/><span style=\"font-family: arial;\">   If ActiveDocument.ActiveWindow.View.SplitSpecial = wdPaneNone Then<\/span><br \/><span style=\"font-family: arial;\">       ActiveDocument.ActiveWindow.ActivePane.View.Type = wdPrintView<\/span><br \/><span style=\"font-family: arial;\">       ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument<\/span><br \/><span style=\"font-family: arial;\">       ActiveDocument.ActiveWindow.ActivePane.View.Type = wdPrintView<\/span><br \/><span style=\"font-family: arial;\">   Else<\/span><br \/><span style=\"font-family: arial;\">       ActiveDocument.ActiveWindow.View.Type = wdPrintView<\/span><br \/><span style=\"font-family: arial;\">       ActiveDocument.ActiveWindow.View.SeekView = wdSeekMainDocument<\/span><br \/><span style=\"font-family: arial;\">       ActiveDocument.ActiveWindow.View.Type = wdPrintView<\/span><br \/><span style=\"font-family: arial;\">   End If<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Private Sub Margins()<\/span><\/p>\n<p><span style=\"font-family: arial;\">   With ActiveDocument.Styles(wdStyleNormal).Font<\/span><br \/><span style=\"font-family: arial;\">       If .NameFarEast = .NameAscii Then<\/span><br \/><span style=\"font-family: arial;\">           .NameAscii = &#8220;&#8221;<\/span><br \/><span style=\"font-family: arial;\">       End If<\/span><br \/><span style=\"font-family: arial;\">       .NameFarEast = &#8220;&#8221;<\/span><br \/><span style=\"font-family: arial;\">   End With<\/span><br \/><span style=\"font-family: arial;\">   With ActiveDocument.PageSetup<\/span><br \/><span style=\"font-family: arial;\">       .LineNumbering.Active = False<\/span><br \/><span style=\"font-family: arial;\">       .TopMargin = InchesToPoints(0.5)<\/span><br \/><span style=\"font-family: arial;\">       .BottomMargin = InchesToPoints(0.7)<\/span><br \/><span style=\"font-family: arial;\">       .LeftMargin = InchesToPoints(0.5)<\/span><br \/><span style=\"font-family: arial;\">       .RightMargin = InchesToPoints(0.5)<\/span><br \/><span style=\"font-family: arial;\">       .Gutter = InchesToPoints(0)<\/span><br \/><span style=\"font-family: arial;\">       .HeaderDistance = InchesToPoints(0.4)<\/span><br \/><span style=\"font-family: arial;\">       .FooterDistance = InchesToPoints(0.5)<\/span><br \/><span style=\"font-family: arial;\">       .PageWidth = InchesToPoints(8.5)<\/span><br \/><span style=\"font-family: arial;\">       .PageHeight = InchesToPoints(11)<\/span><br \/><span style=\"font-family: arial;\">       .SectionStart = wdSectionNewPage<\/span><br \/><span style=\"font-family: arial;\">       .OddAndEvenPagesHeaderFooter = False<\/span><br \/><span style=\"font-family: arial;\">       .DifferentFirstPageHeaderFooter = False<\/span><br \/><span style=\"font-family: arial;\">       .VerticalAlignment = wdAlignVerticalTop<\/span><br \/><span style=\"font-family: arial;\">       .SuppressEndnotes = False<\/span><br \/><span style=\"font-family: arial;\">       .MirrorMargins = False<\/span><br \/><span style=\"font-family: arial;\">       .TwoPagesOnOne = False<\/span><br \/><span style=\"font-family: arial;\">       .BookFoldPrinting = False<\/span><br \/><span style=\"font-family: arial;\">       .BookFoldRevPrinting = False<\/span><br \/><span style=\"font-family: arial;\">       .BookFoldPrintingSheets = 1<\/span><br \/><span style=\"font-family: arial;\">       .GutterPos = wdGutterPosLeft<\/span><br \/><span style=\"font-family: arial;\">   End With<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Private Sub ClearHeaders()<\/span><br \/><span style=\"font-family: arial;\">    Dim hdr As HeaderFooter<\/span><\/p>\n<p><span style=\"font-family: arial;\">    For Each hdr In ActiveDocument.Sections(1).Headers<\/span><br \/><span style=\"font-family: arial;\">       hdr.Range.Text = vbNullString<\/span><br \/><span style=\"font-family: arial;\">    Next hdr<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Private Sub AddHeader()<\/span><\/p>\n<p><span style=\"font-family: arial;\">&#8216;<\/span><br \/><span style=\"font-family: arial;\">&#8216;TODO: Add your header code here<\/span><br \/><span style=\"font-family: arial;\">&#8216;<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Private Sub ClearFooters()<\/span><br \/><span style=\"font-family: arial;\">    Dim hdr As HeaderFooter<\/span><\/p>\n<p><span style=\"font-family: arial;\">    For Each hdr In ActiveDocument.Sections(1).Footers<\/span><br \/><span style=\"font-family: arial;\">       hdr.Range.Text = vbNullString<\/span><br \/><span style=\"font-family: arial;\">    Next hdr<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Private Sub AddFooter()<\/span><\/p>\n<p><span style=\"font-family: arial;\">&#8216;<\/span><br \/><span style=\"font-family: arial;\">&#8216;TODO: Add your footer code here<\/span><br \/><span style=\"font-family: arial;\">&#8216;<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Private Sub UpdateFields()<\/span><\/p>\n<p><span style=\"font-family: arial;\"> With Options<\/span><br \/><span style=\"font-family: arial;\">     .UpdateFieldsAtPrint = True<\/span><br \/><span style=\"font-family: arial;\">     .UpdateLinksAtPrint = True<\/span><br \/><span style=\"font-family: arial;\"> End With<\/span><\/p>\n<p><span style=\"font-family: arial;\"> If ActiveDocument.ActiveWindow.View.SplitSpecial <> wdPaneNone Then<\/span><br \/><span style=\"font-family: arial;\">     ActiveDocument.ActiveWindow.Panes(2).Close<\/span><br \/><span style=\"font-family: arial;\"> End If<\/span><\/p>\n<p><span style=\"font-family: arial;\"> ActiveDocument.ActiveWindow.View = wdNormalView<\/span><\/p>\n<p><span style=\"font-family: arial;\"> &#8216;Application.ScreenUpdating = False<\/span><\/p>\n<p><span style=\"font-family: arial;\"> If ActiveDocument.ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveDocument.ActiveWindow.ActivePane.View.Type = wdOutlineView Then<\/span><br \/><span style=\"font-family: arial;\">     ActiveDocument.ActiveWindow.ActivePane.View.Type = wdPrintView<\/span><br \/><span style=\"font-family: arial;\"> End If<\/span><\/p>\n<p><span style=\"font-family: arial;\"> &#8216;Footer<\/span><br \/><span style=\"font-family: arial;\"> ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekPrimaryFooter<\/span><br \/><span style=\"font-family: arial;\"> Selection.WholeStory<\/span><br \/><span style=\"font-family: arial;\"> Selection.Fields.Update<\/span><\/p>\n<p><span style=\"font-family: arial;\"> &#8216;Main Document<\/span><br \/><span style=\"font-family: arial;\"> ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument<\/span><br \/><span style=\"font-family: arial;\"> Selection.WholeStory<\/span><br \/><span style=\"font-family: arial;\"> Selection.Fields.Update<\/span><\/p>\n<p><span style=\"font-family: arial;\"> &#8216;Header<\/span><br \/><span style=\"font-family: arial;\"> ActiveDocument.ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader<\/span><br \/><span style=\"font-family: arial;\"> Selection.WholeStory<\/span><br \/><span style=\"font-family: arial;\"> Selection.Fields.Update<\/span><\/p>\n<p><span style=\"font-family: arial;\">End Sub<\/span><\/p>\n<p><span style=\"font-family: arial;\">Close the VB environment and again save your template as a .dotm file.<\/span><\/p>\n<p><span style=\"font-weight: bold; font-family: arial;font-family:arial;\" >Bind the Driver Template to the Content Type<\/span><\/p>\n<p><span style=\"font-family: arial;\">Go to your Document Library<\/span><br \/><span style=\"font-family: arial;\">Settings > Document Library Settings<\/span><br \/><span style=\"font-family: arial;\">Under Content Types, click on your Content Type<\/span><br \/><span style=\"font-family: arial;\">Click Advanced Settings<\/span><br \/><span style=\"font-family: arial;\">Enter the URL for the DriverTemplate.dotm (http:\/\/sharepoint-test\/Docs\/MyLibrary\/Forms\/DriverTemplate.dotm)<\/span><br \/><span style=\"font-family: arial;\">Click OK<\/span><\/p>\n<p><span style=\"font-weight: bold; font-family: arial;\">Test<\/p>\n<p><\/span><span style=\"font-family: arial;\">In the Document Library, click New and select your Content Type.<br \/>At this point your macro code in ActualTemplate.dotm should have fired. Any header or footer creation should have occurred.<\/span><span style=\"font-weight: bold; font-family: arial;\"><\/p>\n<p><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>SharePoint is a good Document Management System until you need to embed properties within a document. Once properties are embedded they won&#8217;t update until you print, you manually update the fields or you add a macro to update them every time the document is opened. SharePoint as a Document Management System also falls down when &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.turtle.works\/knowledge\/the-perfect-sharepoint-document-template\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;The Perfect SharePoint Document Template&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[38,39,41,20,29],"tags":[],"class_list":["post-171","post","type-post","status-publish","format-standard","hentry","category-document-library","category-document-management","category-moss","category-sharepoint","category-template"],"_links":{"self":[{"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/posts\/171","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":0,"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.turtle.works\/knowledge\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}