Outlook.Stores stores = oNS.Stores;
int iCount = stores.Count;
Outlook.Store store = null;
for (int i = 1; i <= iCount; i++)
{
store = stores
;
if (store != null)
{
// working code here
}
}
That way you avoid the multiple dot operator problem and explicitly
instantiate each object separately so it can be checked. With judicious use
of try...catch blocks you can then trap any exceptions and see what went
wrong where.
I often try to avoid foreach loops although they are easier to write. They
are slower than explicit for loops and in addition they create internal
object variables that cannot be released explicitly. That ends up causing
resource problems, especially in loops with Exchange. In Exchange you are by
default limited to under 256 RPC channels and each object variable counts as
one RPC channel. So lots of those internally created variables that can't be
released leads to running out of channels.
With limits on dot operators and careful use of where you declare objects
you can then release your objects explicitly in the loop if needed
(Marshal.ReleaseComObject() and GC.Collect()). Notice I declared the store
obejct outside the loop so only 1 instance is created.
That may not apply in this case with only a few stores, but it's good
practice to get into that habit. And the explicit instantiations make it
easier to debug since you don't have to guess which part of a multiple dot
operator line is causing the problem.
stefanom74 said:
Hi Ken,
Thanks a lot for you interest.
I tried to get All Pubilc Folders folder and i got the same Error:
"Microsoft Exchange is not available. Either there are network problems or
the Exchange computer is down for maintenance".
At this point I think this is a problem of my Exchange 2007 installation
...
I will try reinstalling the Dev environment and will let you know if got
fixed ...
... but anyway ... since this "problem" may occur when Exchange is not
available (i know it is strange in NON Cached Mode) how can i solve the
foreach problem!?
Since everything is started developing a VSTO 3SP1 c# Outlook Addin while
I
was trying to enumerates Stores ... and Application.Session.Stores is not
an
array of Outlook.Store but it is IEnumerable and can be accessed just with
foreach (or equivalent) ... how can I enumerate all Stores with a for (int
i=0;....) in order to put error handling on each item ?!?
Thanks!
Regards,
Stefano