Reg Exp
Web Design
Notes Client
Launch MS Word Document
Someone recently wanted to be able to launch a Word document that was attached to a profile. (It was a "template" that they would fill out and then attach to a new document). He tried at first to get a handle to the attachment and use the Active method to start it, but that only works for embedded OLE objects.

The solution wasn't as difficult as it could have been. My first thought was to detach the file to a temporary directory (see this document for a description of how to get the Windows Temp directory) and then shell to MS Word with a parameter of the file name which would automatically bring up the file. That's pretty close to the actual solution, but not quite.

The shell command would not have been the way to go. Generally the Word program (winword.exe) is not in a OS-searched path, so you can't just shell to "winword.exe" and get MS Word to launch. In order to shell to the program, you would have to read the registry to find the fully qualified path to the program. While that's certainly possible, it's not desirable in the least.

The easier solution is to use OLE automation to load up MS Word, then (after it's loaded), open the file that was detached earlier. This ends up being quite easy.

Here are all the variables we will need to accomplish this task:

Dim session As New NotesSession
Dim db As NotesDatabase
Dim profile As NotesDocument
Dim attachments As Variant
Dim attachment As NotesEmbeddedObject
Dim tempDir As String
Dim msWord As Variant

The "template" is stored as the only attachment in a profile document called "Settings". So the next thing to do is get a handle to that profile document:

Set db = session.CurrentDatabase
Set profile = db.GetProfileDocument("Settings")

Since there is only one attachment in that document, we can get a handle to the attachment easily without even knowing the file name:

attachments = Evaluate("@AttachmentNames", profile)
Set attachment = profile.GetAttachment(attachments(0))

The next thing to do is call the GetTempDirectory function (see this document for an overview) and then detach the file to that temp directory:

tempDir = GetTempDirectory
If Right(tempDir, 1) <> "\" Then tempDir = tempDir & "\"
Call attachment.ExtractFile(tempDir & attachment.Name)

Next, OLE automation is used to open up MS Word. If Word is already open, the existing instance is used.

On Error Goto CreateNewInstance
Set msWord = GetObject("", "Word.Application")

If Word is not already opened, then the GetObject call will raise an error and the code will jump to the CreateNewInstance statement (still to come). That block (the CreateNewInstance block) will return back to the Done block which is why that statement is in there.

The code needs to make sure that MS Word is visible to the user, and then it opens the file that was detached above:

msWord.Visible = True
msWord.documents.Open(tempDir & attachment.Name)
Exit Sub

The main block of code is done, but the error trapping block (where MS Word is loaded if it wasn't already running) needs to be defined. This block launches MS Word and then returns control back to the Done block so the file can be opened.

Print "Loading Microsoft Word.... Please Wait...."
Err = 0   ' Clear the error handler
Set msWord = CreateObject("Word.Application")
Print " "
Resume Done

Notice how this time the CreateObject method is used to launch MS Word.

That was the extent. Now the template stored in that profile document can easily be launched and the user can update the information and attach it to another document for further processing.