Solved

Adding Images to CommandBarButtons in Office

Posted on 2004-08-19
7
314 Views
Last Modified: 2013-12-25
Hi there,

I am trying to add an image to a toolbar button I have created in Outlook. I have tried 2 methods I have found on the Internet. One being the suggested Microsoft method and I cannot get either to work.
The first way is the Microsft method:

      Dim objPicture As stdole.IPictureDisp
      Const PICTURE_PATH As String = "C:\tickok.bmp"
      Set olApp = Application
      Set oNS = olApp.GetNamespace("MAPI")
      Set oCBs = olApp.ActiveExplorer.CommandBars
      Set oMenuBar = oCBs.Add("My ToolBar", , , True)
      Set objPicture = LoadPicture(PICTURE_PATH)
      Set menuButton = oMenuBar.Controls.Add(msoControlButton, , , , True)

      menuButton.Picture = objPicture
      menuButton.Caption = "My Button"


The problem with this first one is that the compiler doesn't recognise ".Picture" as a valid property. This is the point at which the image is supposed to be added.
I also tried a method from a microsoft professional, which uses the clip board and a picture box on a form with my "tickok.bmp" picture placed into it:

      Set olApp = Application
          Set oNS = olApp.GetNamespace("MAPI")
          Set oCBs = olApp.ActiveExplorer.CommandBars
          Set oMenuBar = oCBs.Add("Cmis Timetable", , , True)
      Set menuButton = oMenuBar.Controls.Add(msoControlButton, , , , True)
      
      Clipboard.SetData (LogoForm.Picture1.Image)

      With menuButton
              .Caption = "Cmis Timetable"
              .PasteFace
              .Visible = True
          End With
      
The problem here is that the line "Clipboard.SetData (LogoForm.Picture1.Image)" is coming up as a type mismatch. This method seems to want a stDole.IPictureDisp object not a reference to an image on a form. I have tried creating this using

      Dim objPicture As stdole.IPictureDisp
      Const PICTURE_PATH As String = "C:\tickok.bmp"
      Set objPicture = LoadPicture(PICTURE_PATH)
      Clipboard.SetData (objPicture)

Again this is not excepted as a valid parameter and gives a type mismatch(???). What type of object is it expecting??
Does anyone have a solution that they could advise me trying or errors in the 2 methods, which I am missing.

Many Thanks

Mike Jones
Developer
0
Comment
Question by:masj78
  • 5
  • 2
7 Comments
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 11842523
Hi masj78,

This may be what you are looking for, the .style property, you need to tell it what kind of face to use. Also the .bmp may need to be converted to an icon.

.Style = msoButtonIconAndCaption    'icon and caption
'.Style = msoButtonCaption          'caption only
'.Style = msoButtonIcon             'icon only

HTH
dragontooth

0
 

Author Comment

by:masj78
ID: 11868712
Many thanks for the help. I have tried your suggestion and I keep getting a Method Object Failed error. The code I am trying to run is:

     With olApp.ActiveExplorer.CommandBars.FindControl(msoControlButton, intButtonID)
        .Style = msoButtonIconAndCaption
        .Picture = objPicture
        .Caption = "My Button"
    End With

I forgot to mention that the button is part of a COM object, which runs in Microsoft Outlook. This runs fine with just the button's caption property set. Not sure if this may have something to do with the problem, though I am not sure it would, as articles on Microsoft indicate it should work fine.
If you have any more suggestions or have seen a working example then they would be greatly appreciated. Many Thanks.
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 11869930
Hi masj78,

The COM/Add-In Object model is almost the same thing. Where is the error occuring? I think just looking at the code it is at .Picture = objPicture I think it should be PasteFace. I will have to find this, I have seen it before.

In the help file it is:
Set myControl = CommandBars.FindControl(Type:=msoControlButton, Id:=2)
myControl.CopyFace
Set myControl = CommandBars.FindControl(Type:=msoControlButton, Id:=23)
myControl.PasteFace

But this is for an existing commandbarbutton face I have seen the code to paste a bitmap onto the face of a commandbutton let me look.

dragontooth

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 14

Accepted Solution

by:
Tommy Kinard earned 125 total points
ID: 11873270
OK I found it.

I am not familar with the FindControl, but you will need to add a resource file to store the bitmap.

 With olApp.ActiveExplorer.CommandBars.FindControl(msoControlButton, intButtonID)
        .Style = msoButtonIconAndCaption
        Clipboard.SetData LoadResPicture(101, vbResBitmap) '<- the last arg tells it it is a bitmap (of course)
'                                                          ^ bitmap number assigned in the resource file
        .PasteFace
        .Caption = "My Button"
    End With
What that does in short is load the bitmap from the resource file and copy it to the clipboard. Then it is just a paste.
The Bitmap is 16x16, mine are just 16 colors.

HTH
dragontooth

0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 11891862
Hi masj78,

Just a follow up. Did this work or do you need to load the bitmap from a file?

dragontooth

0
 

Author Comment

by:masj78
ID: 11902327
Your solution worked fine. Many Thanks Dragontooth. However I did need to read up first on how to add a resource to my project, which was what was causing the problems all along I think.
Many of the articles I read on adding icons to butttons and toolbars failed to mention about this.
For reference here is a very good article, which helped me toward the final solution, along with your code:

http://www.ibirbal.com/downloads/vbrfkn.pdf

Thanks Again!
0
 
LVL 14

Expert Comment

by:Tommy Kinard
ID: 11902469
Thank You for the Points and the Grade!

So you know how to add a resource file, add the resource editor in the VB add-in, and how to put the bitmap into the resource file. Right?

Glad I could help.
dragontooth

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

825 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