Script to Populate Word Document with Text from a Notes Document

Author: Tripp W Black

Created: 08/13/2001 at 10:53 AM

 

Category:
Notes Developer Tips
Agents, COM

Example code of Word doc population from Notes Document.

Here code. Runs backend on Notes and requires Word doc template below in the root of c: and a c:\temp folder.
RTLNUG_Announcement.dot

Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim doc As NotesDocument
Dim ErrorMessage As String

Dim maildoc As NotesDocument
Dim bodytext As String
Dim dateTime As NotesDateTime
Dim rtitem As NotesRichTextItem
Dim sumitem As NotesItem
Dim plaintext As String
Dim sendto As String
Dim object As NotesEmbeddedObject
Dim WordObj As Variant
Dim WordDoc As Variant
Dim intUsersCheckSpellingAsYouType As Variant ' This way it can be 0,-1 or False,True
Dim intUsersCheckGrammarAsYouType As Variant
Dim intUsersPagination As Variant
Dim intUsersWindowViewType As Variant
Dim bookmark, text As String
Dim i As Long

' setup the environment
Set db = session.CurrentDatabase
Set view = db.GetView("CalendarNextMeetingLookup")
Set doc = view.GetFirstDocument

' if no docs then exit sub
If doc Is Nothing Then
Exit Sub
End If
' else process the 1st document
' create the word document annoucement off the original document and mail it to each person

' setup the word doc
Set WordObj = Createobject("Word.Application")
' you can have Word do the processing in the background
' by setting the property to FALSE.
WordObj.visible = True
' This is the name of the Word template that the be used
Call WordObj.Documents.Add ("C:\RTLNUG_Announcement.dot")
ErrorMessage = "Unable to activate VisualBasic in Microsoft Word."
Set WordDoc = WordObj.ActiveDocument

' Backup user's settings on MSWord
intUsersCheckSpellingAsYouType = WordObj.Options.CheckSpellingAsYouType
intUsersCheckGrammarAsYouType = WordObj.Options.CheckGrammarAsYouType
intUsersWindowViewType = WordObj.ActiveWindow.View.Type
' Remove MSWord's "slow" options
WordObj.Options.CheckSpellingAsYouType = False
WordObj.Options.CheckGrammarAsYouType = False
' Remove MSWord background repagination too
' (but you'll have to switch to wdNormalView Window type first, which equals to 1 by the way...)
WordObj.ActiveWindow.View.Type = 1 ' wdNormalView
intUsersPagination = WordObj.Options.Pagination
WordObj.Options.Pagination = False

' add the next event data to the word doc using the bookmarks in the template

WordDoc.Fields.Update
ErrorMessage = "Unable to get a bookmark."
' cycle through bookmarks adding new content
For i = 1 To WordDoc.Bookmarks.Count
With WordDoc.Bookmarks(i)
' If .Empty Then Goto next_i
bookmark = .Name
Select Case bookmark
Case "RSVPLink" : text = "http://www.rtlnug.org/RTLNUG.nsf/Calendar/" & doc.UniversalID
Case "VendorName" : text = doc.VendorName(0)
Case "MtgDate" : text = doc.DateAndTime(0)
Case "BodyText" :
Set sumitem = doc.GetFirstItem( "Comments" )
text = sumitem.Text
Case Else : Goto next_i
End Select

With .Range
If text = "" Then Goto next_i
.Delete
.InsertAfter(text)
WordDoc.Bookmarks.Add bookmark, WordDoc.Range(.Start, .End)
End With
End With
next_i:
Next

' WordDoc.Bookmarks("RSVPLink").Select
' WordDoc.Application.Selection.TypeText "http://www.rtlnug.org/RTLNUG.nsf/Calendar/" & doc.UniversalID
' WordDoc.Bookmarks("VendorName")
' WordDoc.Application.Selection.TypeText doc.VendorName(0)
' WordDoc.Bookmarks("MtgDate")
' WordDoc.Application.Selection.TypeText doc.DateAndTime(0)
' get body text of the comments field
' Set sumitem = doc.GetFirstItem( "Comments" )
' plaintext = sumitem.Text
' WordObj.Selection.GoTo.Bookmarks("Body")
'WordObj.Selection.TypeText (plaintext)

' At the very end, restore user's settings on MSWord
WordObj.Options.CheckSpellingAsYouType = intUsersCheckSpellingAsYouType
WordObj.Options.CheckGrammarAsYouType = intUsersCheckGrammarAsYouType
WordObj.Options.Pagination = intUsersPagination
WordObj.ActiveWindow.View.Type = intUsersWindowViewType
' Now we are done, lets save and close the word doc
WordObj.ActiveDocument.SaveAs ("C:\temp\meeting.doc")
WordObj.ActiveDocument.Close
Set WordObj = Nothing

' maildoc is the memo to which we are going to attach the word doc
Set maildoc = New NotesDocument(db)
maildoc.Form = "Memo"
' create the subject line
maildoc.Subject = doc.Title(0) & " - Meeting Announcement, RTLNUG"
' create the body field and intro text
Set rtitem = New NotesRichTextItem( maildoc, "Body" )
Call rtitem.AppendText _
("We invite you to the upcoming Lotus Notes Users Group Meeting on " & doc.DateAndTime(0) & ".")
Call rtitem.AddNewLine( 1 )
Call rtitem.AppendText _
("Please feel free to also bring a guest. You may RSVP for you and any guests using the URL below.")
Call rtitem.AddNewLine( 1 )
Call rtitem.AppendText _
("http://www.rtlnug.org/RTLNUG.nsf/Calendar/" & doc.UniversalID)
Call rtitem.AddNewLine( 2 )
Call rtitem.AppendText _
("Attached is also a Word document containing this announcement. Please detach it and post in your workplace to help spread the word.")
Call rtitem.AddNewLine( 2 )
Call rtitem.AppendText _
("Look forward to seeing you at the meeting.")
Call rtitem.AddNewLine( 1 )
Call rtitem.AppendText _
("Santiago")
Call rtitem.AddNewLine( 1 )
Call rtitem.AppendText _
("santiago@adteco.com")
Call rtitem.AddNewLine( 2 )
' attach the file
Set object = rtitem.EmbedObject _
( EMBED_ATTACHMENT, "", "C:\temp\meeting.doc")

' address and send the new document
' maildoc.BlindCopyTo = doc.MailAddress(0)
maildoc.SendTo = "Santiago Hernandez"
maildoc.From = "RSVP@RTLNUG.org"
maildoc.Principal = "RSVP@RTLNUG.org"
maildoc.ReplyTo = "RSVP@RTLNUG.org"
Call maildoc.Send(False, maildoc.SendTo)

Exit Sub

ErrorHandler:
Messagebox ErrorMessage
Exit Sub
End Sub

previous page