Solved

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

Posted on 2003-11-20
10
988 Views
Last Modified: 2007-12-19
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
Comment
Question by:tillmanz123
  • 5
  • 4
10 Comments
 
LVL 7

Expert Comment

by:wsteegmans
ID: 9789676
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
 

Author Comment

by:tillmanz123
ID: 9789768
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
 
LVL 7

Accepted Solution

by:
wsteegmans earned 500 total points
ID: 9790124
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
Technology Partners: 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!

 

Author Comment

by:tillmanz123
ID: 9790460
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
 
LVL 7

Expert Comment

by:wsteegmans
ID: 9790508
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
 

Author Comment

by:tillmanz123
ID: 9790597
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
 

Author Comment

by:tillmanz123
ID: 9791016
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
 

Author Comment

by:tillmanz123
ID: 9791672
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
 
LVL 7

Expert Comment

by:wsteegmans
ID: 9791745
I'll try it tomorrow on my Home PC ...
Come back to you soon

Regards.
0
 

Expert Comment

by:Ozzard
ID: 9937150
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

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 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