Cregg Hardwick is Collaboration Solutions Technical Lead for CenterPoint Energy in Houston. He has an MBA, a PCLP and an MCAD and focuses on collaboration and business process support applications, and has been a Notes developer since 1998. He has graciously agreed to answer some of the many questions that come in regarding LotusScript.
Question #1: I need to know a way to open a Word document from Lotus Shared Resources File from a Lotus document using LotusScript. Is there a way to save a Word template in the Lotus database, and use that to create a Word document on the client side using LotusScript?
Cregg Hardwick: This is one of those areas that is so frustrating to the Lotus developer! Lotus comes up with a great, sensible, useful feature and then fails to see how it fits into the overall platform! Shared resources are typical. Designed to support non-Notes resources within a Domino (web) database, they are a powerful, flexible way to provide resources to—and cut support costs for--any Notes application. Unfortunately, this apparently didn't occur to our friends at Lotus, and so they added no programmatic support for shared resources (that I have been able to find).
It is, however, possible to address a resource file through a URL, and the ?OpenFileResource URL syntax allows us to call up a file in a browser. In fact, though I haven't tried it, that syntax, in an @urlOpen formula, would probably open the file in Word. But since you are using LotusScript, you most likely want to do something more than just open the file, and for that, you need a COM reference to Word.
Since Word knows how to open files from a Web server using a URL, and Domino knows how to serve up resource files given a URL, you can use this URL syntax along with some good old-fashioned COM programming to open a resource file in Word. Of course, this will only work if the database is located on a server running the Domino HTTP task and addressable from your location and if you are using R6 or later. (There are other limitations that lead me to question the utility of this method.) But first, here is a subroutine that will do the trick:
Sub LaunchSharedWordDoc(docName As String)
Dim session As New NotesSession
Dim db As NotesDatabase
Set db = session.currentdatabase
If db.server<>"" Then
' we are on a server, let's hope it's
running the http task
Dim server As New NotesName(db.server)
('Get the common name of the server,
because we need it to build the url.)
Dim servername As String
serverName = Format(server.Common, "<")
Dim filePath As String ' Convert the file
path of the database to url syntax
filePath = Replace(db.filepath, "\", "/")
Dim url As String
(' Create url to the desired document. The shared resources are located relative to the database, so it's pretty easy to construct the url.)
(' Note: "YourDomain" is whatever you tack after
the server name to access it at your site. You could use an IP address here instead.)
Dim wApp As Variant
(' Launch Word and return an object reference to it so its COM methods can be called…)
Set wApp = createObject("Word.application")
wapp.visible=True
Call wApp.activate
wApp.documents.open(url)
. . . Call whatever methods and change whatever
properties of the Word application (wApp) you desire.
Else
Error 5101,"Shared resource files can only be
opened from a domino server running the HTTP task."
End If
End Sub
Now, this code is pretty self-explanatory. The key is that you are constructing a URL that looks something like this. If you add ?openFileResource to the end, such a URL will launch a file from the address line of a browser. Spoon-fed to Word, this URL will cause the Domino to serve the file from the Notes database into the open instance of Word (in this case, "wApp") where you can use the extensive object COM model to programatically interact with word. By the way, this will also work with Excel or any other application that exposes and API through COM.
The problem with this approach is that it won't work on a local machine or on a server not running the HTTP (Domino) task. You also have to determine what type of file you are dealing with manually. You have no easy way to access the MIME-type table in Windows to determine that, for example, Excel is supposed to open file with a .xls extension. You also have no way to present the user with a list of files to choose among or allow them to add files. . .things you might likely want to do.
Fortunately, there is a simple way to do all of these things; Just create your own "resource form" containing a rich text field and attach the files to the rich text field on documents created using this form. You can include a key on the form so the the correct resource can be looked up at runtime. Once you find the correct document, the following code will launch it and return a handle to its COM object so that you can interact with the word document through the COM API:
. . .
Dim rtitem As NotesRichTextItem
Set rtitem = doc.GetFirstItem( "Body" )
(We are assuming here that you have
guaranteed that the document will always
contain one rich text field called "Body")
Dim o As NotesEmbeddedObject
' Use Forall o In rtitem.EmbeddedObjects for
multiples (but then don't declare it)
Set o=rtitem.EmbeddedObjects(0)
Dim app as variant
Set app= o.Activate(True)
. . .
I say this will launch it. According to the documentation, this will launch it. I've found the "Activate" method to be a bit "dodgy." But in theory, this would open it using whatever application is installed on your machine that can open this file type. You can then check to see which file type it is and make appropriate method calls for that product. This is, of course, quite a bit more complex, but in theory more flexible.
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.