Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Outlook / Programming VBA / September 2005

Tip: Looking for answers? Try searching our database.

Trouble creating an Error Handler correctly

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
SCrowley - 15 Sep 2005 21:47 GMT
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  
Signature

Thank you,
scrowley@littleonline.com

Sue Mosher [MVP-Outlook] - 15 Sep 2005 23:13 GMT
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?
Signature

Sue Mosher, Outlook MVP
  Author of Configuring Microsoft Outlook 2003
    http://www.turtleflock.com/olconfig/index.htm
  and Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> Hello,
>
[quoted text clipped - 48 lines]
> Exit Function
> End Function  
SCrowley - 16 Sep 2005 14:08 GMT
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?
Signature

Thank you,
scrowley@littleonline.com

> 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?
> > Hello,
[quoted text clipped - 49 lines]
> > Exit Function
> > End Function  
Sue Mosher [MVP-Outlook] - 16 Sep 2005 14:21 GMT
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.
Signature

Sue Mosher, Outlook MVP
  Author of Configuring Microsoft Outlook 2003
    http://www.turtleflock.com/olconfig/index.htm
  and Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> 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
[quoted text clipped - 56 lines]
>> > Exit Function
>> > End Function  
SCrowley - 16 Sep 2005 15:07 GMT
Thank you very much.
Signature

Thank you,
scrowley@littleonline.com

> If Err <> 0 Then
>     MsgBox "Your text goes here"
[quoted text clipped - 61 lines]
> >> > Exit Function
> >> > End Function  
SCrowley - 16 Sep 2005 15:38 GMT
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

Signature

Thank you,
scrowley@littleonline.com

> If Err <> 0 Then
>     MsgBox "Your text goes here"
[quoted text clipped - 61 lines]
> >> > Exit Function
> >> > End Function  
Sue Mosher [MVP-Outlook] - 16 Sep 2005 15:52 GMT
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.

Signature

Sue Mosher, Outlook MVP
  Author of Configuring Microsoft Outlook 2003
    http://www.turtleflock.com/olconfig/index.htm
  and Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> I'm trying the If Err but I must be putting it in the wrong place.
>
[quoted text clipped - 98 lines]
>> >> > Exit Function
>> >> > End Function  
SCrowley - 16 Sep 2005 21:01 GMT
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

Signature

Thank you,
scrowley@littleonline.com

> 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.
>
[quoted text clipped - 100 lines]
> >> >> > Exit Function
> >> >> > End Function  
Sue Mosher [MVP-Outlook] - 16 Sep 2005 21:18 GMT
I don't think it matters. It's mainly a matter of how you want to organize your code modules.

Signature

Sue Mosher, Outlook MVP
  Author of Configuring Microsoft Outlook 2003
    http://www.turtleflock.com/olconfig/index.htm
  and Microsoft Outlook Programming - Jumpstart for
    Administrators, Power Users, and Developers
    http://www.outlookcode.com/jumpstart.aspx

> 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
[quoted text clipped - 119 lines]
>> >> >> > Exit Function
>> >> >> > End Function  
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.