Trouble creating an Error Handler correctly

S

SCrowley

Hello,

OL2003

In creating a custom Required Category Form (using Sue Mosher's original
code, modified) I am tyring to create an Error_Handler to fire a Message Box
if the user doesn't select 3 of the required categories. I'm not sure if I
should use the Exit Function or Resume statement or if I'm placing it in the
right line of the code.

Here is the partial code:

Function HasRequiredCategory()
'Set Error Trap
On Error GoTo Error_Handler
Dim intMatchCount
Dim intReqCats

'******USER OPTION*****
'number of category matches required
intReqCats = 3

Set objPage = Item.GetInspector.ModifiedFormPages("General")
Set objControl = objPage.Controls("lstCategories")
If gstrRequiredCats <> "" Then
arrCats = Split(UCase(Item.Categories),",")
arrRequiredCats = Split(gstrRequiredCats,";")
For I = 0 To UBound(arrCats, 1)
For J = 0 To UBound(arrRequiredCats, 1)
If Trim(arrCats(I)) = Trim(arrRequiredCats(J)) Then
intMatchCount = intMatchCount + 2
If intMatchCount >= intReqCats Then
Exit For
End If
End If
Next
If intMatchCount >= intReqCats Then
Exit For
End If
Next
End If
HasRequiredCategory = (intMatchCount >= intReqCats)

Exit Function

'Error Handling Routine
Error_Handler:
'Handle the error
MsgBox "Please select categories as instructed in the Red Box located to the
left of the Categories box"
Exit Function
End Function
 
S

Sue Mosher [MVP-Outlook]

The real trouble is that VBScript doesn't support error handlers. It supports only On Error Resume Next. Couldn't you just check (intMatchCount >= intReqCats) and/or (Err = 0) at the end of your procedure?
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

SCrowley

Thank you Sue. The code does have (intMatchCount >= intReqCats) however, I'd
like the user to know why their contact isn't saving. Right now it just sits
there if they don't select the correct amount of categories. I'm not familiar
with (Err = 0) and how to create a message box.

Can all of the Required Category code be put into VBA and run that way?
--
Thank you,
(e-mail address removed)


Sue Mosher said:
The real trouble is that VBScript doesn't support error handlers. It supports only On Error Resume Next. Couldn't you just check (intMatchCount >= intReqCats) and/or (Err = 0) at the end of your procedure?
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

Sue Mosher [MVP-Outlook]

If Err <> 0 Then
MsgBox "Your text goes here"
End If

VBA is not suitable for building an application you're deploying to other people. A COM add-in could do it, but you'd have to install it for each user.
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



SCrowley said:
Thank you Sue. The code does have (intMatchCount >= intReqCats) however, I'd
like the user to know why their contact isn't saving. Right now it just sits
there if they don't select the correct amount of categories. I'm not familiar
with (Err = 0) and how to create a message box.

Can all of the Required Category code be put into VBA and run that way?
 
S

SCrowley

I'm trying the If Err but I must be putting it in the wrong place.

I placed it under:
Function HasRequiredCategory()
Dim intMatchCount
Dim intReqCats
If Err <> 0 Then
MsgBox "Sorry, your contact will not save until you select categories as
instructed in the Red Box located to the left of the Categories box"
End If
'******USER OPTION*****
'number of category matches required
intReqCats = 2

Set objPage = Item.GetInspector.ModifiedFormPages("General")
Set objControl = objPage.Controls("lstCategories")
If gstrRequiredCats <> "" Then
arrCats = Split(UCase(Item.Categories),",")
arrRequiredCats = Split(gstrRequiredCats,";")
For I = 0 To UBound(arrCats, 1)
For J = 0 To UBound(arrRequiredCats, 1)
If Trim(arrCats(I)) = Trim(arrRequiredCats(J)) Then
intMatchCount = intMatchCount + 1
If intMatchCount >= intReqCats Then
Exit For
End If
End If
Next
If intMatchCount >= intReqCats Then
Exit For
End If
Next
End If
HasRequiredCategory = (intMatchCount >= intReqCats)

End Function
 
S

Sue Mosher [MVP-Outlook]

Err returns 0 if there's no error and some other number if there is an error. If you check Err <> 0 at the beginning of your procedure, it will always be True. Therefore, you need to put it at the end of your procedure, after all any code which could result in an error has run.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers



SCrowley said:
I'm trying the If Err but I must be putting it in the wrong place.

I placed it under:
Function HasRequiredCategory()
Dim intMatchCount
Dim intReqCats
If Err <> 0 Then
MsgBox "Sorry, your contact will not save until you select categories as
instructed in the Red Box located to the left of the Categories box"
End If
'******USER OPTION*****
'number of category matches required
intReqCats = 2

Set objPage = Item.GetInspector.ModifiedFormPages("General")
Set objControl = objPage.Controls("lstCategories")
If gstrRequiredCats <> "" Then
arrCats = Split(UCase(Item.Categories),",")
arrRequiredCats = Split(gstrRequiredCats,";")
For I = 0 To UBound(arrCats, 1)
For J = 0 To UBound(arrRequiredCats, 1)
If Trim(arrCats(I)) = Trim(arrRequiredCats(J)) Then
intMatchCount = intMatchCount + 1
If intMatchCount >= intReqCats Then
Exit For
End If
End If
Next
If intMatchCount >= intReqCats Then
Exit For
End If
Next
End If
HasRequiredCategory = (intMatchCount >= intReqCats)

End Function
 
S

SCrowley

I'm wondering if I should use the Item_Write instead and in addition to
setting the focus back on the lstCategories box, also display a Message Box
asking the user to follow the instructions listed in Label13

I'm not sure which is the best and most succinct way to solve this issue.
Any suggestions? Thank you.

Here is the Item_Write code:

Function Item_Write()
If HasRequiredCategory() = False Then
Item_Write = False
Item.GetInspector.SetCurrentFormPage "General"
Set objPage = Item.GetInspector.ModifiedFormPages("General")
Set objControl = objPage.Controls("lstCategories")
objControl.SetFocus
End If
End Function
 
S

Sue Mosher [MVP-Outlook]

I don't think it matters. It's mainly a matter of how you want to organize your code modules.

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top