IIRC, Static variables have a scope equal to the life of the
function, unless they are declared at form or application wide
level like a global variable.
Eh? Not sure what "equal to the life of the function" means. A
Static variable inside a function survives the call to the function,
which is the whole point of my using it. I initialize it once and
don't have to do so again, which with heavyweight startup processes,
like automating one of the other Office apps, or initializing a
database variable with CurrentDB, can be significant.
In the case of automating apps outside of Access, the startup
penalty can be quite large.
The reason for using them is so the variable won't reinitialize.
Eh?
Since the variable is called only once, and never reinitialized,
there is no advantage, in this particular case, to using a static
variable.
The advantage to me over a global is that it's declared directly in
the context in which it's used, and cannot be cleared except by code
operating within the function/sub in which it is declared.
At the
global or even form level, unless this function is used repeatedly
throughout the session, Excel.exe stays in memory the entire time.
There is no difference in terms of performance between the static
module-level variable and the one inside a function/sub. But in
terms of code manageability, there's a huge difference, in my
opinion.
That's
only efficient if the function is to be used in a query were it
may be called multiple times.
Bollocks. It's efficient even outside it. If you're using Outlook to
send email, you don't want to have to reload Outlook every single
time you send an email message -- the user *will* notice the
difference (assuming Outlook is not already loaded, of course). Or
Word or Excel or PDFCreator or whaterever.
Certainly, it seems to me if you're loading Excel for a function
from the Excel library, you could be very likely to use it in
contexts in which the pause to reload Excel would be quite
noticeable, even outside of a query or a loop.
On the other hand, if it's a function you use once in your app in a
component that's called only seldom, it's probably better to *not*
persist the Excel instance, because then you don't have to tear it
down when your app exits.
Also, I was typing aircode and didn't bother to clean
up or use error handling. That too would make the function better.
My comment was not intended as a criticism of what you posted.