开发者

Options in VBA Outlook Add-in

开发者 https://www.devze.com 2023-04-12 11:59 出处:网络
I have been asked to implement a solution whereby staff must designate a security level for new emails (these are filtered by the exchange server). What I am wanting to do is have it so when a new ema

I have been asked to implement a solution whereby staff must designate a security level for new emails (these are filtered by the exchange server). What I am wanting to do is have it so when a new email is sent, a dialogue box comes up with options of the security level i.e. PERSONAL, UNCLASSIFIED, CLASSIFIED etc, and then append to the back of subject line [SEC=variable]. At the moment I have the code below, however this clearly has to be manually modified and I don't want staff to have to do that (and they won't).

Public Class ThisAddIn

Private WithEvents inspectors As Outlook.Inspectors

Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup
    inspectors = Me.Application.Inspectors
End Sub

Private Sub inspectors_NewInspector(ByVal Inspector As Microsoft.Office.Interop.Outlook.Inspector) Handles inspectors.NewInspector
    Dim mailItem As Outlook.MailItem = TryCast(inspector.CurrentItem, Outlook.MailItem)
    If Not (mailItem Is Nothing) Then
        If mailItem.EntryID Is Nothing Then
            mailItem.Subject = "[SEC=UNCLASSIFIED开发者_StackOverflow中文版]"
        End If
    End If
End Sub

Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

End Sub
End Class

Any assistance to get this dialogue box going would be greatly appreciated. I am using Visual Studio 2010 with Outlook 2010.


As Gaffi suggested, use the ItemSend Event to display a userform where your users can select a sending option. I'm afraid I don't know how to do that in Visual Studio, only in VBA. Hopefully you can translate this code.

I created a sample userform with radio buttons so only one option can be selected.

Options in VBA Outlook Add-in

The only code behind the form is to unload the form:

Private Sub CommandButton1_Click()
  Unload Me
End Sub

In the ThisOutlookSession module (the built-in class module for Outlook), the following code creates the form when an email is sent, asking the sender to classify the email:

Private Sub Application_ItemSend(ByVal Item As Object, Cancel As Boolean)

  Dim frm As UserForm1
  Dim chosenvalue As String

  Set frm = New UserForm1

  frm.Show vbModal

  Select Case True
  Case frm.OptionButton1.Value
    chosenvalue = "PERSONAL"
  Case frm.OptionButton2.Value
    chosenvalue = "UNCLASSIFIED"
  Case frm.OptionButton3.Value
    chosenvalue = "CLASSIFIED"
  Case Else  ' no value chosen
    MsgBox "you did not select a value. cancelling send."
    Cancel = True
    Exit Sub
  End Select

  If TypeName(Item) = "MailItem" Then
    Item.Subject = Item.Subject & " [SEC=" & chosenvalue & "]"
  End If

End Sub

Since we create the form outside the class module, we can read what radio button was selected even after the form is closed. Depending on the selection, the subject is modified. If no selection is made, the ItemSent event is cancelled and the user is forced to hit Send again and select something from the userform.

0

精彩评论

暂无评论...
验证码 换一张
取 消