View Single Post
  #3  
Old January 16th 07, 05:05 AM posted to microsoft.public.outlook.program_vba
Michael Bauer [MVP - Outlook]
external usenet poster
 
Posts: 1,885
Default Why don't I need a MapiFolder object?



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.Cou nt
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?...4&languageid=1
(German: http://www.VBOffice.net/product.html?pub=6)

Am Tue, 16 Jan 2007 02:32:30 GMT schrieb 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

Ads