Consulting

Results 1 to 4 of 4

Thread: Equivalent of try...catch

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Jul 2010
    Posts
    29
    Location

    Equivalent of try...catch

    Hi,

    I'm changing the Sensitivity option in code dependent on some criteria, using Application_ItemSend

    With Item
    .Sensitivity = 2 or 0 depending on criteria
    End With
    What I need to do is catch if somebody tries to change it from Private back to Normal because this isn't allowed. This does work:

    If .Sensitivity <> olNormal Then GoTo ExitHandler
    What would be better though is if the check checks if the value can be changed rather than just checking what it's current value is. If an email is the first to be changed to private it can be changed back and forth legitimately. Only if it isn't the first to be changed can it not be changed back.

    I'm using On Error Goto ErrorHandler at the top of this routine.

    Sub SetEmailSensitivity(ByVal value As String)
    
    
        On Error GoTo ErrorHandler
        
        Dim Outlook As Outlook.Application
        Dim Inspector As Outlook.Inspector
        Dim Subject As String
        
        Set Outlook = New Outlook.Application
        Set Inspector = Outlook.ActiveInspector
        
        Select Case TypeName(Outlook.ActiveWindow)
        
            Case "Explorer"
            
                Set Item = Outlook.CreateItem(olMailItem)
                
            Case "Inspector"
            
                Set Item = Inspector.CurrentItem
                
        End Select
        
        With Item
        
            If .Sensitivity <> olNormal Then GoTo ExitHandler 'would want to go to exit handler only if Sensitivity cannot be changed
            .Sensitivity = value 'this errors out if a reply or forward tries to change back to normal
            Subject = .Subject
            
            If InStr(1, Subject, "[PRIVATE] ", vbTextCompare) > 0 Then Subject = Replace(Subject, "[PRIVATE] ", vbNullString)
            
            Select Case value
            
                Case olPrivate
                
                    If Left(Subject, InStr(1, Subject, ":", vbTextCompare)) = "RE:" Or Left(Subject, InStr(1, Subject, ":", vbTextCompare)) = "FW:" Then
                    
                        Subject = Left(Subject, InStr(1, Subject, ":", vbTextCompare) + 1) & "[PRIVATE]" & Mid$(Subject, InStr(1, Subject, ":") + 1, Len(Subject))
                        
                    Else
                    
                        Subject = "[PRIVATE] " & Subject
                        
                    End If
                    
            End Select
            
            .Subject = Subject
            If TypeName(Outlook.ActiveWindow) = "Explorer" Then .Display
            
        End With
        
    ExitHandler:
        
        On Error Resume Next
        
        If Not Item Is Nothing Then Set Item = Nothing
        If Not Inspector Is Nothing Then Set Inspector = Nothing
        If Not Outlook Is Nothing Then Set Outlook = Nothing
        
        Exit Sub
        
    ErrorHandler:
        
        MsgBox Err.Number & ": " & Err.Description, vbExclamation, "Error"
        Resume ExitHandler
        
    End Sub
    Last edited by ldoodle; 01-29-2019 at 09:39 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •