Strange behavior

odrap

Registered User.
Local time
Today, 23:42
Joined
Dec 16, 2008
Messages
156
To retrieve attachements from Emails into my database i use a procedure of wich a part is given below.
Most of tHe time this code works fine, but now and then, for reasons i can't figure out i get a errormessage telling me that there are type mismatched. That happens when the execution of the code reach the line marked with (*). If i replace the codeline "On error GoTo Errorhandler " by "On Error Resume next", neglecting the error,the execution of the code works allwright!
What's going on ?

Public Sub SaveAttachments()
On Error GoTo ErrorHandler
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim MoveToFldr As MAPIFolder
Dim MoveFldr2 As MAPIFolder
Dim MoveFldr3 As MAPIFolder
Dim olMi As mailitem
Dim olAtt As Outlook.attachment
Dim MyPath1 As String
Dim MyPath2 As String
Dim I As Long
Dim Aantal As Integer

On Error Goto ErrorHandler
Set olApp = GetObject(, "Outlook.Application")
If Err = 429 Then
Set olApp = CreateObject("Outlook.Application")
End If
On Error GoTo ErrorHandler
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set MoveToFldr = Fldr.Folders("Special")
MyPath1 = "C:\DW\Isabelle\"
MyPath2 = "C:\DW\Bart\"

(*) For I = Fldr.Items.Count To 1 Step -1
Set olMi = Fldr.Items(I)
If InStr(1, olMi.Subject, "Geboekte orders van agent Bart") > 0 Then
MsgBox " Er werd een Email afkomstig van Bart gevonden"
For Each olAtt In olMi.Attachments
Bart = True
If olAtt.FileName = "Ordersverzenden_be.accdb" Then
olAtt.SaveAsFile MyPath2 & olAtt.FileName 'de bijlage wordt opgeslagen in c:\dw\Bart
End If
Next olAtt
olMi.Move MoveToFldr
...
...
 
This will normally mean that the For Next loop counters are out of sync or outside the boundaries. Lets say you have 25 items and your for loop states form x = 1 to 30 then as soon as x > 25 it will still look for the next item in the list - which does not exist - so it errors on you.

Check the validity of the x & y

David
 
First of all, thank you very much for your help.
However, looking to my code, and especially to the following line: "For I = Fldr.Items.Count To 1 Step -1", how can the counter I in this case exceeds the boundary of available items?
Am i wrong to suppose that Fldr.Items.Count just determine the number of available items ?
 
o,

For I = (Fldr.Items.Count - 1) To 0 Step -1

The index starts at 0.

Wayne
 
The fact that the index starts at 0 and not at 1, makes it for the line of code, which starts at 1, lesser probably to exceed the number of items. So in my opinion this can't be the reason here for the error message that occur, or am I wrong in my conclusion?
 
o,

If Fldr.Items.Count is for example 30, the subscripts will run from 0 to 29.

Your loop will run from 30 to 1 (decrementing).

There is no element #30 (your error) and you will always miss the first
element #0.

If you use the Resume Next, you'll ignore the error, but you'll still miss
the first element.

hth,
Wayne
 

Users who are viewing this thread

Back
Top Bottom