PDA

View Full Version : Looping through inbox, code fails



Zack Barresse
04-27-2006, 10:26 AM
Hey all,

Can anybody tell me why this code fails after the first iteration email??

..

Sub LoopThrough_SentReceived()

Dim OL As Outlook.Application
Dim NS As Outlook.NameSpace, Inbox As Outlook.MAPIFolder, FL As Outlook.MAPIFolder
Dim Email As Object, IBitems As Outlook.Items
Dim XL As Outlook.MailItem, XLwb As Object, XLws As Object
Dim Cnt As Long

On Error GoTo ErrHandle
Set OL = ThisOutlookSession
Set NS = GetNamespace("MAPI")
Set Inbox = NS.GetDefaultFolder(olFolderInbox)
Set IBitems = Inbox.Items
Set XL = CreateObject("Excel.Application")
XL.Visible = True
Set XLwb = XL.Workbooks.Add
Set XLws = XLwb.Sheets(1)
Cnt = 1

For Each FL In Inbox.Folders
For Each Email In IBitems
Cnt = Cnt + 1
XLws.Cells(Cnt, 1).Value = Email.ReceivedByName
Next Email
Next FL

ErrHandle:
If Err <> 0 Then
XL.Quit
Else
MsgBox Cnt
End If
Set Inbox = Nothing
Set NS = Nothing
Set XLws = Nothing
Set XLwb = Nothing
Set XL = Nothing

End Sub

Jacob Hilderbrand
04-27-2006, 10:55 AM
Shouldn't

For Each Email In IBitems

Be

For Each Email In FL.Items

Norie
04-27-2006, 11:07 AM
Zack

Where is the code failing?

I get a type mismatch here.


Set XL = CreateObject("Excel.Application")

Because of this declaration.


Dim XL As Outlook.MailItem
Perhaps just a typo?:)

When I change the declaration type to Object the code seems to work fine.

Zack Barresse
04-27-2006, 02:17 PM
Wow, two horrible typos, in one routine!! I'm getting sloppy. Thanks guys. Appreciate the extra set of eyes. :yes

lucas
04-27-2006, 03:45 PM
how would you handle this with pop3 instead of mapi? I may not understand how this is supposed to work. It runs after the mentioned changes and excel is opened but nothing added to the excel file from in or outbox..