Manage Learn to apply best practices and optimize your operations.

Create file system labels for Microsoft Excel and Word mail merges

Use LotusScript code to automatically create file system labels in a Microsoft Excel file and a Microsoft Word mail merge template that uses Avery labels.

This LotusScript code automatically creates file system labels for a Microsoft Excel file and a Microsoft Word (Avery label) mail merge template.

Related resources from
Tip: Putting an end to the frustration of mail merge

Expert Advice: Mail merge with Microsoft Word and Lotus Notes

Tutorial: 30 LotusScript tips

LotusScript Reference Center

The Microsoft Excel file was generated in a separate process, as required. Instead of going through the process again to make labels, the LotusScript code uses the process as the data source for the mail merge.

The code also consolidates various Help documents on the Web including LotusScript, VBScript, DBase coding examples and other coding types. It's useful if working with the Microsoft Windows application programming interface (API).

The label template was downloaded from The Microsoft Word "mail merge" wizard was used to create the template from a sample Microsoft Excel input file. This template was then embedded into a Lotus Notes document and referenced through a view lookup.

The key feature in this subroutine is that it can run in the background and includes the entire spreadsheet, without having to iterate through the Microsoft Excel spreadsheet row-by-row.

Sub GenerateLabelsFromXLS
MergeTemplateName As String)
On Error Goto errorhandler       
' This code creates labels 
from an Excel Spreadsheet.
On Error 213 Resume Next       
' The spreadsheet was 
created in the sub "GenerateFulfilmentXLS".
' ExcelInputFile is the location 
on the File System on this spreadsheet.
Dim MergeView As NotesView       
' LabelsOutputFile is the location 
on the File System for your merged output.
Dim MergeDoc As NotesDocument      
' MergeTemplateName is a lookup
 to an Embedded Word Merge Template.
Dim WordMailTPL As Variant  
' This will give us a handle on the 
embedded merge template
Dim wdFormLetters As Variant 
' This is a constant used by the 
MS Word application
Dim rtitem As NotesRichTextItem
Dim object As NotesEmbeddedObject

Set MergeView = db.GetView( "Label Templates" )
Set MergeDoc = MergeView.
Set rtitem = MergeDoc.GetFirstItem("Body")
Set object = rtitem.GetEmbeddedObject
("Microsoft Word Document")
Set WordMailTPL = object.Activate(False)   
' False is what starts Word 
up in the backgroud (I think).
WordMailTPL.Application.Visible = False   
' You might not need this, 
but this code seems to work and 
stays in hidden.
= wdFormLetters
Call WordMailTPL.MailMerge.OpenDataSource
( ExcelInputFile,0,False,False,True,False,"",
"",False,"","","Entire Spreadsheet")
SuppressBlankLines = True
' This is where the merge takes place
' This only closes the file in Word, 
but Word is still running.
WordMailTPL.Application.DisplayAlerts = True  
' This closes the Word.exe process

Set WordMailTPL = Nothing       
' Clears the handle on the object
 Print "Labels Completed"
 Exit Sub
Msgbox "Error " & Err() & ": " & Error(),0+64,"Error"
WordMailTPL.DisplayAlerts = True
Set WordMailTPL = Nothing
End Sub

Do you have comments on this tip? Let us know.

This tip was submitted to the tip library by member Dan Frazer. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.

Dig Deeper on LotusScript

  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...