Skip to main content
    Country/region select      Terms of use
     Home      Products      Services & solutions      Support & downloads      My account     

developerWorks  >  Lotus  >  Forums & community  >  Best Practice Makes Perfect

Best Practice Makes Perfect

A collaboration with Domino developers about how to do it and how to get it right in Domino

I've been poking around in a lot of applications recently, and I see that people like to use the statement:

Set uidoc = wksp.CurrentDocument

in form events, especially Postopen, as a way of retrieving a handle to the document associated with that event code.

This is a bad idea. CurrentDocument actually returns the document from the window that has focus. In a lot of cases, that's the same thing, but not necessarily. In composite applications, in framesets, or when using an embedded editor, the window with focus may contain a different document -- or no document. The NotesUIWorkspace object is context-neutral as regards where the code that calls it is running, so there's only one "current" document at any given time (or sometimes, none). When different documents open in different frames or components, they can't all be the "current" document at the same time.

Instead, as a best practice, use the Source As NotesUIDocument calling argument to the event handler function. So instead of:

Sub Postopen(Source As Notesuidocument)
        Dim wksp As NotesUIWorkspace
        Dim uidoc As NotesUIDocument
        Dim doc As NotesDocument
        Set uidoc = wksp.CurrentDocument
        Set doc = uidoc.Document
End Sub

you would write:

Sub Postopen(Source As Notesuidocument)
        Dim doc As NotesDocument
        Set doc = Source.Document
End Sub

which is not only more reliable, but also shorter.

Even if the CurrentDocument property works in your specific situation, you can't control how other people will use your form in composite applications, where the document isn't opened in its own window and so, might not have focus.

In certain cases, it makes sense to define a global variable "Dim Guidoc As NotesUIDocument," assign it during the Queryopen event (Set Guidoc = Source) and refer to that in places in the code where you'd like to know the current document but can't assume your window has focus. The Initialize events of fields, for instance, or the event subroutine for a NotesTimer. In fact, NotesTimer events often need to take advantage of the behavior of CurrentDocument to test whether "their" window has focus by comparing it to Guidoc.

BTW: It's okay to use CurrentDocument in action buttons and other contexts where the code can't be activated unless the document has focus.

Andre Guirard | 15 July 2009 08:00:00 AM ET | Home, Plymouth, MN, USA | Comments (1)


1) Strange
Henning Heinz | 7/15/2009 8:43:52 AM

That is very very strange. When you recently asked for suggestions for the Designer Wiki this was just what I would have asked. I did not because I was afraid that this was a very dumb question. So thank you for taking the time to read my mind and sort this out. I am really baffled.

2) Actually, the fun part is dialog boxes
Nathan T. Freeman | 7/15/2009 9:12:50 AM

If you're in a dialog box, Source is the document presented in the dialog, while CurrentDocument is the document that had focus at the time of the DialogBox call.

This, of course, turns out to be ENORMOUSLY useful.

3) CurrentDocument in buttons
Dietmar Höhmann | 10/11/2011 2:03:17 AM

CurrentDocument is not generally save in buttons, because buttons may be "clicked" by JavaScript without the document having focus. But as the Click event does not get the document as a parameter, we're forced to save the source document to a global variable in QueryOpen event.

4) Very useful in Composite Applications
Heinrich Mueller | 8/16/2012 8:42:30 AM

Here focus often changes between the components

so the definition of

Set uidoc=Source in the QueryOpen event and

Global Declaration of uidoc did the trick.

 Add a Comment
Comment:  (No HTML - Links will be converted if prefixed http://)
Remember Me?     Cancel

Search this blog 


    About IBM Privacy Contact