« CipherTrust TrustedSource Toolbar - for Lotus Notes | Main| Blogsphere 2.5.1 is available - get it. »

SnT2: Launch First Attachment Revisited

   
Category
Bookmark : del.icio.us  Technorati  Digg This  Add To Furl  Add To YahooMyWeb  Add To Reddit  Add To NewsVine 

Last year I wrote an entry about some code I wrote that replaced the Notes feature known as "launch first attachment". I explained the technique I used to Bob Balaban the other day, and he thought it was another "righteous hack", and he suggested I use it for a Show-n-Tell Thursday entry. I remembered I had written it up before, but since that original writeup I have enhanced it a bit, and I have also created an example database to demonstrate it; therefore I think it is time to cover it again.

When designing a form you have the ability to set its launch properties - you can decide what happens when the form is used to open a document. One of the choices available is "First Attachment", as shown in the figure below:

form launch properties

When First Attachment is chosen then the first attachment in the document is opened instead of the form when a document using the form is opened. If the document is opened in Edit mode from the view (such as by selecting the document and hitting CTRL-E) then the document is opened with the form in edit mode (handy for changing the attachment, or changing other field values, etc.). This works great for applications that contain such things as Excel spreadsheets, reports, etc. - things where the "important" information is contained in the attachment, and the database is simply a repository. However, there is a bug with this feature - and it is a bug that has been around for quite some time.

The problem is that certain programs, such as MS Excel and Word, launch but don't maintain focus; while others, such as MS Powerpoint, do launch with focus. The real aggravation with this bug is for users who use Notes open full-screen, and have their taskbars hidden. In this scenario the user will double-click the document to open the attachment, and to them, nothing happens. So, they double-click it again. And again. And then they eventually call support to ask what's going on, only to find out they have opened the same attachment 35 times in the background, all because the attachment didn't have focus when opened (and therefore the user didn't know it was opening). This problem has been around for quite some time (as noted in Technote 1091159), with no plans to fix. But there is a way to give your users the convenience of this feature without the annoying focus problem - and here's how.

First, I recommend you download the example database from here. Got it? Good.

We begin by taking a look at the example form (LaunchFirstAttach) used to contain an attachment for testing, as well as my example code in the QueryOpen event. This is the "meat" of what's in the QueryOpen event - there is some error trapping, etc. as well.
REM I only want to launch the first attachment if the doc isn't new, REM and if it isn't being edited (like the notes feature) If Source.IsNewDoc = False And Source.EditMode = False Then Set doc = Source.Document retflg = detachFileOpen(doc, "Attach") ' from LS.utilities.FE.6 Continue = False End If

This code checks to make sure this isn't a new document and to make sure it isn't being opened it edit mode; if this is an existing document being opened in read mode, then the detachFileOpen function is called, which lives in the LS.utilities.FE.6 library.
Function detachFileOpen(doc As NotesDocument, rtname As String) As Variant On Error Goto errHandler detachFileOpen = True Dim ws As New NotesUIWorkspace Dim rtitem As NotesRichTextItem Dim filepath As String, filename As String Dim retflg As Boolean, res As Long, res2 As Integer Dim exepath As String, shellpath As String exepath = String(MAX_FILENAME_LEN, 32) retflg = detachFile(doc, rtname, filename, filepath) REM determine if the file is an EXE - if it is, we just want to launch it If Lcase(Right(Fulltrim(filepath), 3)) = "exe" Then res2 = Shell(filepath, 1) Exit Function End If REM use the C API call to find the associated executable res = FindExecutable(filepath, "", exepath) If res <= 32 Then Error 1000, "Problem with FindExecutable call." exepath = Fulltrim(Left$(exepath, Instr(exepath, Chr$(0)) - 1)) ' remove trailing spaces REM can't launch a DLL If Ucase$(Strrightback(exepath, ".")) = "DLL" Then Error 1001, "Unable to locate associated program" If exepath = "" Then Error 1001, "Unable to locate associated program" shellpath = exepath & | "| & filepath & |"| REM open the file with the associated executable REM the "1" param tells it to launch with focus res2 = Shell(shellpath, 1) getOut: Exit Function errHandler: detachFileOpen = False Select Case Err Case 1001 Msgbox Error$, 48, "Error" Case Else On Error Goto 0 Error Err, Error$ & " (" & Err & ") [in " & Lsi_info(2) & "]" End Select Resume getOut End Function

This function first uses the detachFile function (that I have described in other posts, so I won't cover it here) from the LS.utilities.BE.6 library to get the file on the local drive. Then the extension is checked - if it is an EXE then it is automatically launched. Next the FindExecutable function is used to determine the program executable associated with this file type in Windows. This is a C API function defined in the (Declarations) that works great for determining the appropriate executable to use with the type of file being launched.

Next the extension of the executable path (exepath) is checked to see if it is a DLL; if it is, we can't launch the file.

If everything checks out the Shell function is used to launch the file with the associated program. There are two things to notice about this call. First, I am using the format of executable path "file path" for the string being provided to Shell. Windows programs will automatically launch a file if it is provided as a parameter to the program's executable. The second thing is the second parameter of "1". It turns out that if the second parameter is 1 then the Shell program will launch the program with focus - which is what we wanted all along.

I believe that I am basically doing the same thing the launch first attachment feature does under the hood, except for the 1 parameter on the Shell call. After extensive testing it appears that my version fails in the same places the feature does, and works with all of the files for which the feature works.

Comments

1 - Hi Almar - I tried to duplicate the problem with an AVI and a MOV file, and couldn't duplicate. What type of file should I try? If I can narrow down the exact parameters for the cause, I can add some conditional code to handle it.

Also, can you test this with the actual Launch First Attachment function as well? Simply comment out the small bit of code in the QueryOpen, and enable the feature on the Launch properties tab. I'm curious if it occurs with the "real" feature as well.

Thanks for the kind words, too!

Rock

2 - I found this via a trackback - good stuff. And here's the manual trackback for those interested in an additional scenario where this was applied, and helped beat out a MS product.

{ Link }

3 - Hi! Great Stuff!
I find it much more comfortable now using the MsOfficeLibrary.

By using Shell Commands this only works for Windows machines, right? On a Mac this is still not possible?

Thx
Tom

4 - Hi Rocky,

thanks, this is great stuff again. One minor problem. It looks like your code (or my PC ) is too fast. If I open a document with a video file MediaPlayer start, but doesn't show the movie. Opening the same document with the debugger on and stepping through does show the movie.

It seems like Mediaplayer is starting before the detach is ready (putting a sleep 10 after the detach also makes it work).

Meet Rocky

Rocky Oliver
Rocky Oliver
If you see me at a conference, please stop me and say hi!

Calendar

Search

Categories

LotusGeek Tour 2008

DNUG08-2.png

Proudly Employed By

I am the Vice President of Products for TeamStudio

Our Corporate Blog

I am the Vice President of Products for TeamStudio

Thawte Notary

Thawte Web of Trust Notary

LOTUS GEEK gear

Social Networking


Add to Technorati Favorites

View Rocky Oliver's profile on LinkedIn

Rocky  Oliver

LotusGeek Blog Roll

Why display a blog roll when Planet Lotus does it so much better?

Dilbert

Buy my book!

Blog Buttons

Atheist - Unitarian - Humanist

Atheist Symbol

chalice_150.gif

Happy Humanist

Poker Players Alliance

This Site Designed By

YOU! If you would like to see your name and link here, read more about the Skin the Geek contest!