Solved

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

Posted on 2003-11-20
10
985 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

777 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