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

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
0
tillmanz123
Asked:
tillmanz123
  • 5
  • 4
1 Solution
 
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.

0
 
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
0
 
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
   
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy 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
0
 
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 ...
0
 
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


0
 
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
0
 
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
0
 
wsteegmansCommented:
I'll try it tomorrow on my Home PC ...
Come back to you soon

Regards.
0
 
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
   
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

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