Solved

Adding Images to CommandBarButtons in Office

Posted on 2004-08-19
7
311 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
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…

910 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

21 Experts available now in Live!

Get 1:1 Help Now