Solved

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

Posted on 2014-02-11
48
1,852 Views
Last Modified: 2014-02-27
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
Comment
Question by:lheier
  • 23
  • 20
  • 5
48 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 39854174
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
 

Author Comment

by:lheier
ID: 39854190
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
 
LVL 76

Expert Comment

by:David Lee
ID: 39854210
How about

Me.Application.ActiveInspector.CurrentItem.Sensitivity = Outlook.olSensitivity.olConfidential
0
 

Author Comment

by:lheier
ID: 39854217
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
 
LVL 76

Expert Comment

by:David Lee
ID: 39854235
Is the code you posted above all the code, or is there more?
0
 

Author Comment

by:lheier
ID: 39854238
That's everything
0
 

Author Comment

by:lheier
ID: 39854263
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
 
LVL 76

Expert Comment

by:David Lee
ID: 39854322
I've never created an add-in in VS.  How about

Outlook.Application.ActiveInspector.CurrentItem.Sensitivity = Outlook.olSensitivity.olConfidential
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39854467
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
 

Author Comment

by:lheier
ID: 39854499
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
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39855291
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
 

Author Comment

by:lheier
ID: 39856143
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
 
LVL 76

Expert Comment

by:David Lee
ID: 39856185
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
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39856389
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
 

Author Comment

by:lheier
ID: 39856450
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
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39857966
OK thx...tand by...I am investgating further.
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39857987
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
 

Author Comment

by:lheier
ID: 39858017
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
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39858045
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
 

Author Comment

by:lheier
ID: 39858102
This isn't a macro though...this is an add-in that I'm developing in Visual Studio.
0
 

Author Comment

by:lheier
ID: 39865828
Any other ideas?
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39865946
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
 

Author Comment

by:lheier
ID: 39866006
Sounds good, thanks!  I can zip up the project and upload it somewhere too if that would help.
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39866018
That would be great.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

Author Comment

by:lheier
ID: 39866044
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39866428
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
 

Author Comment

by:lheier
ID: 39877586
Any luck?
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39877753
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
 

Author Comment

by:lheier
ID: 39877764
Oh no!...Hoping for a speedy recovery!  Thanks, I really appreciate it!
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39880637
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
 

Author Comment

by:lheier
ID: 39881151
I'm using VS 2013
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39881244
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
 

Author Comment

by:lheier
ID: 39881301
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
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39881532
OK thx......working on it.

Cheers
Chris
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39882684
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
 

Author Comment

by:lheier
ID: 39882710
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
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39882737
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
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39884062
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
 

Author Comment

by:lheier
ID: 39884068
Hopefully this helps...
Form
0
 

Author Comment

by:lheier
ID: 39884087
Here are all the properties of the different ribbon components.
button.png
group.png
ribbon.png
tab.png
0
 

Author Comment

by:lheier
ID: 39884434
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
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39884592
Thanks for that! :-)   Stand by.....
0
 
LVL 13

Accepted Solution

by:
Chris Raisin earned 500 total points
ID: 39884696
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
 

Author Comment

by:lheier
ID: 39884702
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
 

Author Closing Comment

by:lheier
ID: 39884704
Can't give enough thanks to craisin.  Very patient and put in a lot of time working with me on this.  Grade A!!
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39884718
:-)    A pleasure!
0
 
LVL 13

Expert Comment

by:Chris Raisin
ID: 39889096
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
 

Author Comment

by:lheier
ID: 39892237
Thank you for the afterthought!  That did not occur to me.  I'll take a look at making these changes.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sometimes Outlook might have problems sending a message. There may be various causes- corrupted PST, AV scanner etc. The message, instead of going to the Sent Items folder, sits in the Outbox indefinitely. To remove it you can use a free tool cal…
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now