29 April 2009

Display SharePoint Metadata in Word 2003

Yes, there are still customers who use Office 2003 in combination with SharePoint 2007...

So i had to find a generic solution do display SharePoint Metadata in Word 2003 templates. I found different solutions to achieve this, but none of them solved all my Problems. The most common Problem ist the update of your Fields at the right time.

Basics / Limitations:
  • SharePoint Metadata is mapped into Word 2003 over Document Properties
  • Only Fields of Type "Text" can be mapped (If you need to display Values of other Fields than Type Text, you need to add a Textfield to your Content Type and use a Eventhandler to Copy the Value of your desired Field to the Textfield and map this as Doument Property to your Word Template. A common case could be "Version" or Author of a Document, which are OOB no Textfields)
  • OOB Document Properties are updated on Print only (This is a Word Option which is set by Default)

    How to:
  • Bind your Word Template to corresponding ContentType of the DocLib
  • Add the following Code, which updates the Fields when the Document opens, as Macro (Macro on the current Document, not as Module) to your Word Template:

    Private Sub Document_New()

    ActiveDocument.Fields.Update

    End Sub

    Private Sub Document_Open()

    ActiveDocument.Fields.Update

    Dim oStory As Object
    Dim oToc As Object

    'exit if no document is open
    If Documents.Count = 0 Then Exit Sub
    Application.ScreenUpdating = False

    For Each oStory In ActiveDocument.StoryRanges
    oStory.Fields.Update 'update fields in all stories
    Next oStory

    For Each oToc In ActiveDocument.TablesOfContents
    oToc.Update 'update TOC's
    Next oToc

    Application.ScreenUpdating = True

    End Sub
    Private Sub Document_Close()

    ActiveDocument.Fields.Update

    End Sub

    Private Sub Document_Sync(ByVal SyncEventType As Office.MsoSyncEventType)

    End Sub

    This Code updates all Sections of the Document including Header and Footer, not just the Content Section.

Save your Template and create your first Document from this Template, enjoy the Interopability from SharePoint 2007 and Office 2003. ;-)

4 Kommentare:

Anonym hat gesagt…

Any idea to make it two way? That is to say, change the document, and get the metadata updated?
Thansk,
Barbara

Marcel Haas hat gesagt…

Sorry, I haven't seen that with Office 2003. For such scenarios I would suggest use Office 2007.

Surya hat gesagt…

How will this work when user is uploading a document instead of creating new from Sharepoint Doc Lib?

Philipp hat gesagt…

Hi,

great, but there's a limitation on 256 signs, am I right? Any idea how to bypass this limitation to get more text in this field?