Why don't I need a MapiFolder object?

C

Carol G

I'm trying to wrap my head around the outlook objects. I'm sure this is a
stupid question but I'm having trouble grasping when I need to declare
objects and when I don't.
Why don't I need a MapiFolder object to view properties such as Name of the
Current Folder below?
Confused....
Carol

Sub TestOfExpolorerObject()
Dim objApp As Outlook.Application
Dim objNms As Outlook.NameSpace
Dim objExplorer As Outlook.Explorer

Set objApp = CreateObject("Outlook.Application")
Set objNms = objApp.GetNamespace("Mapi")
Set objExplorer = objApp.ActiveExplorer

Debug.Print objExplorer.CurrentFolder.Name

Set objApp = Nothing
Set objNms = Nothing
Set objExplorer = Nothing
End Sub
 
D

Dmitry Streblechenko

It is all up to you. The line

Debug.Print objExplorer.CurrentFolder.Name

can be split into

dim Folder As Outlook.MAPIFolder
set Folder = objExplorer.CurrentFolder
Debug.Print Folder.Name

That would be helpful if you are planning to access
objExplorer.CurrentFolder more than once. In that case it makes sense to
cache it in an explicitly declared variable rather than call
objExplorer.CurrentFolder multiple times.

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
M

Michael Bauer [MVP - Outlook]

Carol, one might say that's up to you. Actually you wouldn't need any
variable, but the root object. For instance, for the Inbox' name and its
first Items' subject you could write:

Debug.Print
Application.GetNamespace("mapi").GetDefaultFolder(olFolderInbox).Name
Debug.Print
Application.GetNamespace("mapi").GetDefaultFolder(olFolderInbox).Items(1).Subject

As you can see, this quickly would become confused and it's a lot more work
to write the code. And each call takes time! E.g., in VBA t1 needs on my PC
for about 1800 items 3.5 seconds, t2 only 1.4 seconds.

Private Declare Function GetTickCount Lib "kernel32" () As Long
Private time1 As Long
Private time2 As Long

Public Sub t1()
Dim i&

time1 = GetTickCount
For i = 1 To Application.ActiveExplorer.CurrentFolder.Items.Count
Debug.Print
Application.ActiveExplorer.CurrentFolder.Items(i).Subject
Next
time2 = GetTickCount
Debug.Print (time2 - time1) / 1000
End Sub

Sub t2()
Dim i&
Dim Items As Outlook.Items

time1 = GetTickCount
Set Items = Application.ActiveExplorer.CurrentFolder.Items
For i = 1 To Items.Count
Debug.Print Items(i).Subject
Next
time2 = GetTickCount
Debug.Print (time2 - time1) / 1000
End Sub

So I'd say, whenever you need to access an object more than once then use a
variable and store the reference to the object.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)

Am Tue, 16 Jan 2007 02:32:30 GMT schrieb Carol G:
 
C

Carol G

So , does that mean that .GetDefaultFolder(olFolderWhatever) creates an
explorer object for itself and then each time I access
..GetDefaultFolder(whatever) it creates a new temporary explorer object.
So declaring one myself and pointing it at the folder is quicker but not
necessary? I have a lot to learn.
I am going to try out your code.
Danke Schoen for your time.
Carol
 
M

Michael Bauer [MVP - Outlook]

GetDefaultFolder doesn't create Explorers, but it returns a reference to a
folder. As you can see, the function needs an argument for the requested
folder, that is for each call the function has to check what folder you want
to get and 'find' it. Obviously, it's faster to reuse the reference stored
in a variable.

So yes, it's quicker but not necessary.

--
Viele Gruesse / Best regards
Michael Bauer - MVP Outlook
Keep your Outlook categories organized!
http://www.shareit.com/product.html?productid=300120654&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)

Am Tue, 16 Jan 2007 19:53:47 GMT schrieb Carol G:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top