Monday, 25 July 2011

Display SharePoint Metadata in Word 2003

So I had to find a generic solution shows the SharePoint metadata in Word 2003 templates. I have found different solutions to achieve this goal, but none of them solved all my problems. The most common problem ist updating their fields at the right time.

Getting Started / Limitations:
* SharePoint Metadata in Word 2003 is assigned more than Document Properties
* Only fields of type "Text" can be assigned (If you need to display the values of other text fields, you must add a text field to your type of content and use an EventHandler to copy the value to the desired the text field and the map as Doument this property to your Word template. A common scenario might be "version" or author of a document that are not text fields OOB)
* Properties OOB document updates Print only (This is a Word option that is set by default)
How:
* Link to your Word template for ContentType corresponding DocLib
* Add the following code, which updates the fields when the document is opened, such as Macro (Macro in the current document, not as module) for the Word template:

  1. Private Sub Document_New()
  2.         ActiveDocument.Fields.Update()
  3.     End Sub
  4.     Private Sub Document_Open()
  5.         ActiveDocument.Fields.Update()
  6. OStory As Object
  7. OTOC As Object
  8.         'exit if no document is open
  9.         If Documents.Count = 0 Then Exit Sub
  10.         Application.ScreenUpdating = False
  11.         For Each oStory In ActiveDocument.StoryRanges
  12.             oStory.Fields.Update() 'update the fields in all stories
  13.         Next oStory
  14.         For Each OTOC In ActiveDocument.TablesOfContents
  15.             oToc.Update() 'update TOC
  16. OTOC Next
  17.             Application.ScreenUpdating = True
  18.     End Sub
  19.     Private Sub Document_Close()
  20.         ActiveDocument.Fields.Update()
  21.     End Sub
  22. Document_Sync Private Sub (ByVal SyncEventType As Office.MsoSyncEventType)
  23. End Sub

This code updates all sections of the document, including header and footer, not only the content section.
Save the template and create your first document of this template. 

No comments:

Post a Comment