Consulting

Results 1 to 4 of 4

Thread: Equivalent of try...catch

  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.

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Top of my head pseudo-code ---

    On Error Goto ErrorHandler
    
    …
    …
    
    
    If .Sensitivity <> olNormal Then Err.Raise 10000
    … … ErrorHandler: If Err.Number = 10000 Then Msgbox "Can't change Sensitivity" .Sensitivity = olNormal Resume ….
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Regular
    Joined
    Jul 2010
    Posts
    29
    Location
    Thanks Paul. I did think of that but the problem is when the sensitivity is being changed to private for the first time, it can be changed back as long as the email has not been sent. Only once an email is sent does the sensitivity option become read only, unless it is set to Normal.

    So if somebody presses the private button I've added to the ribbon accidentally and this is the first time the sensitivity option is being change from Normal, they wouldn't be able to put it back to Normal without closing the email and starting again.

    Can VBA check the state of a property?

    If .Sensitivity = ReadOnly Then Err.Raise 123456
    Last edited by ldoodle; 01-30-2019 at 03:07 AM.

  4. #4
    VBAX Regular
    Joined
    Jul 2010
    Posts
    29
    Location
    I managed to sort it. I used MFCMAPI to look at all the properties of an email and came across PR_ORIGINAL_SENSITIVITY. This is always olNormal if the option has not been changed before, and is always what the chain is locked to if set in a previous message.

    If .PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x002E0003") <> olNormal Then GoTo ExitHandler
    An email being changed for the first time (whether a brand new or existing chain) can be changed back and forth
    An email being replied to that is not olNormal hits the If statement and goes to the exit handler.

    Thanks for your reply Paul.

Posting Permissions

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