sacha chua :: living an awesome life

2142 blog subscribers
2745 on Twitter
Subscribe!
E-mail Feed reader

Lotus Notes mail merge from a Microsoft Excel spreadsheet

Mwahahahahaha!

I’ve been looking for ways to recognize people’s voluntary contributions to community discussions. E-mailed thanks are great because people can use the Memo-to-File feature to save it in their performance record, and copying their managers means extra kudos.

I wanted to thank 21 people for their contributions. With the Lotus Connections Communities API, a little bit of Ruby scripting and an internal tool for looking up people’s managers, I came up with a spreadsheet that listed people’s names, e-mail addresses, number of posts, first-line manager, and either “post” or “posts” depending on how many posts they had. Pulling random bits together from examples on the Net, I developed this super-nifty Lotus Notes script which does a flexible mail merge from a Microsoft Excel spreadsheet to Lotus Notes.

Create the agent and copy the following code into it. Then write an e-mail that you’ll use as the template for your new messages, and call the agent while the e-mail is selected. Give it a spreadsheet where the column headings are the tokens you’d like to replace in the template (body only). “” and “” are special – they’ll also be replaced in the mail header. The resulting mail messages will be in the “Drafts” folder so that you can customize the messages before sending them out.

This may not work with multi-line replacements or fancy formatting. Review before sending, and have fun. =)

Sub Initialize
  Dim ws As New NotesUIWorkspace
  'Prompt for the filename - should be a Microsoft Excel file
  'with columns, where the first row of each column
  'is a token that will be used when replacing text in the body of the message
  'Special tokens: <to> and <cc> set the appropriate fields

  fileName$ = ws.Prompt(12, "Select file", "3")
  If fileName$ = "" Then
    Exit Sub   'Cancel was presed
  End If
  strXLFilename = fileName$
  Dim s As New NotesSession
  Dim uidoc As NotesUIDocument
  Dim partno As String
  Dim db As NotesDatabase
  Dim view As NotesView
  Dim doc As NotesDocument
  Dim collection As NotesDocumentCollection
  Dim memo As NotesDocument
  Dim body As NotesRichTextItem
  Dim newBody As NotesRichTextItem
  Dim range As NotesRichTextRange
  Dim count As Integer

  Set db = s.CurrentDatabase
  Set collection = db.UnprocessedDocuments
  Set memo = collection.getFirstDocument()

  'Get data from the spreadsheet
  Set varXLFile = CreateObject("Excel.Application")
  varXLFile.Visible = False
  Set varXLWorkbook = Nothing
  varXLFile.Workbooks.Open strXLFilename
  Set varXLWorkbook = varXLFile.ActiveWorkbook
  Set varXLSheet = varXLWorkbook.ActiveSheet

  lngRow = 2
  While (Not (varXLSheet.Cells(lngRow, 1).Value = ""))

    'Fill in the template
    subject = memo.Subject(0)
    Set body = memo.GetFirstItem("Body")

    'Compose message
    Set maildoc = New NotesDocument(db)
    Set maildoc= db.CreateDocument()
    maildoc.Form = "Memo"
    maildoc.Subject = subject
    Set newBody = maildoc.CreateRichTextItem("Body")
    Call newBody.appendRTItem(body)
    Set range = newBody.CreateRange      

    'Look up tokens from the column headings and replace them
    columnNo = 1
    While Not(varXLSheet.Cells(1, columnNo).Value = "")
      token = varXLSheet.Cells(1, columnNo).Value
      value = varXLSheet.Cells(lngRow, columnNo).Value
      count = range.FindAndReplace(token, value, 16)
      If (token = "<to>") Then
        maildoc.SendTo = value
      End If
      If (token = "<cc>") Then
        maildoc.CopyTo = value
      End If
      columnNo = columnNo + 1
    Wend
    Call maildoc.Save(True, False)
    lngRow = lngRow +1
  Wend
End Sub

So-soHmmGoodGreatAwesome! (No Ratings Yet)
Loading ... Loading ...
Save to - del.icio.us - Digg it - reddit - StumbleUpon -

