Arthur's Bit Bucket

Friday, April 29, 2005

SharePoint document library properties, Word fields, and macros, oh my!

SharePoint does a great job promoting properties from document libraries into the Word documents inside the libraries. You can then insert these properties via Word fields into your document content to good effect. However, you will notice that opening the Word document doesn't cause the fields to be updated with the latest values on that document from the document library.

I added an AutoOpen macro to my Word document to get it to automatically update the fields when the document gets opened, and then set this document as the template document for the document library. When I created a new document from the document library's New Document link, the Word fields did not automatically update as I had hoped, and I found that my AutoOpen macro was not in the new document.

To fix this, I turned the document into a Word document template (.dot). Instead of an AutoOpen macro, I created an AutoNew macro to generate the AutoOpen macro and insert it into the document created when the Word template is opened. I then tried creating a new document from the library again, and it created the AutoOpen macro as I expected in the resulting document. However, when I re-opened the new document after saving it back to the document library, I got another error that it couldn't find the template document (it looked like it was trying to find it in my temporary files). I corrected this by having the AutoNew macro change the template the new document was based on to Normal.dot. This worked like a charm. Note the use of late binding (Object instead of CodeModule for the macros variable)--this avoids a dependency needed by Office 2003 on a VBA helper library that isn't available in older (Office 97) clients.

Here's the AutoNew macro:

Sub AutoNew()
'
' AutoNew Macro
' Inserts a macro into the document to update all fields on opening
'
Dim macros As Object
Dim code As String

'Build the AutoOpen macro for the Word document
code = "Sub AutoOpen()" + vbCrLf
code = code + "Dim aStory As Range" + vbCrLf
code = code + "Dim aField As Field" + vbCrLf
code = code + "For Each aStory In ActiveDocument.StoryRanges" + vbCrLf
code = code + "For Each aField In aStory.Fields" + vbCrLf
code = code + "aField.Update" + vbCrLf
code = code + "Next aField" + vbCrLf
code = code + "Next aStory" + vbCrLf
code = code + "End Sub" + vbCrLf

'Insert the macro and change the template on the resulting document
Set macros = ActiveDocument.VBProject.VBComponents(1).CodeModule
macros.AddFromString code
ActiveDocument.AttachedTemplate = "Normal.dot"
End Sub

0 Comments:

Post a Comment

<< Home