Populate Word Data from Notes using LotusScript

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 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

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.