• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2639
  • Last Modified:

Creating an Outlook Add-In (Button to toggle message sensitivity)

I'm working on developing a simple COM add-in for Outlook.  It is a button that toggles an email's sensitivity setting between "Normal" and Confidential".  Our email encryption software encrypts emails based on the "Confidential" setting in the email header.

I've got the button created and the toggle function working.  The problem is that I can't figure out how to get it to actually toggle the setting.  I'm able to toggle the setting during the Load, but cannot get it to do that during the button click event.

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)
        mailItem.Sensitivity = Outlook.OlSensitivity.olNormal
    End Sub

    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown

    End Sub

End Class

Open in new window


Public Class Encryption

    Public Sub btnEncrypt_Click(sender As Object, e As RibbonControlEventArgs) Handles btnEncrypt.Click

        If btnEncrypt.Label = "Encryption (OFF)" Then
            'Set the sensitivity to Confidential

            btnEncrypt.Label = "Encryption (ON)"

        Else
            'Set the sensitivity to Normal

            btnEncrypt.Label = "Encryption (OFF)"
        End If

        'System.Windows.Forms.MessageBox.Show("Your Ribbon Works!")
        'btnEncrypt.Label = "Encryption (OFF)"
    End Sub

End Class

Open in new window

0
lheier
Asked:
lheier
  • 23
  • 20
  • 5
1 Solution
 
David LeeCommented:
What do you mean when you say "the "Confidential" setting in the email header"?  Are you talking about an Outlook property or a field in the message header?  If the latter, what's the field name?
0
 
lheierAuthor Commented:
Outlook has a setting to mark emails as Normal, Confidential, or Private (sensitivity setting).  When you change that setting, it modifies the header information of the email.

This is the code I am using during the load of the add-on:

mailItem.Sensitivity = Outlook.OlSensitivity.olNormal

I can simply change it to .olConfidential and it will change the setting.  However, I do not know how to get that to work in the button click event.
0
 
David LeeCommented:
How about

Me.Application.ActiveInspector.CurrentItem.Sensitivity = Outlook.olSensitivity.olConfidential
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lheierAuthor Commented:
I get this in the error list:

"Error      1      'Application' is not a member of 'EncryptButton.Encryption'."

Sorry, I am no expert at programming and this is the first time I've attempted to make a COM add-in.
0
 
David LeeCommented:
Is the code you posted above all the code, or is there more?
0
 
lheierAuthor Commented:
That's everything
0
 
lheierAuthor Commented:
Let me rephrase that.  The code I posted is the only code that was not autogenereated by Visual Studio.  When I created the COM add-in, VS created a bunch of code for the layout, etc.
0
 
David LeeCommented:
I've never created an add-in in VS.  How about

Outlook.Application.ActiveInspector.CurrentItem.Sensitivity = Outlook.olSensitivity.olConfidential
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
All your click code is doing is changing the label but not actually changing the sensitivity.

Amend your even code to the following:
Public Class Encryption

Public Sub btnEncrypt_Click(sender As Object, e As RibbonControlEventArgs) _
                                            Handles btnEncrypt.Click

  If btnEncrypt.Label = "Encryption (OFF)" Then
      'Set the sensitivity to Confidential
      btnEncrypt.Label = "Encryption (ON)"
      Outlook.Application.ActiveInspector.CurrentItem.Sensitivity = _   
                              Outlook.OlSensitivity.olConfidential

  Else
     'Set the sensitivity to Normal
     btnEncrypt.Label = "Encryption (OFF)"
     Outlook.Appllication.ActiveInspector.CurrentItem.Sensitivity = _ 
                               Outlook.OlSensitivity.olNormal

  End If

End Sub

End Class

Open in new window

0
 
lheierAuthor Commented:
I should have clarified that I knew the button click event was only changing the label.  I didn't have any code in there to actually change the sensitivity property because I didn't know what to put :)

I made the following change:
Outlook.Application.ActiveInspector.CurrentItem.Sensitivity = Outlook.olSensitivity.olConfidential

Open in new window



However, I get this error: "Reference to a non-shared member requires an object reference."
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
Try using the "New" instantiator directive in the line at the head of your first code:

That is:

Private WithEvents inspectors As New Outlook.Inspectors


If the does not work, then you may need to instantiate within the body of the code
with "set inspectors = new Outlook.inspectors".

I gather that the error is happening when the object "inspectors" is being utilized.
If it is not, please step through the code using the debugger (that is , pressing F9).

Cheers

Christopher (craisin)
0
 
lheierAuthor Commented:
I tried instantiating like you suggested, but I get the following error:

'Microsoft.Office.Interop.Outlook.InspectorsClass.Friend Sub New()' is not accessible in this context because it is 'Friend'.

I also tried to instantiate within the code, but get this error:

'inspectors' is not declared. It may be inaccessible due to its protection level.
0
 
