Unable to enumerate Mailboxes in NON Cached Mode (if more than 2)

Status
Not open for further replies.
S

stefanom74

I don't know if it is a bug, but writing this code in a VBA Macro something

strange happens:

If I have mailboxes in my Outlook 2007 in Cached Mode everything works fine,

but if I'm not in Cached Mode I receive an "Automation Error", any idea?

Dim store As store

For Each store In Application.Session.Stores

MsgBox (store.DisplayName)

Next
 
This is running in the Outlook VBA project? Does it help if you get a

NameSpace object and use that?

Dim oNS As Outlook.NameSpace

Set oNS = Application.GetNameSpace("MAPI")

For each store In oNS.Stores

Also, step your code and see where it's failing. Do you get a store object?

How many oNS.Stores are there?

"stefanom74" <stefanom74> wrote in message

news:B9CC5CF4-7AF5-49D5-A860-72B816129539@microsoft.com...
> I don't know if it is a bug, but writing this code in a VBA Macro something
> strange happens:
> If I have mailboxes in my Outlook 2007 in Cached Mode everything works
> fine,
> but if I'm not in Cached Mode I receive an "Automation Error", any idea?

> Dim store As store

> For Each store In Application.Session.Stores

> MsgBox (store.DisplayName)

> Next

>
 
Re: Unable to enumerate Mailboxes in NON Cached Mode

Hi Ken,

Thanks for the reply.

I found the problem !

I tried your code but it returns the same Automation Error (on "Next"

statement), then i tried to change the code from "for each" to a simple (for

index=1 to oNS.Count) ... and here it is !!! The "real" error appeared

"Unable to connect Exchange...." while accessing to the DisplayName store's

property (not on "Next i" statement). The error was on the "Public Folders"

that is enumerated as store too!

An error handling inside the "For" will probably "solve" the problem.

