Solved

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

Posted on 2003-11-20
10
983 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:tillmanz123
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

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

Regards.
0
 

Expert Comment

by:Ozzard
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

763 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

8 Experts available now in Live!

Get 1:1 Help Now