+ Reply to Thread
Results 1 to 20 of 20

Insert PNG image into userform

  1. #1
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Insert PNG image into userform

    Hi. I have searched the internet for this solution but keep coming up blank.

    I have two issues:

    1) Is it possible through VBA to allow png files to be inserted as images into a userform. I found some code on another site but didn't really understand how it worked. Not sure allowed to post link so I wont.
    If it is possible how is it done?

    2) I have 2 listboxes in my userform. When I move off the second listbox I would like an image to appear in the userform based on the names selected in the two listboxes. For example if the first listbox selection is "clematis" and the second listbox is "The President" I would like it to call up clematisthepresident.png - if this is possible does this kind of calling up files slow down the workbook dramatically as the image call up is not essential it is a bonus for my boss.

    Hope I have explained this ok

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,120

    Re: Insert PNG image into userform

    Yes it's possible. requires some API functions.

    This example userform will allow you to select a image file and then display it.
    Attached Files Attached Files
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Insert PNG image into userform

    Quote Originally Posted by Andy Pope View Post
    Yes it's possible. requires some API functions.

    This example userform will allow you to select a image file and then display it.
    Hi Andy. Thanks for response.

    I opened spreadsheet attached but it was just blank. Was it the right file?

    Cheers

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,120

    Re: Insert PNG image into userform

    you need to open the VBE (ALT+F11) and then run the userform (F5)

  5. #5
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Insert PNG image into userform

    Quote Originally Posted by Andy Pope View Post
    you need to open the VBE (ALT+F11) and then run the userform (F5)
    Still doesn't seem to do anything or find a userform.
    Any chance you could drop a command button in the spreadsheet and link it to the userform. Cant see any code at all.

    Sorry if being stupid.

  6. #6
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Insert PNG image into userform

    OK I have found the userform now. Will have a look and report back if figure it out

  7. #7
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,120

    Re: Insert PNG image into userform

    latest file has image selection and launch button on worksheet
    Attached Files Attached Files

  8. #8
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Insert PNG image into userform

    CHeers for this.
    I have inserted the second module from your sheet into mine and now is all I need to do is set the image to load in my userform? For now lets say I want the image to appear when the userform is initialised.
    and the filename is static.

    What would I type for that and are there any other steps I need to do.

  9. #9
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,120

    Re: Insert PNG image into userform

    Please Login or Register  to view this content.
    double click the user form at design time and add code

  10. #10
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Insert PNG image into userform

    Quote Originally Posted by Andy Pope View Post
    Please Login or Register  to view this content.
    double click the user form at design time and add code
    Excellent that now works and I can get png files into the userform.

    Any chance you could help on the second question.

  11. #11
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,120

    Re: Insert PNG image into userform

    that would be something like

    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    12-14-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    106

    Re: Insert PNG image into userform

    Thank you so much for your assistance. With this. Solved all my problems and saved me a lot of time and frustration.

  13. #13
    Registered User
    Join Date
    02-15-2017
    Location
    Ruckersville, VA
    MS-Off Ver
    Excel 2013
    Posts
    2

    Re: Insert PNG image into userform

    Hi. This looks promising. I too would like to have my cyber PNG handcuffs removed! GIFs have a very low resolution compared to PNGs which to my knowledge are the only other standard format the offers transparency. In fact, I've been "faking" transparency for a while by using an eyedropper to match the background image. If you think I can use PNGs with transparency on a Userform in Excel 2016 VBA, I would like to request that you consider updating your Function to support 64-bit machines.

    I am getting a compile error which says "The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark them with the PtrSafe attribute."

    Normally, I would attempt this on my own, but I figure there might be other 64-bit users out there who would benefit as well.

    Thanks!

  14. #14
    Registered User
    Join Date
    07-15-2019
    Location
    Croatia
    MS-Off Ver
    2010
    Posts
    1
    Quote Originally Posted by Andy Pope View Post
    latest file has image selection and launch button on worksheet
    Hi!
    But when you load it transparent become black.
    How to fix that?
    Regards

  15. #15
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,120

    Re: Insert PNG image into userform

    I don't believe that transparency is supported by the LoadPicture. The functions turn file image into BMP format.

  16. #16
    Registered User
    Join Date
    09-26-2019
    Location
    London, England
    MS-Off Ver
    Office 19
    Posts
    2

    Re: Insert PNG image into userform

    Hi,

    I came across this thread because I wanted to add my logo to UserForms. Below are the image file formats that can currently be used.
    You can set the image property to transparent in the "appearance" options as shown below.

    The resolution appears to be driven more by the image size, as it looks looks good if I make the image larger (unfortunately I only want it to appear as a smallish icon)

    I also tried the eye-dropper workaround to give the image the same background as the userform, with no visible improvement in the results.

    KB Snip uf image files.PNG
    KB Snip uf image options.PNG

  17. #17
    Registered User
    Join Date
    12-10-2019
    Location
    portugal
    MS-Off Ver
    365
    Posts
    5

    Re: Insert PNG image into userform

    Hi,

    I am trying to use this code, but with windows 10 I have the error: "compile error: type mismatch"

    I already looked for help and made the following change:
    Private Declare PtrSafe Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal filename As Long, bitmap As Long) As Long
    Private Declare PtrSafe Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal filename As LongPTR, bitmap As Long) As Long

    But it still doesn't work, the image doesn't appear


    can someone help?

    thanks

  18. #18
    Registered User
    Join Date
    12-10-2019
    Location
    portugal
    MS-Off Ver
    365
    Posts
    5

    Re: Insert PNG image into userform

    Hi,

    I am trying to use this code, but with windows 10 I have the error: "compile error: type mismatch"

    I already looked for help and made the following change:
    Private Declare PtrSafe Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal filename As Long, bitmap As Long) As Long
    Private Declare PtrSafe Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal filename As LongPTR, bitmap As Long) As Long

    But it still doesn't work, the image doesn't appear


    can someone help?

    thanks

  19. #19
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 2000/3/7/10/13/16/365
    Posts
    52,018

    Re: Insert PNG image into userform

    Quote Originally Posted by joaomarques View Post
    Hi,

    I am trying to use this code, but with windows 10 I have the error: "compile error: type mismatch"

    I already looked for help and made the following change:
    Private Declare PtrSafe Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal filename As Long, bitmap As Long) As Long
    Private Declare PtrSafe Function GdipCreateBitmapFromFile Lib "GDIPlus" (ByVal filename As LongPTR, bitmap As Long) As Long

    But it still doesn't work, the image doesn't appear


    can someone help?

    thanks
    Administrative Note:

    Welcome to the forum.

    We are happy to help, however whilst you feel your request is similar to this thread, experience has shown that things soon get confusing when answers refer to particular cells/ranges/sheets which are unique to your post and not relevant to the original.

    Please see Forum Rule #4 about hijacking and start a new thread for your query.

    If you are not familiar with how to start a new thread see the FAQ: How to start a new thread
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  20. #20
    Registered User
    Join Date
    02-10-2021
    Location
    Adelaide, Australia
    MS-Off Ver
    Office 365
    Posts
    1

    Re: Insert PNG image into userform

    Thanks for this! Worked first time.
    I had 500+ thumbnails to convert, and it was a maintenance issue, as new ones were being added all the time. Now..all fully automated.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Insert jpg image from file path to image frame
    By SAsplin in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-27-2012, 10:12 AM
  2. Replies: 1
    Last Post: 09-05-2012, 04:11 PM
  3. Image insert with VB
    By Jacky_PT in forum Excel General
    Replies: 1
    Last Post: 12-10-2008, 11:00 PM
  4. best image on a userform
    By obsessive in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-04-2006, 05:30 PM
  5. [SOLVED] Userform Image
    By Jennifer in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 02-24-2006, 03:50 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1