SNTT : using LotusScript to launch file attachements (EmbeddedObjects) in Lotus Notes

I wanted to launch a excel (xls) file which was stored in a Profile document, from a Notes Client (tested in r8.5 but I see no reason this will not work in versions going back to r5)

(Making it available to all users of a certain type of document with making hundreds of replicate copies, and all a central db administrator user able to roll out a new file with out changing those other documents)

It is not very obvious how to do this, hence this posting

My first Draft was this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    Dim  workspace As New NotesUIWorkspace
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim Pdoc As NotesDocument
    Dim attachments As Variant
    Dim attachment As NotesEmbeddedObject  

    Set db = session.CurrentDatabase
    Set Pdoc = db.GetProfileDocument("ApplicationProfileDocument" ) ' get the profile document

    attachments = Evaluate("@AttachmentNames", Pdoc)
    Set attachment = Pdoc.GetAttachment(attachments(0)) 'use a evalutate to get the first "(0)" file attachment
   
    Call attachment.ExtractFile("c:" & attachment.Name)     ' write it out to the hard drive
    Set e = CreateObject("Excel.Application")                       ' open excel
    Set eWB = e.Workbooks.Open("c:" & attachment.Name)  ' open the file in excel
    eWB.Visible = True                                     'make the opened excel file visible

There was a problem the that code in that I could be sure that there weren’t other file attachments and the first one was going to be the one I wanted.

I’ve tried to correct that problem here :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    Dim  workspace As New NotesUIWorkspace
    Dim session As New NotesSession
    Dim db As NotesDatabase
    Dim Pdoc As NotesDocument
    Set db = session.CurrentDatabase
    Set Pdoc = db.GetProfileDocument("ApplicationProfileDocument" )
       
    Set rtitem = Pdoc.GetFirstItem( "RichTextFilewithFileAttacment" ) 'get the field I know the file is in.
    If ( rtitem.Type = RICHTEXT ) Then
        Forall o In rtitem.EmbeddedObjects
            If ( o.Type = EMBED_ATTACHMENT )  Then
                Call o.ExtractFile ("c:" & o.Name)      ' write it out to the c: hard drive    
                Set e = CreateObject("Excel.Application")  ' open  excel
                Set oWB = e.Workbooks.Open("c:" & o.Name)   ' open the file in excel
                oWB.Visible = True            'make the opened excel file visible
            End If
        End Forall     
    End If

This will actually launch all the files that are attached in the rich text (or rich text light) field, which I could limit by doing some validation work on the Profile form to ensure only 1 file is attached.

2 (possibly) big assumptions :

  1. This is a windows machine with a c: drive and enough free space. this is testable but I’m too lazy/busy/silly (please only pick one).
  2. The file to be launched is an Excel. This could be allow for be testing the file name extension and doing the appropriate thing. (and that assumes the MS Excel is the application for that file extension and the Open Office, or something similar).

update: Collin and Sean both make go points and reminded me of another thing : Execution Control List’s (ECL) which control access on the Notes Client. You may need to get the notes database signed with a ID that has right to write to the file system in your organization, or your end users will be prompted with a “Execution Security Alert” with will ask them to either stop the action, execute once or trust the id used.

I’ve also add related notes in : a pseudo Computed for Display Rich Text Field in the Lotus Notes Client

3 Replies to “SNTT : using LotusScript to launch file attachements (EmbeddedObjects) in Lotus Notes”

  1. Good tip. Generally best to not assume that the user is a member of “power users”+ on Windows (without this, they can’t save files to c:\). You could get the temp folder path and save the file there;

    tmpdir = Environ(“TEMP”) & “\”

    You could also use the C API to find the associated executable and launch the file that way.

  2. while probably technically ok storing attachments in profile documents gives me the creeps – I don’t like things that I can’t easily see especially if they are big things ! I would store the attachment in a traditional document.

    I would also consider extracting the file(s) and then open it allowing the OS to decide which program to use – see this tip from nsftools
    http://www.nsftools.com/tips/WinTips.htm#shellexec

    This would leave you with a more flexible tool for other applications

    Obviously if you need to control the Excel object then your method is best

    thanks for posting – it has got me thinking about profile documents again.
    Sean

Leave a Reply