David LeeCommented:
Can you create a public variable (i.e. one that's outside of either class and visible to both?  If you can, then in ThisAddIn_Startup you can set that variable to Outlook.Application.  You should then be able to use that variable in the Encryption class.  Assuming that variable was named olkApp, then something like this

olkApp.ActiveInspector.CurrentItem.Sensitivity = Outlook.olSensitivity.olConfidential
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
Try these two things.

1. Change the declaration of of the Encryption class to :

           Public Shared Class Encryption

If that does not fix things,

2.   Change the declaration of the ThisAddIn class to:
   
          Public Shared Class ThisAddIn

Since I am not sure which call is producing the error, it is just a test to see whether
making one of the classes (or both) "Shared" may carry the instantiation into the entire
project. (If you get my drift).

It will be interesting to see if either of these work.

Have you used the debugger and worked out exactly on which line the error occurs?
0
 
lheierAuthor Commented:
Tried that but get an error: "Classes cannot be declared as shared."

I haven't tried debugging because these errors show up in the error list before I can even try to debug.
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
OK thx...tand by...I am investgating further.
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
Yes, but what you must do is open up the "Visual Basic" code within Outlook, place your cursor in the module "btnEncrypt_Click" on the following line

           If btnEncrypt.Label = "Encryption (OFF)" Then

then press "F9" (to place a BreakPoint on that line.

Then simply click on the sensitivity button and the code will start running but stop at that line.
Then you simply press "F8" to step through the code. Take not where the error message occurs (and which line was the last one to successfully run before the message occured.

Of course it may happen immediately you click on the button, which in itself will give us clues.

I await your investigation. :-)
0
 
lheierAuthor Commented:
I guess I'm confused how to do this.  I can't get it to open up into Outlook because it gives the build error.
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
You need to have the developer button showing on your desktop and then you click on
ther developer button.

You will then see "VB Editor" button and when you click on it, you will be taken into the code of the macros held within Outlook.

If the developer button is not showing on your desktop, you need to go into the settings for the Ribbon and ensure that the "developer" item is checked tso that is shows on the ribbon.

If you are still unclear how to do this I can prepare a short Video showing how to do it.

Please just clarify what version of Outlook you are using (2008, 2009,2010, 386)
0
 
lheierAuthor Commented:
This isn't a macro though...this is an add-in that I'm developing in Visual Studio.
0
 
lheierAuthor Commented:
Any other ideas?
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
OK. I will look at it a boit closer along the lines of coding within the VB.Net module.
Pressedfor time at the moment but will give it some thought after I finish teaching tonight.

Cheers
Chris
0
 
lheierAuthor Commented:
Sounds good, thanks!  I can zip up the project and upload it somewhere too if that would help.
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
That would be great.
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
Thanks for that,,,,,,working on it, but will be teaching in about 6 hours so may net get back to you until midnight our time (11 or hours from now)....
Cheers
Chris
0
 
lheierAuthor Commented:
Any luck?
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
Sorry for the dealy.

Just recovering from sudden problems health-wise which left me at the doctorsfor three hours yesterday with lots of tests (blood tests etc.)

No problems, just a virus I think.

I will work on your problem after Church today and all tomorrow (Sunday) to nail a solution.

Cheers
Chris
0
 
lheierAuthor Commented:
Oh no!...Hoping for a speedy recovery!  Thanks, I really appreciate it!
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
When I load your Project into Visual Studio 2010 it says "This project is incompatible wih this version of Visual Studio".

What version are you using?

(P.S. I am still not well...but recovering slowly).
0
 
lheierAuthor Commented:
I'm using VS 2013
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
OK.

Can you please paste your main code into this question and I will see if it works within VS2010 else (I guess) I will have to upgrade!  :-)
0
 
lheierAuthor Commented:
There are a ton of files that are autocreated by VS...

The main code file for the Ribbon contains this:

Imports Microsoft.Office.Tools.Ribbon

Public Class EncryptButton

    Private Sub Ribbon1_Load(ByVal sender As System.Object, ByVal e As RibbonUIEventArgs) Handles MyBase.Load

    End Sub

    Private Sub btnEncrypt_Click(sender As Object, e As RibbonControlEventArgs) Handles btnEncrypt.Click
        If btnEncrypt.Label = "Encryption (OFF)" Then
            'Set the sensitivity to Confidential
            Outlook.Application.ActiveInspector.CurrentItem.Sensitivity = Outlook.OlSensitivity.olConfidential
            btnEncrypt.Label = "Encryption (ON)"
        Else
            'Set the sensitivity to Normal
            Outlook.Application.ActiveInspector.CurrentItem.Sensitivity = Outlook.OlSensitivity.olNormal
            btnEncrypt.Label = "Encryption (OFF)"
        End If
    End Sub
End Class

Open in new window

0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
OK thx......working on it.

Cheers
Chris
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
Well, I cannot run your code without VS2013 which would cost me too much.
(I will download Express and try it out).

My guess though is that the  code declaration should be made "shared".

Try the following code instead:

Imports Microsoft.Office.Tools.Ribbon

Public Class EncryptButton

Private Sub Ribbon1_Load(ByVal sender As System.Object, _
                                          ByVal e As RibbonUIEventArgs) Handles MyBase.Load
              '..........................
              'Place code here as required
              '.........................
End Sub

Public Shared Sub btnEncrypt_Click(sender As Object, e As RibbonControlEventArgs) _
                                                        Handles btnEncrypt.Click
        If btnEncrypt.Label = "Encryption (OFF)" Then
            'Set the sensitivity to Confidential
            Outlook.Application.ActiveInspector.CurrentItem.Sensitivity = _
                                                       Outlook.OlSensitivity.olConfidential
            btnEncrypt.Label = "Encryption (ON)"
        Else
            'Set the sensitivity to Normal
            Outlook.Application.ActiveInspector.CurrentItem.Sensitivity = _ 
                                                       Outlook.OlSensitivity.olNormal
            btnEncrypt.Label = "Encryption (OFF)"
        End If
    End Sub
End Class

Open in new window


Changing the "Click" sub to be publicly available and shared might resolve that latest message you are getting.

This is just a supposition, not being able to test it myself. One I have "Express" I will try it out.

Another thought is, are you using Outlook 2013 (cloud version) or Outlook 2013/earlier installed on desktop? I have had several issues with OTHER applications when using Outlook 2013 supplied in Office 2013 (subscription via cloud). I had to completely uninstall earlier versions of Office (2010 in my case) to get everything to work OK.
Make sure you do not have multiple version of Office on the same machine.

Let me know how you go.

Cheers
Chris
0
 
lheierAuthor Commented:
I get the following error:
Errors
I actually downloaded a full trial version of VS 2013.  I think I got it off of Microsoft's site.
http://www.visualstudio.com/en-us/downloads
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
That trial is no longer available, I think, but I am downloading VS2013 Express to see whether that will handle the code (it should). I cannot see the sense in paying $500 to upgrade from 2010 to 2013 (seeing how I am retired now).  :-)

