Outlook VBA Errors and disables rule

This VB script looks at the begining of the subject and removes the mess that our IT dept adds to our email to notify us that the email came from an external source and should be handled with care.  Since I'm smart enough to do that without being reminded I personally want to remove that because an email conversation will end up with an additional string pre-pended to the subject line.    "RE: <EXT> Subject RE: <EXT> SubjectRE: <EXT> Subject RE: <EXT> Subject RE: <EXT> Subject RE: <EXT> Subject <EXT> Subject"  

Occasionally it throws an error that I haven't been able to trap and handle which causes the rule to be deactivated.  Any suggestions?

Sub Remove_EXT(myItem As MailItem)

    On Error GoTo Error_Handler
    
    Dim mySubject As String
    mySubject = myItem.Subject

If (Left(mySubject, 6)) = "<EXT> " Then
    mySubject = Right(mySubject, Len(mySubject) - 6)
End If

If (Left(mySubject, 5)) = "<EXT>" Then
    mySubject = Right(mySubject, Len(mySubject) - 5)
End If

If mySubject = "" Then
    Exit Sub
Else
       myItem.Subject = mySubject
       myItem.Save
End If

Exit Sub

Error_Handler:

    MsgBox "An error occurred" & vbCrLf & vbCrLf & "Error Number: " & Err.Number & vbCrLf & _
    "Error Source: RemoveExternalPrefix " & "Error Description: " & Err.Description, vbCritical, "An Error has Occured!"
    
End Sub

Open in new window

LVL 7
TimBusiness Systems AnalystAsked:
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.

Martin LissOlder than dirtCommented:
You have an error routine so what do you mean when you say "an error that I haven't been able to trap"?

BTW the error routine says "Error Source: RemoveExternalPrefix " which doesn't seem to be right since it's not the name of the procedure..
0
Martin LissOlder than dirtCommented:
You can also simplify you code by doing it this way.
Sub Remove_EXT(myItem As MailItem)

    On Error GoTo Error_Handler
    
    Dim mySubject As String
    mySubject = myItem.Subject

Select Case True
    Case Left(mySubject, 6) = "<EXT> "
        mySubject = Right(mySubject, Len(mySubject) - 6)
    Case Left(mySubject, 5) = "<EXT>"
        mySubject = Right(mySubject, Len(mySubject) - 5)
    Case mySubject = ""
        Exit Sub
End Select

myItem.Subject = mySubject
myItem.Save

Exit Sub

Error_Handler:

    MsgBox "An error occurred" & vbCrLf & vbCrLf & "Error Number: " & Err.Number & vbCrLf & _
    "Error Source: RemoveExternalPrefix " & "Error Description: " & Err.Description, vbCritical, "An Error has Occured!"
    
End Sub

Open in new window

0
Bill PrewCommented:
Outlook rules that fire VBA can be a little tricky too, I have at least one that every so often seems to just error for no apparent reason.  When it does I have to do as you are and re-enable the rule.  I'm not sure if Outlook might be trying to fire the rule more than once in two small a time, and the VBA routine is still processing, and it doesn't like that?  Or if there is some contention for the email item with other Outlook activities like resolving images in it, etc.  But it doesn't seem to be bullet proof from my experience.

Your code is pretty straight forward, hard to imagine anything that you are doing that errors, unless it is caused by contention with outlook, etc.  Think about adding check anyplace you can to prevent stuff that shouldn't happen.  Like if for some reason Outlook moves or deletes the email after launching the rule based VBA, but before you have Saved.  Maybe you would get an error, and maybe you can check that the mail item still exists before saving.  Etc...


»bp
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
TimBusiness Systems AnalystAuthor Commented:
Thanks guys, regarding trapping the error.  I don't get my message, I get a message from Outlook that has the 'unexpected error' msg that I loathe MS so much for.  Of course it's unexpected you fools! </rant>

I suspect Bill's suggestion of moving the message may be raising the error.  I'm going to look in to that and be back to close this out.
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
VB Script

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.