To create a word document from LotusScript is quite simple, though it has some pitfalls that is caused not by LotusScript, but by the fact that working with the COM object isn't always as straightforward as working directly in any Microsoft Product.
This works for ND5, ND6, ND7 and ND8. I haven't tested on ND8.5 but there is nothing in the script that should be version dependent.
The versions of Word tested is Word 2003 and Word 2007.
The first thing You need to do is to create a COM-object to word, and that is simply done by following:
Dim objWord As VariantSet objWord = CreateObject("Word.Application")
Next step is to actually initialize the word document:
Dim objDoc As Variant
Set objDoc = objWord.Documents.Add([TemplateName])
Here is a thing: [TemplateName] should be replaced; either by "normal.dot" (the name of the default template that word uses when creating a new empty document) or by Your own template. This gives You the option of creating Your own template with all the things that should be the same on every document; headers, footers, font styles, borders etc... And that is quite nice to not having to deal with in Your script.
This will also give You two different ways to write text to Your Word document; If You specify a template, You can add Bookmarks in Your template to predefine places where You want text to be written, or if You don't use a template, text will simply be written in sequence from top to bottom, left to right.
If You have added bookmarks in the word document, You will write to them like this:
objDoc.FormFields("NameOfBookmark").Result = "My text to write here..."
If You don't use bookmarks, then simply add text like this:
objWord.Selection.InsertAfter "My text to write here..."
When all text has been written as You want it, then simply save the document.
objDoc.SaveAs([FullFilePath])
The filepath should be provided with the full name, like "C:\Temp\mytestdocument.docx".
Now, the document is saved, but the word document is still present in the memory (as well as the word application object). They has to be removed:
objDoc.Close
Set objDoc = Nothing
objWord.Quit
Set objWord = Nothing
Here is a word of warning: If You try to quit the Word application or close the Word document before You have saved it; Word will automatically ask the user if he/she wants to save the document first. If that happens; You have absolutely no control over what the user answers and where the document is saved (if it is saved). Sometimes that is ok - but If You want to process the document in Your script after creation - then don't let this happen! Make sure that You save the document.
You must also provide correct errorhandling, because if there is any error that word encounters it will throw an error and You must handle it. That might be that the Template name isn't found, the filepath where You try to save the document doesn't exists etc.
I will not describe how errorhandling is to be done - that is a completely different article...
If You can't handle the error and want to exit Your script - then make sure that You kill the objects from memory (objDoc.Close and objWord.Quit and set the variables to Nothing). Otherwise they will linger in the client memory until the computer is restared.
To handle the filepath, I have a function that creates all directories before saving the document:
Sub CreateDir(sDirectory As String)
' ==================================================================
' Purpose: To create a directory. It is created only if it doesn't exist already.
' Parameters:
' sDirectory = The name of the directory to create
' Return value: none
' ==================================================================
On Error Goto Dir_Error
Dim sPath As String
Dim iToken As Integer
Dim iLastToken As Integer
Dim x As Variant
iToken = Instr(1,sDirectory,"\")
iLastToken = iToken
Do Until iToken = 0
iToken = Instr(iLastToken+1,sDirectory,"\")
If iToken <> 0 Then
iLastToken = iToken
End If
Loop
sPath = Left$(sDirectory,iLastToken)
x = Dir$(sPath,16)
breakout:
Exit Sub
Dir_Error:
If Err = 76 Then
' The specified path does not exist - make it!
Mkdir sPath
Resume
Else
Error Err, Error$
Resume breakout
End If
End Sub
Then I also have a function to test if the file acutally exists before saving, this to avoid having Word to handle the file exists dialog with the user:
Function FileExists(sFilePath As String) As Variant
' ==================================================================
' Purpose: To check wheather a specified filename/directory exists
' Parameters:
' sFilePath = The filename/directory to investigate
' Return value:
' True - Indicates that the filename/path exists
' False - Indicates that the filename/path doesn't exist
' ==================================================================
On Error Goto File_Error
FileExists = False
Dim x As Variant
x = Dir$(sFilePath)
If x = "" Then
FileExists = False
Else
FileExists = True
End If
breakout:
Exit Function
File_Error:
FileExists = False
Resume breakout
End Function