Iterating Through A Folder

G

Guest

I can iterate through an open message's text via the follwoing declarations:
Dim myOlApp As Object
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim myBody As Variant
Dim lngMsgLen As Long

Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.ActiveInspector
Set objItem = myItem.CurrentItem 'Current eMail MUST BE OPEN!
myBody = objItem.Body
lngMsgLen = Len(myBody)

But, how do I designate a folder, then iterate through all of its message's
text?
Thanks,
Trent Argante
[DC.J(125)!(382)]
 
D

David Lloyd

Trent:

The following KB article may be a good reference:

http://support.microsoft.com/default.aspx?scid=kb;en-us;208520

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


I can iterate through an open message's text via the follwoing declarations:
Dim myOlApp As Object
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim myBody As Variant
Dim lngMsgLen As Long

Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.ActiveInspector
Set objItem = myItem.CurrentItem 'Current eMail MUST BE OPEN!
myBody = objItem.Body
lngMsgLen = Len(myBody)

But, how do I designate a folder, then iterate through all of its message's
text?
Thanks,
Trent Argante
[DC.J(125)!(382)]
 
G

Guest

David, I followed it upto:
Dim ol As Outlook.Application
Dim olns As Object
Dim MyFolder As Outlook.Folders
Dim myItem As Object

Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set MyFolder = olns.GetDefaultFolder(olFolderDrafts)

Then got lost. (I added the Dim statements.) I have a folder created off
of the "Drafts" folder called "VBA" for testing purposes. Can you cut to the
chase of this article and give me the code lines to access email message text
in this folder?
Thanks,
Trent Argante
[DC.J(125)!(382)]
 
D

David Lloyd

Trent:

Below is some sample code. You did not say whether you were going to run
this code from Outlook, or another program. The sample below assumes you
are running the code from Outlook. The Body property of the MailItem object
is protected by Outlook security and will generate a security warning for
each MailItem unless you derive the Namespace from Outlook's inherent
Application object. Using the Set ol = New Outlook.Application, will cause
these warnings, for instance.

Function IterateEmails()
Dim olkVBA As Outlook.MAPIFolder
Dim olkNameSpace As Outlook.NameSpace
Dim mi As Outlook.MailItem

On Error GoTo Errorhandler

'Notice that we are using the inherent Outlook Application Object
Set olkNameSpace = Application.GetNamespace("MAPI")
Set olkVBA =
olkNameSpace.GetDefaultFolder(olFolderDrafts).Folders("VBA")

For Each mi In olkVBA.Items
Debug.Print mi.Subject
Debug.Print mi.Body
Next mi

Function_Exit:

Set mi = Nothing
Set olkVBA = Nothing
Set olkNameSpace = Nothing

Exit Function

Errorhandler:
If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical, "Error"
Resume Function_Exit
End If

End Function


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


David, I followed it upto:
Dim ol As Outlook.Application
Dim olns As Object
Dim MyFolder As Outlook.Folders
Dim myItem As Object

Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set MyFolder = olns.GetDefaultFolder(olFolderDrafts)

Then got lost. (I added the Dim statements.) I have a folder created off
of the "Drafts" folder called "VBA" for testing purposes. Can you cut to
the
chase of this article and give me the code lines to access email message
text
in this folder?
Thanks,
Trent Argante
[DC.J(125)!(382)]


David Lloyd said:
Trent:

The following KB article may be a good reference:

http://support.microsoft.com/default.aspx?scid=kb;en-us;208520

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or
warranties.


I can iterate through an open message's text via the follwoing
declarations:
Dim myOlApp As Object
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim myBody As Variant
Dim lngMsgLen As Long

Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.ActiveInspector
Set objItem = myItem.CurrentItem 'Current eMail MUST BE OPEN!
myBody = objItem.Body
lngMsgLen = Len(myBody)

But, how do I designate a folder, then iterate through all of its
message's
text?
Thanks,
Trent Argante
[DC.J(125)!(382)]
 
G

Guest

David, you're right. I forgot to mentioned that the code was being run on
Windows NT, Office 97, Excel. The VBA Editor has not been installed for
Outlook on my system as of yet. Anyway, I was able to take your example and
revise it with the following:
Dim olkApp As Object 'ADDED
Dim olkNameSpace As Outlook.NameSpace
Dim olkVBA As Outlook.MAPIFolder
Dim mi As Outlook.MailItem

On Error GoTo Errorhandler

Set olkApp = CreateObject("Outlook.Application") 'ADDED
Set olkNameSpace = olkApp.GetNamespace("MAPI") 'ADJUSTED
Set olkVBA = olkNameSpace.GetDefaultFolder(olFolderDrafts).Folders("VBA")

Namely, the addition and use of olkApp. It works like a charm.
Thank you very much, David.
--
Trent Argante
[DC.J(125)!(382)]


David Lloyd said:
Trent:

Below is some sample code. You did not say whether you were going to run
this code from Outlook, or another program. The sample below assumes you
are running the code from Outlook. The Body property of the MailItem object
is protected by Outlook security and will generate a security warning for
each MailItem unless you derive the Namespace from Outlook's inherent
Application object. Using the Set ol = New Outlook.Application, will cause
these warnings, for instance.

Function IterateEmails()
Dim olkVBA As Outlook.MAPIFolder
Dim olkNameSpace As Outlook.NameSpace
Dim mi As Outlook.MailItem

On Error GoTo Errorhandler

'Notice that we are using the inherent Outlook Application Object
Set olkNameSpace = Application.GetNamespace("MAPI")
Set olkVBA =
olkNameSpace.GetDefaultFolder(olFolderDrafts).Folders("VBA")

For Each mi In olkVBA.Items
Debug.Print mi.Subject
Debug.Print mi.Body
Next mi

Function_Exit:

Set mi = Nothing
Set olkVBA = Nothing
Set olkNameSpace = Nothing

Exit Function

Errorhandler:
If Err.Number <> 0 Then
MsgBox Err.Description, vbCritical, "Error"
Resume Function_Exit
End If

End Function


--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or warranties.


David, I followed it upto:
Dim ol As Outlook.Application
Dim olns As Object
Dim MyFolder As Outlook.Folders
Dim myItem As Object

Set ol = New Outlook.Application
Set olns = ol.GetNamespace("MAPI")
Set MyFolder = olns.GetDefaultFolder(olFolderDrafts)

Then got lost. (I added the Dim statements.) I have a folder created off
of the "Drafts" folder called "VBA" for testing purposes. Can you cut to
the
chase of this article and give me the code lines to access email message
text
in this folder?
Thanks,
Trent Argante
[DC.J(125)!(382)]


David Lloyd said:
Trent:

The following KB article may be a good reference:

http://support.microsoft.com/default.aspx?scid=kb;en-us;208520

--
David Lloyd
MCSD .NET
http://LemingtonConsulting.com

This response is supplied "as is" without any representations or
warranties.


I can iterate through an open message's text via the follwoing
declarations:
Dim myOlApp As Object
Dim myItem As Outlook.Inspector
Dim objItem As Object
Dim myBody As Variant
Dim lngMsgLen As Long

Set myOlApp = CreateObject("Outlook.Application")
Set myItem = myOlApp.ActiveInspector
Set objItem = myItem.CurrentItem 'Current eMail MUST BE OPEN!
myBody = objItem.Body
lngMsgLen = Len(myBody)

But, how do I designate a folder, then iterate through all of its
message's
text?
Thanks,
Trent Argante
[DC.J(125)!(382)]
 

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