Originally Posted by
JrMontgom
I have tried to use the Outlook library and I attach the code I am using. The code compiles but after 10 loops through the For Each section it throws a runtime error 13, Type mismatch. The Debug.print statements seem to be OK until the 11th time throught the For Each. Any help would be appreciated. Also since there are 1667 entries to scan there must be a way I can just look at one subfolder in the Inbox folder but I can't seem to figure that out.
[Code]
Private Sub Command14_Click()
Dim olApp As Outlook.Application
Dim OLFolder As Outlook.Folder
Dim olNS As Outlook.Namespace
Dim olMail As Outlook.mailItem
Dim strname As String
Dim intCt As Integer
'Dim olApp As Object, olNS As Object, olMail As Object, eFldr As Object
Dim eFldr As Outlook.Folder
Dim inCt As Integer
On Error GoTo ErrorHandler
Const CALLER As String = " Form_frmtestForm2:Command14_Click "
' Set the reference to outlook or create one
Set olApp = CreateObject("Outlook.Application")
' Set the reference to the oulook MAPI namespace
Set olNS = olApp.GetNamespace("MAPI")
'why can't I reference just one folder in the Folders collection????
Set eFldr = olNS.Folders("johnm1940@comcast.net").Folders("Inb ox")
' Cycle through the emails in the specified mailbox folder
Debug.Print eFldr.Items.Count 'this shows about 1667 items
For Each olMail In eFldr.Items
Debug.Print Nz(eFldr.Name, "No flder name")
Debug.Print Nz(olMail.Subject, "Nothing")
Debug.Print intCt
Debug.Print olMail.To
If InStr(1, olMail.Subject, "Re: Add New UU Members to EF table", vbTextCompare) > 0 Then 'And olMail.UnRead = True 'Then
Debug.Print Nz(olMail.Subject, "No Subject")
Debug.Print Nz(olMail.SenderName, "No Sender name")
Debug.Print Nz(olMail.ReceivedTime, "No received time")
Debug.Print Nz(olMail.SenderEmailAddress, "No send email address")
'olMail.UnRead = False
' From this point additional scripting needed to save to table. Could be like:
' CurrentDb.Execute "INSERT INTO tablename(field1, field2, field3, field4) " & _
' VAlUES('" & olMail.Subject & "', '" & olMail.SenderName & "', #" & olMail.ReceivedTime & "#, '" & olMail.SenderEmailAddress & "')"
End If
intCt = intCt + 1
NextMsg:
Next olMail
Cleanup:
Set olApp = Nothing
Set olNS = Nothing
Set olMail = Nothing
Set eFldr = Nothing
Exit Sub
ErrorHandler:
MsgBox Err.Description & vbCrLf & _
Err.Number & vbCrLf & _
"Called By :" & CALLER & vbCrLf & _
Err.Source, VbMsgBoxStyle.vbCritical, "Could not add new name to data base" & vbCrLf & _
"Module Name: = " & MODULENAME
'GoTo Cleanup
End Sub
[Code/]
I never enter the code in the IF statement.