Microsoft Office Tutorials and References
In Depth Information
Collection Naming Conventions
By tradition, a collection is named with the plural of the object it contains. For example, a
collection that holds Folder objects will be called Folders . Unfortunately there are a few exceptions
to this rule that you’ll encounter as you explore the Outlook Object Model.
First, you must declare an object variable of the same type as the objects in the collection.
Continuing with the email example:
Dim msg As Outlook.MailItem
Then create a reference to the object that contains the collection — in this case, an Outlook folder —
specifically, the Inbox:
Dim f As Outlook.Folder
Dim MyOutlookNamespace As Outlook.NameSpace
Set MyOutlookNameSpace = GetNameSpace(“MAPI”)
Set f = MyOutlookNameSpace.GetDefaultFolder(olFolderInbox)
Finally, loop through the collection as follows:
For Each msg in f.Items
‘ With each repetition of the loop, msg refers to the ‘ next
message in the collection.
Next
The previous code examples are missing a couple of details and will not actually run, but they
serve to illustrate the principle of collections. You’ll note that this is one of those exceptions where
a collection — Items in this case — is not named as the plural of the kind of object it contains,
MailItems .
Using Named Constants
If you examine the previous code snippet, you’ll see this line of code:
Set f = GetDefaultFolder(olFolderInbox)
What is olFolderInbox ? It’s not a variable that you have declared. Rather it’s one of the named
constants in the Object Model. Some methods (a procedure that is part of an object) require an
argument to tell them what to do. Rather than use hard-to-remember numbers, the Object Model
provides these descriptive constants that are a lot easier to remember. What’s more, the VBA
Editor’s IntelliSense feature will list the available constants as you are typing in your code, as
shown in Figure 24.1.
Search JabSto ::




Custom Search