Best Practice Makes Perfect

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:

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.