(but why the "For Each" statement doesn't have the same behavior?)

This is the update code:

Sub TestBug()

Dim store As store

Dim oNS As Outlook.NameSpace

Set oNS = Application.GetNamespace("MAPI")

MsgBox (oNS.Stores.Count) '<- this returns 3

For i = 1 To oNS.Stores.Count

MsgBox (oNS.Stores(i).DisplayName) ' <- this returns Error on i = 2

Next i

End Sub

Regards,

Stefano
wrote:


> This is running in the Outlook VBA project? Does it help if you get a
> NameSpace object and use that?

> Dim oNS As Outlook.NameSpace
> Set oNS = Application.GetNameSpace("MAPI")
> For each store In oNS.Stores

> Also, step your code and see where it's failing. Do you get a store object?
> How many oNS.Stores are there?

> >

>

> "stefanom74" <stefanom74> wrote in message
> news:B9CC5CF4-7AF5-49D5-A860-72B816129539@microsoft.com...
> >I don't know if it is a bug, but writing this code in a VBA Macro something
> > strange happens:
> > If I have mailboxes in my Outlook 2007 in Cached Mode everything works
> > fine,
> > but if I'm not in Cached Mode I receive an "Automation Error", any idea?
> > Dim store As store
> > For Each store In Application.Session.Stores
> > MsgBox (store.DisplayName)
> > Next
> >


>
 
Re: Unable to enumerate Mailboxes in NON Cached Mode

I tested both code methods (with the For...Each and For loops) here on

Outlook 2007 in both online and cached modes and ran the code with no

errors. I got the display name for public folders with no errors.

Let's try a couple of tests.

First, when you iterate the stores get each one as a Store object. Make sure

you can do that with the public folders store and as a Store object try to

access DisplayName. Do that instead of using those compound dot operators

(oNS.Stores(i).DisplayName). They make it hard to see exactly where the

error is occurring.

Second, try getting the All Public Folders folder using

oNS.GetDefaultFolder(olPublicFoldersAllPublicFolders) and see if you get a

valid Folder object and if you can access its properties such as Name.

I'm amazed that this is happening in online mode, if anything I'd expect

some problem with cached mode rather than online mode.

"stefanom74" <stefanom74> wrote in message

news:93170509-8FFF-4085-B6B6-DD8C7ACD2D93@microsoft.com...
> Hi Ken,

> Thanks for the reply.

> I found the problem !

> I tried your code but it returns the same Automation Error (on "Next"
> statement), then i tried to change the code from "for each" to a simple
> (for
> index=1 to oNS.Count) ... and here it is !!! The "real" error appeared
> "Unable to connect Exchange...." while accessing to the DisplayName
> store's
> property (not on "Next i" statement). The error was on the "Public
> Folders"
> that is enumerated as store too!
> An error handling inside the "For" will probably "solve" the problem.
> (but why the "For Each" statement doesn't have the same behavior?)

> This is the update code:

> Sub TestBug()
> Dim store As store
> Dim oNS As Outlook.NameSpace
> Set oNS = Application.GetNamespace("MAPI")
> MsgBox (oNS.Stores.Count) '<- this returns 3
> For i = 1 To oNS.Stores.Count
> MsgBox (oNS.Stores(i).DisplayName) ' <- this returns Error on i =
> 2
> Next i
> End Sub

> Regards,
> Stefano
 
Re: Unable to enumerate Mailboxes in NON Cached Mode

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
wrote:


> I tested both code methods (with the For...Each and For loops) here on
> Outlook 2007 in both online and cached modes and ran the code with no
> errors. I got the display name for public folders with no errors.

> Let's try a couple of tests.

> First, when you iterate the stores get each one as a Store object. Make sure
> you can do that with the public folders store and as a Store object try to
> access DisplayName. Do that instead of using those compound dot operators
> (oNS.Stores(i).DisplayName). They make it hard to see exactly where the
> error is occurring.

> Second, try getting the All Public Folders folder using
> oNS.GetDefaultFolder(olPublicFoldersAllPublicFolders) and see if you get a
> valid Folder object and if you can access its properties such as Name.

> I'm amazed that this is happening in online mode, if anything I'd expect
> some problem with cached mode rather than online mode.

> >

>

> "stefanom74" <stefanom74> wrote in message
> news:93170509-8FFF-4085-B6B6-DD8C7ACD2D93@microsoft.com...
> > Hi Ken,
> > Thanks for the reply.
> > I found the problem !
> > I tried your code but it returns the same Automation Error (on "Next"
> > statement), then i tried to change the code from "for each" to a simple
> > (for
> > index=1 to oNS.Count) ... and here it is !!! The "real" error appeared
> > "Unable to connect Exchange...." while accessing to the DisplayName
> > store's
> > property (not on "Next i" statement). The error was on the "Public
> > Folders"
> > that is enumerated as store too!
> > An error handling inside the "For" will probably "solve" the problem.
> > (but why the "For Each" statement doesn't have the same behavior?)
> > This is the update code:
> > Sub TestBug()
> > Dim store As store
> > Dim oNS As Outlook.NameSpace
> > Set oNS = Application.GetNamespace("MAPI")
> > MsgBox (oNS.Stores.Count) '<- this returns 3
> > For i = 1 To oNS.Stores.Count
> > MsgBox (oNS.Stores(i).DisplayName) ' <- this returns Error on i =
> > 2
> > Next i
> > End Sub
> > Regards,
> > Stefano


>
 
Re: Unable to enumerate Mailboxes in NON Cached Mode

Hi Ken,

Forget my last post... about the Stores[] ... it is a collection of

object... but starting from index = 1 so just solved my problem with error

handling on each Store item index.

Thanks,

Regards,

Stefano
wrote:


> I tested both code methods (with the For...Each and For loops) here on
> Outlook 2007 in both online and cached modes and ran the code with no
> errors. I got the display name for public folders with no errors.

> Let's try a couple of tests.

> First, when you iterate the stores get each one as a Store object. Make sure
> you can do that with the public folders store and as a Store object try to
> access DisplayName. Do that instead of using those compound dot operators
> (oNS.Stores(i).DisplayName). They make it hard to see exactly where the
> error is occurring.

> Second, try getting the All Public Folders folder using
> oNS.GetDefaultFolder(olPublicFoldersAllPublicFolders) and see if you get a
> valid Folder object and if you can access its properties such as Name.

> I'm amazed that this is happening in online mode, if anything I'd expect
> some problem with cached mode rather than online mode.

> >

>

> "stefanom74" <stefanom74> wrote in message
> news:93170509-8FFF-4085-B6B6-DD8C7ACD2D93@microsoft.com...
> > Hi Ken,
> > Thanks for the reply.
> > I found the problem !
> > I tried your code but it returns the same Automation Error (on "Next"
> > statement), then i tried to change the code from "for each" to a simple
> > (for
> > index=1 to oNS.Count) ... and here it is !!! The "real" error appeared
> > "Unable to connect Exchange...." while accessing to the DisplayName
> > store's
> > property (not on "Next i" statement). The error was on the "Public
> > Folders"
> > that is enumerated as store too!
> > An error handling inside the "For" will probably "solve" the problem.
> > (but why the "For Each" statement doesn't have the same behavior?)
> > This is the update code:
> > Sub TestBug()
> > Dim store As store
> > Dim oNS As Outlook.NameSpace
> > Set oNS = Application.GetNamespace("MAPI")
> > MsgBox (oNS.Stores.Count) '<- this returns 3
> > For i = 1 To oNS.Stores.Count
> > MsgBox (oNS.Stores(i).DisplayName) ' <- this returns Error on i =
> > 2
> > Next i
> > End Sub
> > Regards,
> > Stefano


>
 
Re: Unable to enumerate Mailboxes in NON Cached Mode

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" <stefanom74> wrote in message

news:D07E7CA3-C937-4BDD-87B1-DDD959E49019@microsoft.com...
> 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
 
Re: Unable to enumerate Mailboxes in NON Cached Mode

Almost all or all Outlook collections start at 1, not 0.

"stefanom74" <stefanom74> wrote in message

news:4A802E31-F8CE-4521-BEB8-E23CF62A69C5@microsoft.com...
> Hi Ken,

> Forget my last post... about the Stores[] ... it is a collection of
> object... but starting from index = 1 so just solved my problem with error
> handling on each Store item index.

> Thanks,
> Regards,
> Stefano
 
Re: Unable to enumerate Mailboxes in NON Cached Mode

Hi Ken,

Thank you!!

It has been very helpful!!!!! I never thought about RCP channels !? I'll

take care of it and all the other tips !!!

Thanks

Regards,

Stefano
wrote:


> 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" <stefanom74> wrote in message
> news:D07E7CA3-C937-4BDD-87B1-DDD959E49019@microsoft.com...
> > 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


>
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
D Unable to change AppointmentItem.Start property Outlook VBA and Custom Forms 3
D VBA - unable to set rule condition 'on this computer only' Outlook VBA and Custom Forms 5
L Unable to Use Alias as From Address Using Outlook 2
R unable to filter tasks when start date is on or before today Using Outlook 3
J Unable to delete folders in Outlook 2019 / Windows Using Outlook 0
D Unable to view older emails in desktop app Using Outlook 0
M Issues with templates - unable to find in old web app Using Outlook 3
Commodore Unable to move message Using Outlook 3
S Unable to change Message Class Outlook VBA and Custom Forms 0
O Unable to make changes to an existing Calendar entry Using Outlook 11
C Outlook 2016 Unable to delete Gmail IMAP calendar Using Outlook 2
S Unable to extract text from an Outlook email message Using Outlook 2
B Outlook 2016 Unable to view images or logos on the outlook 2016 emails the same html code works well when i use outlook 2010 Using Outlook 0
M Unable to share 2019 calendar Using Outlook 0
D Outlook 2016 Unable to load Outlook data pst file Using Outlook 5
L Unable to Sync Web/Android MS To Do with Windows Outlook Tasks Using Outlook 3
A Unable to save recurring Meeting to Documents folder due to error Using Outlook 2
O Outlook 365 - suddenly unable to send using Gmail POP3 Using Outlook 10
S Outlook 2010 unable to change default font Using Outlook 7
Q Unable to Sync Quicken reminder with Outlook 2016 64Bit Using Outlook 1
S Unable to remove rule outlook 2010 Using Outlook 0
L Wierd Office 365 Contact unable to edit body of random contacts Using Outlook 5
X Unable to send an email from one account to another on same PC Using Outlook 2
Mark Foley Unable to subscribe to published calendar in Outlook 2010 Using Outlook 4
S Unable to Edit Contact Information in Certain Contact Folders Using Outlook 3
P Deleted Items - Unable to Iterate All of Items Outlook VBA and Custom Forms 1
G Unable to dismiss reminders from share point list calendar shared in Outlook Using Outlook 2
R Unable to install without email account Using Outlook 4
E Unable to open Outlook 2010 after adding new email account Using Outlook 4
A Outlook 2016- unable to have all subfolders expanded when opening outlook Using Outlook 11
avant-guvnor Unable to view certain emails in Outlook 2016 Using Outlook 16
M Unable to Configure Gmail Account in Outlook 2007 Using Outlook 1
D Unable to Send On Behalf of Another User Exchange Server Administration 0
J Unable to link email messages in BCM using a single microsoft office 365 account in outlook 2013 BCM (Business Contact Manager) 1
Tim King Outlook 365 unable to change accounts Using Outlook 0
R Unable to send mail using Cox.net Using Outlook 1
T Unable to 'Upload a file' using popup 'Browse' button Using Outlook 0
D Unable to add email address to contact Using Outlook 3
G Outlook calendar entry corrupted. Constant pop up when you open outlook. Unable to delete or remove. Using Outlook 2
O OL2000: Unable to create IMAP account Using Outlook 2
wallisellener Unable to reply/post/create new thread using Chrome BCM (Business Contact Manager) 5
Rupert Dragwater unable to change font sizes in some replies Using Outlook 3
C Unable to see meeting attendees Outlook 2010 Using Outlook 5
M Unable to email from Word or Excel Using Outlook 11
O Unable to check name. Using Outlook 3
T Unable to create contacts subfolder in EAS profile Using Outlook.com accounts in Outlook 6
UncleBill Unable to delete items from gmail IMAP Trash using Outlook 2010 Outlook VBA and Custom Forms 5
B Unable to search delegated mailfile Using Outlook 3
K Unable to activate QueryBuilder in Outlook 2010 (32bit) with Windows 7 (64bit) Using Outlook 1
C Unable to forward email with URL Using Outlook 2

Similar threads

Back
Top