24 Responses to “Lotus Notes mail merge from a Microsoft Excel spreadsheet”

  1. OK, way to technical for me, but I am glad you figured it out!

    I take it that there was no UI-based way to do a mail merge in Notes?
    D.

  2. Doesn’t seem to be built-in, but that’s okay. Emacs Gnus didn’t have it built-in either! ;)

  3. As they like to say on The League of Super Evil: Victory. Is. Yours!

    Great work (and I am so stealing this code :) )

  4. BTW, can you provide a sample of what the email looked like (even with Lorum… text)?

  5. Just realized that the first part, where we assign personCounter etc., is completely unnecessary now that it’s all so flexible. =) Pardon the bitrot!

  6. thanks – this just saved me hours of work

  7. Hi,

    Pleased to get your blog about mailmerge using lotusnotes and XL.

    I tried your script but I couldn’t get anything there…no popup comes asking source for XL sheet when the agent runs.
    What are other settings do I need to do?
    On my system, Outlook is default mail program now.

    Please help!

  8. Hi sacha chua,

    I have solved my issue now and I am all set .

    Thanks a lot for sharing this script.

    -Ranjeet

  9. I tried your script. I will receive the mails in the draft folder but they all miss the recipient’s e-mail address. Seems I am doing something wrong here. Can you help me out please?

    Sacha

  10. I’m a Lotus Notes novice (but quite good with vb scripting)- how do I ‘Create the agent’ ?

  11. You’ll need Lotus Notes with Designer installed. If you have that and then view your mail database, you should see a Create > Agent menu item.

  12. Hi,
    I found a way of doing this from outside Lotus Notes using VB Script. I’ve posted it on Microsoft’s Script Centre :

    http://gallery.technet.microsoft.com/ScriptCenter/en-us/fe141119-9599-46a7-90ca-8dbc66d50297

    Regards
    Michael Green

  13. Thanks very much for this Sacha! :) I’ve yet to check if I do have the LN capability for this but anyhow…very useful stuff! :D

  14. Hi Sacha

    After the mail merge, the email will saved in Draft folder. Do you know a how to automate to send it one short?

    Or I have to go “edit” to click send individually?

    Thanks

    Regards
    Yuan Ling

  15. I’m seriously grateful to you for saving me hours of pain. Thanks a ton!

  16. Glad you like it! =D Took me a while to figure it out myself.

  17. Bernie:

    Sure! It looks like

    Hello, <name>! You’ve just won $1,000,000 in the <country-adjective> lottery. Claim your prize by contacting <fake-address> before <random-date>.

    <official-sounding-name>
    <official-sounding-position>
    <official-sounding-agency>


    The delimiters are totally arbitrary, as the spreadsheet just looks for the text used in the columns. You could even use non-delimited text (ex: pony), if you were careful about substrings. =) <to> and <cc> are the only ones that are special.

  18. Hello Sacha

    Many thanks for making this available. I am Lotus novice too and was greatly happy to see this. I have tried performing the task. For me, draft emails are getting created in the drafts folder, but everything is blank (To, CC, Subject, Mail Body etc.) My Excel file contains column To, CC, Subject, Body (as first row, second row onwards has data). I think I haven’t understood the “tokens” stuff you mentioned.

    Can you (or someone who reads and can) help?

    Thanks in advance

  19. Hello Sacha and others

    Refer my earlier post. I tried it little more and realized that the script works just fine. I had not included the column headers in Excel between the brackets ( ). Once included, it works just fine.

    Thanks a lot, it means much to someone like me who has recently started working on Lotus.

  20. Pankaj: You’re welcome!

    The headings are very flexible. <to> and <cc> are the only ones that are fixed, but you can use anything else for the other column headers (ex: |MESSAGE|, and so on).

    Glad you found the code useful!

  21. Hmm… What version of Notes does this work with? I get “Query is not understandable.”

  22. I do not understand what you mean by agent? Does agent refer to a new module?

    I am also getting an error that says that the user defined module is not defined (“ws as New NotesUIWorkspace.

    Thanks for posting this!

  23. Thank you for the script its perfect a part from one thing.

    We have a button enbeded into the email we want to send. When the script pushes to the drafts thats fine the button is still visable and works however once we send it out when it reaches the other end the button has been removed although the attachment we also have is still there any ideas? Its the last part and really frustrating that it only removes the button once the person gets is.

    Cheers
    Marty

  24. The script was useful! Really thank you for making it available!
    I do have a problem though – my ‘master mail’ contains the percentage % sign.
    After applying the Agent, the draft mail displays the % sign as garbage.
    Is there a way around this?

Discussion Area - Leave a Comment

Please comment as you, not your organization.





On This Day...

  • 2008: Eclipse to Emacs: Navigating your source tree — Two other things I like about the Eclipse development environment are the ability to jump to a function definition and [...]
  • 2008: Morning pages from Ottawa — In one of her books, Julia Cameron suggested writing morning pages as a way to get one’s creative juices flowing [...]
  • 2008: Emacs: Keyboard shortcuts for navigating code — One of the lines on my list of things I can do in order to make progress on my book [...]
  • 2007: A Midsummer Night’s Dream — Old readers of this blog would know how special A Midsummer Night’s Dream is to me, of all Shakespeare’s plays. [...]
  • 2007: I’m going to run away and join the circus — When I thought about what I would do if I had all the money I wanted, I realized that trying out [...]
  • 2006: Creative Thursday: Conference commando! — On the way to jazz choir practice, I thought about what I like creating and when people have called me creative. [...]
  • 2006: Planning my week with zones — I think IBM’s Think!Fridays are a great idea. It’s like zoning a day for a particular purpose. I tend to treat [...]
  • 2006: I heart ultraportables! — The power adapter for my Fujitsu Lifebook P1110 gave up two days ago. With the funny way my life works, an [...]
  • 2006: IBM CASCON 2006: Social discovery and conferences — Another thing I want to build for IBM CASCON 2006 is an easy way to create an OPML file for conference [...]
  • 2006: IBM CASCON 2006 and conference backchannels — I got so carried away making lunch that I nearly missed the planning conference call for IBM CASCON 2006’s social [...]
  • 2006: Enterprise 2.0 definition from Andrew McAfee — Via Ross Mayfield comes Andrew McAfee’s description of Enterprise 2.0: / Optional Free of up-front workflow Egalitarian, or indifferent to formal organizational identities Accepting of [...]
  • 2006: Secret knocks — Now that’s a cool application of technology. Seen on Slashdot: “Knock” Some Sense Into Your Linux Laptop describes how to [...]
  • 2006: Speaking of costumes… — Photo taken by Matthew Burpee I have no problems turning up at a high-tech geek get-together about Enterprise 2.0 wearing a [...]
  • 2006: I like someone — I like someone. There, I’ve said it. I probably shouldn’t go into much detail over here, but you can check out [...]
  • 2005: Wahoo! Bikergal! — I’ve signed up with BikeShare, a Community Bicycle Network program that’s like a library for bikes. They plan to turn Toronto into a [...]
  • 2005: Bank account needs social insurance number needs employment contract — Sorting out my finances is harder than I thought. My funding won’t kick in until September, although they’re still paying 12 [...]
  • 2004: Cognitive overload — http://icl-server.ucsd.edu/~kirsh/Articles/Overload/published.html Discovered via http://del.icio.us . I like both much.
  • 2004: What do I really want to focus on for graduate studies? — I think there’s some promise in this planner.el thing. I’m intrigued by the fact that we can make personal information managers [...]
  • 2004: Migration guide for Microsoft Windows to Linux — http://www.linuxgazette.com/node/view/9187 http://www.kbst.bund.de/Anlage304428/Migration_Guide.pdf William Villanueva said: The German Government, specifically the “Bundesministerium des Inneren” (Ministry of Interior Affairs) has published a 444 page Migration-Guide that covers [...]
  • 2003: Configuring SSH behind a proxy — http://www.webweavertech.com/ovidiu/weblog/archives/000017.html
  • 2003: The importance of mentoring — http://www.artima.com/weblogs/viewpost.jsp?thread=7600