Tip

Create file system labels for Microsoft Excel and Word mail merges

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 SearchDomino.com:
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 Avery.com. 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
(ExcelInputFile,LabelsOutputFile,
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.
GetDocumentByKey(MergeTemplateName,True)
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.
WordMailTPL.MailMerge.MainDocumentType 
= wdFormLetters
Call WordMailTPL.MailMerge.OpenDataSource
( ExcelInputFile,0,False,False,True,False,"",
"",False,"","","Entire Spreadsheet")
WordMailTPL.MailMerge.
SuppressBlankLines = True
WordMailTPL.MailMerge.Execute(True)   
' This is where the merge takes place
WordMailTPL.Application.ActiveDocument.
SaveAs(LabelsOutputFile)
WordMailTPL.Application.ActiveDocument.Close 
' This only closes the file in Word, 
but Word is still running.
WordMailTPL.Application.DisplayAlerts = True  
WordMailTPL.Application.Quit       
' This closes the Word.exe process

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

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

This tip was submitted to the SearchDomino.com 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.

This was first published in April 2008

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.