PDA

View Full Version : [SOLVED:] Equivalent of try...catch



ldoodle
01-29-2019, 09:07 AM
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

Paul_Hossler
01-29-2019, 06:45 PM
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

….

ldoodle
01-30-2019, 02:46 AM
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

ldoodle
01-30-2019, 09:08 AM
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.