Stand by. I will try things out in Express.
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
Do you use a form in this project?

If so, could you show a screen grab of it? I cannot import the code direct into VS2013 Express (incompatible for some reason).

If I can see how your "ribbon" is set up I may be able to replicate.
0
 
lheierAuthor Commented:
Hopefully this helps...
Form
0
 
lheierAuthor Commented:
Here are all the properties of the different ribbon components.
button.png
group.png
ribbon.png
tab.png
0
 
lheierAuthor Commented:
I've re-created it in VS 2010.  Happened to find a trial version available.  Here it is zipped up: https://www.dropbox.com/s/jor3y8s7qrx0jl3/EncryptButton2010.zip

Oh, and I am using Outlook 2010.  That's the only installed version.
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
Thanks for that! :-)   Stand by.....
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
Try this change to the code:
   Private Sub btnEncrypt_Click(ByVal sender As System.Object, ByVal e As Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs) Handles btnEncrypt.Click
        Dim oOutlook As New Outlook.Application
        Dim oItem As Outlook.MailItem = oOutlook.ActiveInspector.CurrentItem
        If btnEncrypt.Label = "Encryption (OFF)" Then
            'Set the sensitivity to Confidential
            oItem.Sensitivity = Outlook.OlSensitivity.olConfidential
            btnEncrypt.Label = "Encryption (ON)"
        Else
            'Set the sensitivity to Normal
            oItem.Sensitivity = Outlook.OlSensitivity.olNormal
            btnEncrypt.Label = "Encryption (OFF)"
        End If
    End Sub
End Class

Open in new window

0
 
lheierAuthor Commented:
That works beautifully!!  I can't thank you enough for the help.  I definitely couldn't have done it without your assistance.  Thank you for all the time you've put into this!!
0
 
lheierAuthor Commented:
Can't give enough thanks to craisin.  Very patient and put in a lot of time working with me on this.  Grade A!!
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
:-)    A pleasure!
0
 
Chris Raisin(Retired Analyst/Programmer)Commented:
Just an afterthought.......

If you move from one item to the next in the Outlook Inspector, the button should change to reflect the sensitivity of the currently selected item.

Perhaps some code should be added to handle the change of item as follows:

Publicly declare at top of module: "ThisOutlookSession" the following declaration:
     
       Public WithEvents myOlItems As Outlook.Items

Then within your "Initialize_Handler" have code such as:
Sub Initialize_handler()
      Set myOlItems = olMail.ActiveInspector.Items
End Sub

Open in new window


Private Sub myOlItems_ItemChange(ByVal Item As Object)
        Dim oOutlook As New Outlook.Application
        Dim oItem As Outlook.MailItem = oOutlook.ActiveInspector.CurrentItem
        If oItem.oItem.Sensitivity = Outlook.OlSensitivity.olConfidential then
            btnEncrypt.Label = "Encryption (ON)"
        Else
            btnEncrypt.Label = "Encryption (OFF)"
        End If
    End Sub

Open in new window


I have not tested this code, but it is worth considering, else the button will not reflect the state of the currently highlighted item.

Cheers
Chris
0
 
lheierAuthor Commented:
Thank you for the afterthought!  That did not occur to me.  I'll take a look at making these changes.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 23
  • 20
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now