Add Button to MS Outlook (>=2000) toolbar with VB .NET

Hi!

Built a new project from scratch. I added the references to MS.Office.Core and MS.Office.Outlook COMs.
I even upgraded to the the Office .NET TypeLibrary (that was somehow suggested by MSDN).

Now someone PLEASE tell me: what am I doing wrong?

Regards, Tillman


Imports Outlook = Microsoft.Office.Interop.Outlook
Imports Microsoft.Office.Core

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Const msoControlButton = 6
        Dim olApp As Outlook.Application
        Dim myButton As Microsoft.Office.Core.CommandBarButton

        olApp = CreateObject("Outlook.Application")
       
        myButton = olApp.ActiveExplorer.CommandBars("File").Controls.Add(msoControlButton)

        With myButton
            .name = "Whatever"
            .Caption = "TestButton"

        End With
End Sub
tillmanz123Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

wsteegmansCommented:
Did some testing ...

What do you mean with "File" ... ? It's not a commandbar, it's a menu ...

Anyways, the Standard CommandBar in Outlook is called "Standard". But, you can't add buttons to the Standard CommandBar.

So, try this ... Create a new CommandBar, say  'custom 1'
Also, msoControlButton is not equal to 6, but to 1. But, if you insert your Libraries, you don't have to set them ... ??
Other remark. If you create a new CommandBar, it doens't have the property 'Name'. So, just set 'Caption'

Try this code ... (VBA, not VB.Net)!! So, you must do some small changes ...

        Dim olApp As Outlook.Application
        Dim myButton As Variant

        Set olApp = CreateObject("Outlook.Application")
       
        Set myButton = olApp.ActiveExplorer.CommandBars("custom 1").Controls.Add(msoControlButton)
        With myButton
            .Caption = "TestButton"
        End With

If you give no parameter with the Add Method, it takes by default msoControlButton.

Hope this helps.

Kind Regards.

tillmanz123Author Commented:
Ok, this is where it's getting weired.

If I don't set msoControlButton it will not take it. Also the "olApp.ActiveExplorer.CommandBars" part gets underlined with a tooltip saying:
Reference required to assembly "office" containing the type "Microsoft.Office.Core.CommandBars". Add one to your project.

What can I do more than adding the reference MS Office Outlook + MS Office Core to the project....

Thanks for mentioning that I can't add buttons to the "standard" toolbar. Nevertheless - WinFax did exactly that.


I really appreciate your help!

Regards,

Tillman
wsteegmansCommented:
Hi Tillman,

Did the job in VB.Net ;-)

Just reference your project to Microsoft Outlook x.0 Object Library, where x=9 or 10
Then, you see indeed in your references the refferences Office and Outlook.

OK. Past this code. It creates a new Menu-Item in the File-Menu (it works!) ...
I looked for the code by browsing the Office and Outlook Libraries (via the Object Browser)

Imports Outlook
Imports Office

Public Class Form1
    Inherits System.Windows.Forms.Form

    ...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim olApp As Outlook.Application
        Dim myButton As Office.CommandBarButton

        olApp = New Outlook.ApplicationClass

        myButton = olApp.ActiveExplorer.CommandBars("File").Controls.Add(Office.MsoControlType.msoControlButton)

        With myButton
            .Caption = "TestButton"
        End With

    End Sub
End Class
   

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

tillmanz123Author Commented:
Obviously there is someting going majorly wrong at my side...(User too stupid error? :-)  )

First of all, I had to do the import statements as follows, yours did not work:

Imports Outlook = Microsoft.Office.Interop.Outlook
Imports Office = Microsoft.Office.Core

And for: olApp.ActiveExplorer.CommandBars - I get the "reference required to assembly 'office' containing (...) CommandBars (...)"

As for the reference object, I included "Microsoft Outlook 11.0 Object Library" and "Microsoft Office 11.0 Object Library" - should work, shouldn't it.

Thanks for your patience! ;-)


Regards,

Tillman

-> Points up to 400 now *g
wsteegmansCommented:
Seems that the Classes in Outlook 11 are changed ... ??

With Outlook 10, I don't have these
Imports Outlook = Microsoft.Office.Interop.Outlook
Imports Office = Microsoft.Office.Core

So, I think we can conclude that Outlook 11 really changed object Structure

I have to check it @ Home, because don't have Outlook 11 on the Office ...
Come back to you later ...
tillmanz123Author Commented:
I'm gonna try the 10.0 libs, but I got some feeling that my prob lies somewhere else...

Hopefully you'll come up with some magic trick ;-)


Regards,

Tillman


tillmanz123Author Commented:
Unbeliveable - I put your code into a sample project from "Sybex: Mastering VB .NET" and it worked like a charm.

Now I gotta find out why my project from scratch did not work....if you have any suggestions, please let me know.


The points are all yours of course :-)


Regards,

Tillman
tillmanz123Author Commented:
The difference is obviously the reference part. With my new project I added the COMs: MS Office 11.0 and MS Outlook 11.0 (also tried the 10.0 versions). When I then check the solutions explorer the Office lib show up fully qualified as "Microsoft.Office.Core" - whereas in the sample project the solutions explorer would show: "Office". When I rightclick and check the properties it will state as name (ghosted): Interop.Microsoft.Office.Core

Since it works with the sample project, I could start from there BUT as you probabely will understand it REALLY bugs me that I don't know why my version doesn't work.

Any ideas?


Regards, Tillman
wsteegmansCommented:
I'll try it tomorrow on my Home PC ...
Come back to you soon

Regards.
OzzardCommented:
To add a pointer for anyone else who reads this... you *can* add a button to the Standard toolbar, at least in Outlook XP.  To reproduce this, create a new Mail item, flip it into design mode, paste the code below into its code window, and run the form.  It adds 'Boo!' just after the Send button.

Function Item_Open()
      Dim bar
      Dim c
      Dim i
      Dim ctl

      Dim msoControlButton
      msoControlButton = 1

      i = 1
      Set bar = Item.GetInspector.CommandBars("Standard")
      For Each c In bar.Controls
            If c.Caption = "&Send" Then
                  Set ctl = bar.Controls.Add (msoControlButton, , , i + 1, True)
                  With ctl
                        .Caption = "Boo!"
                  End With
                  Exit For
            End If
            i = i + 1
      Next
End Function
   
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.