Fabiano Augusto
Fabiano Augusto

Reputation: 71

How to display PNG images with transparent background in an Excel VBA UserForm

I am using VBA in Excel to make an UserForm. I would like to display in this UserForm an image with transparent background that is in PNG format. Initially I was getting an error message because VBA does not support PNG format natively, so I found out that if I reference Microsoft Windows Image Acquisition Library v2.0 and use the following code, VBA is able to display the PNG image in an ImageControl.

Sub ShowForm()
    UserForm1.Image1.Picture = loadImg("C:\Temp\Logo.png")
    UserForm1.Show
End Sub

Function loadImg(fileLocation As String) As IPictureDisp
Dim imgctrl As New WIA.ImageFile         'can handle more extensions than built in LoadPicture function
With imgctrl
    .LoadFile fileLocation
    Set loadImg = .fileData.Picture
End With
Set imgctrl = Nothing
End Function

The problem I am experiencing now is that the image is showed but the background is not transparent. Does somebody could help me to solve this issue? An additional doubt I have is if would be possible embed image in Excel instead of call it from a path, as was done in loadImg function.

Upvotes: 1

Views: 11197

Answers (2)

Luiz Vaughan
Luiz Vaughan

Reputation: 675

Complementing @fernando F, once in Photoshop: Save as image.gif > In "Indexed Colours" select Palette "Web" > mark "Transparency" > OK. Once in VBA Userform, open your Image1 properties, select BackStyle and set to "0-FrmBackStyleTransparent"

Upvotes: 0

Fernando F
Fernando F

Reputation: 51

I know it's been a while, but here we go: Save your image (in Photoshop, for example) in ".gif". Then, in Excel / userform, insert image. In the window, choose "all files" and "image.gif" will appear. It only loses its quality a little compared to ".png"

Upvotes: 2

Related Questions