ChrisCalvert
Technical User
I am using code I found at which was posted by mp9. I have found this code helpful. However, I am having a problem trapping errors if the email is not recognized by Outlook.
My Error Handler, which I inserted into the function, always returns a different error number. This is with no changes in the code or table. I just run it, then run again. I have gotten -1421852667, -1318043643, -348110843, and several more.
I want to offer the user the ability to correct the email address, but if I can't trap the error....
Anyone have any idea why this would not be the same. The code is posted below, if anyone thinks it would help.
This is the function being called.
-------------------------------------------------
Public Function fctnOutlook(Optional FromAddr, Optional Addr, Optional CC, Optional BCC, _
Optional Subject, Optional MessageText, Optional Vote As String = vbNullString, _
Optional Urgency As Byte = 1, Optional EditMessage As Boolean = True)
On Error GoTo Error_Handler
' Code sample from Accessory
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Set objOutlook = CreateObject("Outlook.Application"
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
If Not IsMissing(FromAddr) Then
.SentOnBehalfOfName = FromAddr
End If
If Not IsMissing(Addr) Then
Set objOutlookRecip = .Recipients.Add(Addr)
objOutlookRecip.Type = olTo
End If
If Not IsMissing(CC) Then
Set objOutlookRecip = .Recipients.Add(CC)
objOutlookRecip.Type = olCC
End If
If Not IsMissing(BCC) Then
Set objOutlookRecip = .Recipients.Add(BCC)
objOutlookRecip.Type = olBCC
End If
If Not IsMissing(Subject) Then
.Subject = Subject
End If
If Not IsMissing(MessageText) Then
.Body = MessageText
End If
If IsNull(Vote) = False Then
.VotingOptions = Vote
End If
Select Case Urgency
Case 2
.Importance = olImportanceHigh
Case 0
.Importance = olImportanceLow
Case Else
.Importance = olImportanceNormal
End Select
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
If EditMessage Then
.Display
Else
.Save
.Send
End If
End With
Set objOutlook = Nothing
Exit Function
Error_Handler:
Dim strCorrectedAddress As String
'' Adds the current(and assumedly invalid) email address into a string so that it can be displayed
'' as an error message.
MsgBox "Entering Error Handler"
MsgBox Err.Number
MsgBox Err.Description
Select Case Err.Number
'Invalid Email Address
Case -2147467259
MsgBox "Case One"
Resume
Case -1421852667
MsgBox "Case Two"
Resume
Case -1318043643
MsgBox "Case invalid email."
strErrorMessage = "Outlook does not recognize '" & rstEmailAddresses!Email & "' as a valid email address." & (Chr$(13)) _
& "Please correct the address to a valid format below:"
strCorrectedAddress = InputBox(strErrorMessage)
Call fctnOutlook("sendingaddress", strCorrectedAddress, , , "Testing Send Code", _
strTemplate, , , False)
Resume
'' any other error message
Case Else
MsgBox "None of those were it"
MsgBox Error(Err.Number)
End Select
End Function
My Error Handler, which I inserted into the function, always returns a different error number. This is with no changes in the code or table. I just run it, then run again. I have gotten -1421852667, -1318043643, -348110843, and several more.
I want to offer the user the ability to correct the email address, but if I can't trap the error....
Anyone have any idea why this would not be the same. The code is posted below, if anyone thinks it would help.
This is the function being called.
-------------------------------------------------
Public Function fctnOutlook(Optional FromAddr, Optional Addr, Optional CC, Optional BCC, _
Optional Subject, Optional MessageText, Optional Vote As String = vbNullString, _
Optional Urgency As Byte = 1, Optional EditMessage As Boolean = True)
On Error GoTo Error_Handler
' Code sample from Accessory
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Set objOutlook = CreateObject("Outlook.Application"
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
If Not IsMissing(FromAddr) Then
.SentOnBehalfOfName = FromAddr
End If
If Not IsMissing(Addr) Then
Set objOutlookRecip = .Recipients.Add(Addr)
objOutlookRecip.Type = olTo
End If
If Not IsMissing(CC) Then
Set objOutlookRecip = .Recipients.Add(CC)
objOutlookRecip.Type = olCC
End If
If Not IsMissing(BCC) Then
Set objOutlookRecip = .Recipients.Add(BCC)
objOutlookRecip.Type = olBCC
End If
If Not IsMissing(Subject) Then
.Subject = Subject
End If
If Not IsMissing(MessageText) Then
.Body = MessageText
End If
If IsNull(Vote) = False Then
.VotingOptions = Vote
End If
Select Case Urgency
Case 2
.Importance = olImportanceHigh
Case 0
.Importance = olImportanceLow
Case Else
.Importance = olImportanceNormal
End Select
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
If EditMessage Then
.Display
Else
.Save
.Send
End If
End With
Set objOutlook = Nothing
Exit Function
Error_Handler:
Dim strCorrectedAddress As String
'' Adds the current(and assumedly invalid) email address into a string so that it can be displayed
'' as an error message.
MsgBox "Entering Error Handler"
MsgBox Err.Number
MsgBox Err.Description
Select Case Err.Number
'Invalid Email Address
Case -2147467259
MsgBox "Case One"
Resume
Case -1421852667
MsgBox "Case Two"
Resume
Case -1318043643
MsgBox "Case invalid email."
strErrorMessage = "Outlook does not recognize '" & rstEmailAddresses!Email & "' as a valid email address." & (Chr$(13)) _
& "Please correct the address to a valid format below:"
strCorrectedAddress = InputBox(strErrorMessage)
Call fctnOutlook("sendingaddress", strCorrectedAddress, , , "Testing Send Code", _
strTemplate, , , False)
Resume
'' any other error message
Case Else
MsgBox "None of those were it"
MsgBox Error(Err.Number)
End Select
End Function