SyncObjects.Item Method (Outlook), explain

-1

This link: https://msdn.microsoft.com/en-us/vba/outlook-vba/articles/syncobjects-item-method-outlook is devoid of explanations. What is the meaning of SyncObjects.Item(1), for example? Can I have a practical example? SyncObjects.Item(1) corresponds at the first folder in the my folder (email address) or the first folder in absolute on outlook?

Riccardo La Marca

Posted 2018-02-23T19:31:19.403

Reputation: 163

Question was closed 2018-02-25T22:24:13.693

1This question is not about computer hardware or software, within the scope defined in the help center. – Ramhound – 2018-02-23T19:33:50.647

2Do you understand the concepts of an Array? – Ramhound – 2018-02-23T19:34:29.243

I know what the arrays are but I do not know Item(1) to which folder point. – Riccardo La Marca – 2018-02-23T19:36:52.370

What? Item(1) would point to the first SyncObject in the Array. Your response doesn’t seem to indicate you understand Arrays. – Ramhound – 2018-02-23T19:39:19.460

1What do you want an example of? Don’t answer a “code example” that would be out of scope. Trying to help you. – Ramhound – 2018-02-23T19:49:01.100

But i dont know if items started from the first folder in the my account address folder or of the first account of outlook (sorry for bad english, i am italian). – Riccardo La Marca – 2018-02-23T19:49:14.033

the question is Item(1) corresponds at the first folder in the my folder (email address) or the first folder in absolute on outlook? – Riccardo La Marca – 2018-02-23T19:55:43.727

Answers

2

You are discovering collection classes. One such class is the VBA.Collection class; in Excel, you've probably encountered the Workbooks collection class, or the Worksheets collection, or UserForms, or Controls; SyncObjects is just another such collection.

All collection classes have something in common. They're classes, which means they represent objects - but they also contain items. Some collections contain only one type of objects (e.g. Worksheets only contains Worksheet objects), others contain any type of objects.

By convention, all collections are 1-based, so the first item in the collection will be at index 1.

All collections can be enumerated - that is, iterated using a For Each...Next loop. They can do this because they all expose a hidden [_NewEnum] member that has special hidden attributes.

For Each foo In myCollection
    Debug.Print foo.Bar
Next

All collections can also be indexed - that is, their individual items can be accessed by index, or by some kind of key/name. This allows iterating a collection using a For...Next loop - and this is where the Item property kicks in:

For i = 1 To myCollection.Count
    Debug.Print myCollection.Item(i).Bar
Next

The Item property, by convention, is also the collection class' default member (some collection classes can have different default members, e.g. ADODB.Recordset.Fields) - it has a special hidden attribute that makes it possible to implicitly invoke that member, so you can omit it:

For i = 1 To myCollection.Count
    Debug.Print myCollection(i).Bar
Next

...and the outcome is exactly the same as if Item was explicitly spelled out.

Note collections want to be iterated with a For Each...Next loop. Accessing collection items by index is always going to be slower than leveraging the hidden [_NewEnum] member. And the difference is remarkable.

For collection classes, implicitly invoking this default member is convenient - however other (non-collection) classes can also have default members, and implicitly invoking them is a common source of bugs, because the code isn't doing what it says, and/or saying what it does.


What am I saying with Item(1), Item(2), Item(3), etc.?

What .Item(1) is saying, is "give me the first item in the collection". In the case of SyncObjects, each item is a SyncObject object, so you'd retrieve that item with the Set keyword:

Set firstItem = syncList.Item(1)

SyncObjects.Item(1) corresponds at the first folder in the my folder (email address) or the first folder in absolute on outlook?

That should be irrelevant: the entire point of a collection is to encapsulate a group of objects and allow them to be iterated. Which specific object is first, second, third, or last, isn't guaranteed by the implementation: you would use the properties of the individual items (e.g. Name) to find out what specific SyncObject you're looking at, not its index in a collection.

Dim current As SyncObject
For Each current In Application.GetNameSpace("MAPI").SyncObjects
    Debug.Print current.Name
Next

Mathieu Guindon

Posted 2018-02-23T19:31:19.403

Reputation: 795

0

I found a reply, I tested with

dim try as string

try = Outlook.GetNamespace("MAPI").SyncObjects.Item(1) in the debug mode
try is equal at: "Tutti gli account"

try = Outlook.GetNamespace("MAPI").SyncObjects.Item(2)

is equal at: "Cartelle applicazioni"

enter image description here

Riccardo La Marca

Posted 2018-02-23T19:31:19.403

Reputation: 163

1And you can't assume that will be the case every time your code runs, as I explained in my answer. Assuming that index 1 or 2 corresponds to a specific instance of a specific object at run-time will definitely come back to bite you, at one point or another. – Mathieu Guindon – 2018-02-23T20:24:14.160