Error displaying MSO image in object from GetImageMSO method of CommandBars with Access 2021

jacky Perpète 41 Reputation points
2022-03-17T09:16:55.207+00:00

Hello,

With access 2021 I am trying to display an MSO image of the CommandBars object either in a button or in an image object on a form.

Here is the VBA code used to display the image in a button enter code here.

Private Sub Commande3_Click()  
  
Me.btnEssai.Picture = Application.CommandBars.GetImageMso("Paste", 16, 16)  
  
End Sub  

When I run this code I get the following error:

runtime error '2220'
Microsoft cannot open the file.

184112-erreur.png

If I assign the MSO image to a variable, I do get a value.

184084-espion.png

I was also able to try with Access 2016 which reports the same error.
Where is the problem?

I performed the same test in Excel 2021 with viewing the MSO image in an Image object, it worked fine.

184103-excel.png

Access Development
Access Development
Access: A family of Microsoft relational database management systems designed for ease of use.Development: The process of researching, productizing, and refining new or existing technologies.
821 questions
0 comments No comments
{count} votes

8 answers

Sort by: Most helpful
  1. jacky Perpète 41 Reputation points
    2022-04-10T06:34:59.527+00:00

    Hello,

    The only solution I could find is to use stdole's SavePicture function to save the MSO image to a folder and then assign the path of the image to the Picture property of the image.

    This solution does not deal with the image transparency problem.
    You would have to use GDI+ functions to transform the image.

    Private Sub Commande3_Click()
    
    Dim ImageMSO As IPictureDisp
    
    Set ImageMSO = CommandBars.GetImageMso("Paste", 32, 32)
    stdole.SavePicture ImageMSO, "D:\Documents\Tempo\Essai.png"
    Me.Image1.Picture = "D:\Documents\Tempo\Essai.png"
    
    0 comments No comments

  2. Oskar Shon 866 Reputation points MVP
    2022-11-11T12:09:12.12+00:00

    You should to see 16/16 image

    259525-image.png

    Try to set this object:

    set Me.btnEssai.Picture = Application.CommandBars.GetImageMso("Paste", 16, 16)  
    

    Regards.

    0 comments No comments

  3. jacky Perpète 41 Reputation points
    2022-11-12T05:26:55.357+00:00

    Hello,

    Thank you for your suggestion.
    This line of code does not work with Access.
    The 'Set' statement triggers the error.
    With Access in VBA you have to write:
    'Me.btnEssai.Picture = Application.CommandBars.GetImageMso("Paste", 16, 16)' to avoid this error.

    259706-error.png

    For this problem, as I pointed out in my comment, I performed the same test in Excel 2021 by viewing the MSO image in an Image object, it works fine.

    Managing the 'Picture' property is certainly different between Access and Excel.
    With Access, the Picture property contains (bitmap) or the path and file name of a bitmap or other type of graphic to be displayed.

    0 comments No comments

  4. Oskar Shon 866 Reputation points MVP
    2022-11-12T08:02:58.743+00:00

    In ACC you can add form in your VBA project.

    259650-image.png
    Then where you want to use it?

    0 comments No comments

  5. jacky Perpète 41 Reputation points
    2022-11-13T06:15:26.573+00:00

    Hello,

    The VBA code is placed in the form.

    259799-formvba.png

    0 comments No comments