Problem with LS "Execute" statement and script library "Initialize" subs

I’m trying to use the LotusScript “Execute” statement to dynamically load a script library (which uses other libraries in turn) and run a Sub in the library, and getting weird behaviour with Initialize Subs.

The basic problem is any code I put in an Execute statement is running before the Initialize Subs of any script libraries used in the Execute statement. This is the opposite to what normally occurs without using Execute.

My actual script libraries have a few thousand lines, but here’s a very basic example illustrating the problem…

Script lib 1 - “BaseLib” - contains this code:

Sub Initialize

Print "Base lib Initialize"

End Sub

Script lib 2 - “SecondLib” - contains this code:

Use “BaseLib”

Sub Initialize

Print "Second lib Initialize"

End Sub

An agent contains this code:

Sub Initialize()

Execute {Use "SecondLib"

Sub Initialize

Print "Temporary module Initialize"

End Sub}

End Sub

Running the agent produces this output:

Temporary module Initialize

Base lib Initialize

Second lib Initialize

In contrast, if the agent does not use Execute, but instead directly uses “SecondLib”, the output appears as follows:

Base lib Initialize

Second lib Initialize

Agent Initialize

Is there any simple way to ensure that, when using Execute, Initialize subs are called in the same order as when not using Execute?

Subject: Why do you write an Initialize sub in your Execute argument

… instead of just putting the statements you want to execute?

Subject: Because some examples do so (incl. one in Designer Help)…

Designer Help has two examples on the page titled “Examples: Execute function and statement”.One of them has an Initialize sub in the Execute statement. The other one doesn’t, and no explanation is given for this difference.

I have actually tried my Execute statement both with and without Initialize in the Execute. The same problem occurs either way: Initialize subs in script libraries Used in the Execute run after the main body of the Execute, when they should run before.

It’s annoying, but no big deal now because I’ve developed a workaround.

For the two script libraries with their own Initialize subs, I’ve just added new custom subs to do the initialisation, and called those manually in the Execute statement.

I’ve also added a private boolean variable to each script library. I set this to true in the library’s custom initialisation sub, and that sub immediately exits if the boolean var is true at the start, so the initialisation can’t run twice.

Subject: SPR PBIT87TTPJ

SPR PBIT87TTPJ: LScript: ‘Sub Initialize’ of a Library Script runs last of all Execute(|Use “lib” …|) commandscreated on July 30 2010