We used this to generate offer letters for our HR application. Offer letter is stored in MS word, to populate the data into it I had to create a new offer word template with form fields on it. All you have to do then is to have a profile document to tell the script which Notes fields corresponds to field in word or you can name them same to eliminate the profile keyword creation.
As we have six different templates I had to create a new form and doc from it to store these different word template embedded on it. I then display these templates in a dialog for the user to select a template.
This is the script, which writes data from notes field to word form fields.
'Error handler routine
On Error Goto ErrorHandle
'Constants used in this function
Const VIEW_STORING_EMBED_FILE = "Templates"
Const FIELD_TO_MAILMERGE_FIELD = "MergeFields"
Const MAILMERGE_FIELD_DELIMITER = "~"
Const MERGED_DOC_SAVE_DIR_NAME = "C:Temp"
Const MERGED_DOC_FORM_NAME = "MWorksheet"
'Define objects to be used for this function
Dim embedDoc As notesdocument
Dim embedUIDoc As notesuidocument
Dim embedView As notesview
Dim rtitem As Variant
Dim countr As Integer
Dim FileToAttach() As String
Dim strAttachName As String
Dim strEnvironment As String
If Not uidoc.editmode Then uidoc.editmode = True
Call uidoc.save
strEnvironment = Trim$(doc.TemplateName(0))
'strEnvironment = Trim$(session.GetEnvironmentString("TEMPLATE_CIN", False))
'Get handle to the template view and the embeded template to be used
'for word mailmerge
Set embedView = db.getview(VIEW_STORING_EMBED_FILE)
If embedView Is Nothing Then Error(4001)
Set embedDoc = embedView.getdocumentbykey(strEnvironment)
If embedDoc Is Nothing Then Error(4002)
Set pitem = embedDoc.getfirstitem(FIELD_TO_MAILMERGE_FIELD)
If pitem Is Nothing Then Error(4000)
'Proceed only if the embeded template is found
If Not embedDoc Is Nothing Then
Print "Opening MS Word Template Document..."
'Open the embedded template document to get handle to word object
Set embedUIDoc = ws.editdocument(True, embedDoc )
Set worddoc = embedUIDoc.getobject("Mail Template")
Call worddoc.activate
'Start populating fields on the embedded object
Dim strNotesField As String
For i = 0 To Ubound(pitem.values)
'Extract names of Notes & Word fields from the list
strNotesField = Trim$(FindMemberFromList( Trim$(pitem.values(i)), MAILMERGE_FIELD_DELIMITER, 1 ))
'if the notes field exists on the document then populate word field with value
If doc.hasitem(strNotesField) Then
Set itm = doc.getfirstitem(strNotesField)
If Not itm Is Nothing Then
'Manager name needs to common name
With WordDoc
temp = Cstr(Trim$(itm.values(0)))
.FormFields(FindMemberFromList( Trim$(pitem.values(i)), MAILMERGE_FIELD_DELIMITER, 2 )).Result = temp
End With
End If
End If
Next i
'Once mailmerge is complete, either print it out or save it to directory
'Call Worddoc.PrintOut
worddoc.SaveAs MERGED_DOC_SAVE_DIR_NAME & varUniqueNo(0) & ".doc"
'worddoc.SaveAs MERGED_DOC_SAVE_DIR_NAME & doc.universalid & ".doc"
Call uidoc.close
'Delete the file created in temp directory
Kill MERGED_DOC_SAVE_DIR_NAME & varUniqueNo(0) & ".doc"
This was first published in April 2002