This LotusScript code automatically creates file system labels for a Microsoft Excel file and a Microsoft Word (Avery label) mail merge template.
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.