1

I am using Excel to graph financial results based on figures in a Notes database via VBA calls to the Notes server. It is effectively a realtime "update" procedure. The code starts:

Set Session = CreateObject("Lotus.NotesSession") 'Start a session to notes
Call Session.Initialize
Set nam = Session.CreateName(Session.UserName)

but I don't explicitly instruct the session to close. Should I? Can I even do that?

I fancy after a few macro calls the whole thing slows down, but that could be smth else!

And if not explicitly closing it leaves the session open, how do I reconnect to it (without logging in again) ?

Community
  • 1
  • 1
Dave45
  • 215
  • 2
  • 10
  • 2
    CreateObject returns a COM object. It will (well, *should*) "dispose" / "close" itself when all references are dropped. Because this is, uh, COM and not .CLR stuff I've found it best to manually "release" the code references (eg. when done, `Set Session = Nothing`). See http://stackoverflow.com/questions/19038350/when-should-an-excel-vba-variable-be-killed-or-set-to-nothing The effect is the same *when* the variable goes out of scope, but watch the variable lifetime. I recommend strict lifetime cleanup for *all* objects obtained in such a manner. – user2864740 Mar 13 '15 at 16:56

1 Answers1

1

The method you want is Session.Close

And yes, user2864740 is quite correct: you must explicitly set the object to Nothing.

Further, I would recommend that you close each and every every object created by Session, setting them to Nothing, too: you should never assume that any object has been dismissed and cleaned-up when it goes out of scope - and if any one of them is left alive, it can keep a reference to the parent object, keeping the session alive and inaccessible to your attempts to close it.

If you haven't explicitly closed the session, and it's still open after you've ditched every object variable that pointed to it, it's probably out of reach of VBA.

There are some objects that can be picked up by a GetObject() call if they are already running, but the Lotus Notes Session object doesn't support that interface.

Your next step would be to use API calls to enumerate the running processes, pick out the Lotus Notes Session PID by name, and kill the process

An alternative to using 'Process' API calls is WMI scripting, and MSDN have a VBScript listing for the relevant calls here:

https://msdn.microsoft.com/en-us/library/aa394599%28v=vs.85%29.aspx

This is safer than the API calls, but you should still regard this as a last resort: it will probably kill your existing mail session and there's a reason why Task Manager (your UI for enumerating processes and killing PIDs) gives you warnings about unsaved work and system instability.

Nigel Heffernan
  • 4,636
  • 37
  • 41
  • tried calling session.close from VBA - it said method not supported ! – Dave45 Mar 18 '15 at 11:57
  • Interesting: looks like you're calling a different set of Lotus Notes libraries, then.Try 'Quit' and 'Exit'. Alternatively, the object's already closed, and isn't reporting that correctly - it's raising the error 'not supported' rather than telling you that 'close' is only available on an open object. – Nigel Heffernan Mar 23 '15 at 15:46
  • A bit more work... If you go to the VBE Tools menu and declare a reference to Lotus Notes Automation Classes, the Object Browser will list all the properties and methods of the 'NotesSession' and 'NotesDatabase' classes. I am a little concerned that the 'CLOSE' method for these classes is only visible if you right-click the Members pane for 'show hidden members'. This suggests that close is no longer a supported interface for these classes, and it might not be available in later versions - although the Session.Close method is still working in the desktop environment at my current client. – Nigel Heffernan Mar 23 '15 at 15:57