Solved

Adding Images to CommandBarButtons in Office

Posted on 2004-08-19
7
309 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Accepted Solution

by:
Tommy Kinard earned 125 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

743 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

16 Experts available now in Live!

Get 1:1 Help Now