Link to home
Start Free TrialLog in
Avatar of masj78
masj78

asked on

Adding Images to CommandBarButtons in Office

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
Avatar of Tommy Kinard
Tommy Kinard
Flag of United States of America image

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

Avatar of masj78
masj78

ASKER

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.
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

ASKER CERTIFIED SOLUTION
Avatar of Tommy Kinard
Tommy Kinard
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi masj78,

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

dragontooth

Avatar of masj78

ASKER

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!
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