vba reading outlook error

Hi Experts,

I have a database of email addresses that are being used for mass email listings.
Now I also have a module that is used for cleaning up invalid email addresses.

the way it works is that I save all bounced emails in a specific folder and then I run that module, that reads every file under that folder and extract the email address from within the text of the file, and then moves that email from the email address field to the Invalid email address field.

below is the code:
s = Dir("F:\Mail\")
While Not s = ""

Set OL = New Outlook.Application
Set Msg = OL.CreateItemFromTemplate("F:\Mail\" & s)

insertEmails (Msg.Body)

Set OL = Nothing
Set Msg = Nothing
s = Dir
Wend

Open in new window


The issue I am encountering now is that the following line of code gets stuck when trying to open certain files, I guess its because of the name..not sure.
"Set Msg = OL.CreateItemFromTemplate("F:\Mail\" & s)"

the error message states "Type mismatch".

for example  the following file name gave me that error "Undeliverable  Change in Requirements   .msg"
while the following file was accepted "Delivery Status Notification (Failure) (151).msg"

any idea how can I solve this issue?

meanwhile I run it with "on error resume next", however indeed I need the program to work for those files as well..
LVL 5
bfuchsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SheilsCommented:
It may have to do with the space at the end or use of numerical values. To make sure that the computer reads it as a string try modifying your code as follows:-

Set Msg = OL.CreateItemFromTemplate("F:\Mail\'" & s & "'")
0
bfuchsAuthor Commented:
Hi,
That didnt work, file name dont exsists...It concatenates the single quotes as part of the name.
0
SheilsCommented:
Try:

Set Msg = OL.CreateItemFromTemplate("F:\Mail\" & chr(34) s & chr(34))
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

SheilsCommented:
sorry

Set Msg = OL.CreateItemFromTemplate("F:\Mail\" & chr(34) & s & chr(34))
0
bfuchsAuthor Commented:
same issue
0
SheilsCommented:
Post the names of 3 files that works and 3 that doesn't.
0
SheilsCommented:
Also send the code for the insertEmails function
0
bfuchsAuthor Commented:
Hi Sheils,

Further testing shows that this really has nothing to do with the name of file, (As i tried to rename the file and continue to get the type mismatch error), I am assuming that there these may be a different type of file, although they all have the same icon, file extension  and are all being opened by Outlook editor...

I will attach 2 files, one that is working and one that not, maybe you can spot the difference..

Thanks
Ben
Delivery-Status-Notification--Failure---
Undeliverable--Change-in-Requirements.ms
0
SheilsCommented:
Also send the code for the insertEmails function
0
SheilsCommented:
Your two files one have extention and the otherone don't. I am not sure which one works as one is undeliverable and the other is failure.

Also have you run a debug on your code to find out exactly where it breaks
0
bfuchsAuthor Commented:
Hi,
Public Function insertEmails(s As String)
    Dim db As Database, rs As Recordset, s1 As String, s2 As String, i As Long, i2 As Long
    Set db = CurrentDb
    Set rs = db.OpenRecordset("BadEmails")
    i2 = Len(s)
    For i = 1 To i2 - 1
        s1 = s1 & Mid(s, i, 1)
        'Debug.Print s1
        If Mid(s, i + 1, 1) = " " Or i + 1 = i2 Or Asc(Mid(s, i + 1, 1)) = 13 Then
            If InStr(1, s1, "@") > 0 Then
                rs.AddNew
                If InStr(1, s1, "<") > 0 Then
                rs("BadEmail") = getemail(s1, "<>")
                ElseIf InStr(1, s1, "(") > 0 Then
                rs("BadEmail") = getemail(s1, "()")
                Else
                rs("BadEmail") = s1
                End If
                rs.Update
            End If
            s1 = ""
            i = i + 1
        End If
    
    Next
    rs.Close
    db.Close
    
End Function

Open in new window


the code breaks in that line Set Msg = OL.CreateItemFromTemplate("F:\Mail\" & s)

The undeliverable is causing the issue.
0
ltlbearand3Commented:
The variable S is getting set to something that is not registering as a string.

I notice that you first set it as:
s = Dir("F:\Mail\")

Open in new window


But before you loop again you just set is as:
s = Dir

Open in new window


I am assuming that should be the same as the first.

Add this line before your line that errors:
Debug.Print TypeName(s)
Debug.print s

Open in new window


And see if that gives you any clues as to what is happening.
0
Robberbaron (robr)Commented:
the Dir statements are correct.

i have reordered the code somewhat to give a better idea of where and what error may occur and reduced the creation of objects. also used variables
Sub Test1()
    Dim fldr As String, msgfilename As String, sTmp As String
    fldr = "F:\Mail\"
    
    Dim OL As Object, Msg As Object
    
    Set OL = New Outlook.Application
    
    msgfilename = Dir(fldr)   'get first filename in folder
    

    Do While Not msgfilename = ""

        s = fldr & msgfilename
        Debug.Print ">>" & s & "<"
        On Error Resume Next
        Set Msg = OL.CreateItemFromTemplate(s)
        
        If Err Then Debug.Print Err.Description
        
        insertEmails (Msg.Body)

        Set Msg = Nothing
        msgfilename = Dir   'get next file
    Loop
    
    Set OL = Nothing
End Sub

Open in new window

to ensure types are ok and make easier to change if needed.

delete the dim statements if using in vbs.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bfuchsAuthor Commented:
Hi Robberbaron,
I changed to the way you suggested and the problem is not happening, any explanation?
0
Robberbaron (robr)Commented:
1. i moved the outlook object out of loop so only created once. and then reused
2. i forced the creation of the filename to happen outside the template object creation, into a variable that is passed to the Outlook function this should not have had any effect but makes it easier to detect any issues with the file name itself.

3. are you sure it didnt just skip over the error as i had put an On Error Resume there.  Check the Immediate window for details.
Sub Test2()
    Dim fldr As String, msgfilename As String, sTmp As String
    fldr = "F:\Mail\"
    
    Dim OL As Object, Msg As Object
    
    Set OL = New Outlook.Application
    
    msgfilename = Dir(fldr)   'get first filename in folder
    

    Do While Not msgfilename = ""

        s = fldr & msgfilename
        Debug.Print ">>" & s & "<"
        On Error Resume Next
        Set Msg = OL.CreateItemFromTemplate(s)
        
        If Err Then 
            Debug.Print Err.Description
	    MsgBox "error @ msg >>" & s & "<<"
        else
        	insertEmails (Msg.Body)
	End if

        Set Msg = Nothing
        msgfilename = Dir   'get next file
    Loop
    
    Set OL = Nothing
End Sub

Open in new window

.
0
bfuchsAuthor Commented:
Yes, I realized that on error statement and commented out and the error didn't occur,
As a matter of fact, I looked at the old version and saw which file is causing the error message, then on the new version I inserted a breakpoint in the case that file is being processed, and it stopped there, then continued w/o an issue..
I guess you unintentionally solved the problem, just for curiosity I would like to know what did the trick..
0
Robberbaron (robr)Commented:
most likely not recreating the outlook object each time.  it would chew up memory as it sometimes takes a few seconds to complete garbage collection.
0
bfuchsAuthor Commented:
Thanks to all participants!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.