"Value does not fall within expected range" - how to avoid

Add-in Express™ Support Service
That's what is more important than anything else

"Value does not fall within expected range" - how to avoid
ConnectTo issue? 
Michael Dye




Posts: 8
Joined: 2009-02-20
Good afternoon,
We've just deployed Outlook Security Manager and it's not working for one amongst many PCs...

We're getting:

Value does not fall within the expected range.[System.ArgumentException] in Void Connect(System.Object)

Stack Trace:
at AddinExpress.Outlook.IOutlookSecurityManager2.Connect(Object vTarget)
at AddinExpress.Outlook.SecurityManager.ConnectTo(Object outlookApp)
at <our code>.Notifier.EditEmailInOutlook(String subject, String body, Boolean htmlemail)

I've read a thread on this forum about this; and we seem to have followed the code:

AddinExpress.Outlook.SecurityManager osm = new SecurityManager();
osm.ConnectTo(Notifier._appOutlook);
osm.DisableOOMWarnings = true;

where _appOutlook is a static variable; set as follows:

if (Notifier._appOutlook == null)
{ // connect up then...
Notifier._appOutlook = new Microsoft.Office.Interop.Outlook.Application();
Notifier._onsOutlook = _appOutlook.GetNamespace("mapi");
<more code>
}

Now; the _appOutlook must have a value as it's used, and would throw an exception. But as we store it as a static variable we don't "re-get" it each time an email is created. Should we? Is this something like the user closing Outlook makes our static _appOutlook invalid? How would we check?

Help!

Thanks,

Dr. Michael Dye.
Posted 27 Feb, 2009 13:29:07 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Michael.

If the user closes Outlook created by the code above, your reference to the Outlook application becomes invalid. You need to use Marshal.GetActiveObject method to check if Outlook is already running.
Posted 27 Feb, 2009 16:26:52 Top
Michael Dye




Posts: 8
Joined: 2009-02-20
Good Evening Sergey,

Thanks for the response - yes; now it all makes sense.

When we connect to Outlook to send an email we were remembering the reference, so that we could use events on the Sent Items to log the email that was actually sent back into our application.

What we needed to do was always refresh our Outlook reference whenever sending a new email; thus ensuring it was valid (that seems much easier than trying to compare a remembered reference with a new one - couldn't work out how to do that!)

In the sent items event handler we used the Marshal.GetActiveObject approach as we know Outlook will be running (as it's called us!)

We've given that a quick test in the scenario of opening/closing Outlook whilst our application is running and it works.

Thanks,
Dr Michael Dye.
Posted 27 Feb, 2009 20:16:51 Top
Sergey Grischenko


Add-in Express team


Posts: 7187
Joined: 2004-07-05
Hi Michael.

Thanks for the update. Let me know if you need anything else.
Posted 02 Mar, 2009 09:56:22 Top