Solved

Adding Images to CommandBarButtons in Office

Posted on 2004-08-19
7
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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

691 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