none
Assigning a Mso picture to a control at run time RRS feed

  • Question

  • Hi all,

    In VBA, one can assign a built-in image to a control by using the following code:
    label1.Picture = Application.CommandBars.GetImageMso("HappyFace", 16, 16)

    I'd like to know how to get this done too in VSTO coding.

    I tried:
    Globals.ThisWorkbook.Application.CommanBars ... but got an error and couldn't figure out the meaning of it and the solution.

    Please, can anyone help me out?

    TIA.
    Raymond
    A beginner at VSTO

    Sunday, October 21, 2012 9:32 AM

Answers

  • Hi Raymond

    In this article

    http://msdn.microsoft.com/en-us/library/aa338202(office.12).aspx

    near the very end, you'll find a section labelled "Loading Images". It gives code for a PictureConverter class. Copy that code into your solution. You want to use the function PictureDispToImage; you pass it the result of GetImageMso and it should return an Image that can be used for the Label.

    Note: it doesn't matter that this article was written for the Ribbon, the PictureConverter class is generally useable for anything that needs to convert between these graphics file formats.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 23, 2012 6:29 AM
    Moderator
  • Hi Cindy,

    I got it :-)

    I copy the class of that article and used the function
    PictureDispToImage, changed 'Image'
    into 'Object' because of an error 'Type not declared' that I couldn't solve with a few try
    of 'Dim' statements and bingo!

    Code finaly looks like this:
    Label1.Image = PictureConverter.PictureDispToImage( _
    Globals.ThisWorkbook.Application.CommandBars.GetImageMso("HappyFace", 32, 32))

    Thank you very much, Cindy!
    Regards,
    Raymond.

    • Marked as answer by Tokinabo Tuesday, October 23, 2012 11:30 AM
    Tuesday, October 23, 2012 11:30 AM

All replies

  • Hi Raymond

    1. Version of Office involved?

    2. What's the error message?

    3. Please show us the complete code line that's causing the error message

    4. Are you working with the Ribbon Designer or Ribbon XML?


    Cindy Meister, VSTO/Word MVP, my blog

    Sunday, October 21, 2012 4:23 PM
    Moderator
  • Hi Cindy,
    Thx for your reply.

    1)
    Version of Office involved is 2010, Excel

    2)
    Error message:
    Unable to cast COM object of type 'System.__ComObject' to class type 'System.Drawing.Image'. Instances of types that represent COM components cannot be cast to types that do not represent COM components; however they can be cast to interfaces as long as the underlying COM component supports QueryInterface calls for the IID of the interface.

    3)
    The code that generated the error:

    Private Sub ThisWorkbook_Startup() Handles Me.Startup
            Dim frm As New frm_Ribbon
            With frm
                .Show()
                .Label1.Image = _ Globals.ThisWorkbook.Application.CommandBars.GetImageMso("HappyFace", 16, 16)
            End With
     End Sub

    4)
    None of them. As you can detect from my code, I was trying to assign a idMso picture to a label on a UserForm.

    Gr,
    Raymond.

    Sunday, October 21, 2012 9:34 PM
  • Hi Raymond

    Thank you for the additional information :-)

    If you look at the Language Reference on GetImageMso you'll see that it returns an object of type IPictureDisp. This is a "COM thingy" and is not the same as a .NET Framework Image - you need a conversion.

    Do an Internet search on the following terms and you should turn up a number of suggestions and approaches: convert IPictureDisp Image


    Cindy Meister, VSTO/Word MVP, my blog

    Monday, October 22, 2012 1:21 PM
    Moderator
  • Thx Cindy,

    I looked for IPictureDisp stuff on the web and found a lot of blabla that sounds Chinese to me :-)

    I understand from your answer and what I read on the net that I should forget the idea that it can easily be done as in VBA with just one line of code!
    Right now I'm frustrated but that is part of every beginning process, right? :-)

    At least, I need a link to a place where such code is given and maybe explaned (the different properties/arguments/parameters and what they might do or mean).

    I just want that little tiny Microsoft built-in "HappyFace" assigned to a label on a userform ...

    Gr,
    Raymond

    Monday, October 22, 2012 2:22 PM
  • Hi Raymond

    In this article

    http://msdn.microsoft.com/en-us/library/aa338202(office.12).aspx

    near the very end, you'll find a section labelled "Loading Images". It gives code for a PictureConverter class. Copy that code into your solution. You want to use the function PictureDispToImage; you pass it the result of GetImageMso and it should return an Image that can be used for the Label.

    Note: it doesn't matter that this article was written for the Ribbon, the PictureConverter class is generally useable for anything that needs to convert between these graphics file formats.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, October 23, 2012 6:29 AM
    Moderator
  • Hi Cindy,

    I got it :-)

    I copy the class of that article and used the function
    PictureDispToImage, changed 'Image'
    into 'Object' because of an error 'Type not declared' that I couldn't solve with a few try
    of 'Dim' statements and bingo!

    Code finaly looks like this:
    Label1.Image = PictureConverter.PictureDispToImage( _
    Globals.ThisWorkbook.Application.CommandBars.GetImageMso("HappyFace", 32, 32))

    Thank you very much, Cindy!
    Regards,
    Raymond.

    • Marked as answer by Tokinabo Tuesday, October 23, 2012 11:30 AM
    Tuesday, October 23, 2012 11:30 AM