VBA & Macro Problems

Select JPG and PNG Files for an Excel VBA ActiveX Image Control

Elise WilliamsElise Williams Dec 2, 2026 868 views

Question details

Open a specific folder with a VBA file picker, filter for image files, and load a selected image into an ActiveX Image control.

Select JPG and PNG Files for an Excel VBA ActiveX Image Control
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A user wants to select an image from a known folder using a button on an Excel VBA UserForm.
Observed behavior
The file picker should list JPG, JPEG, PNG, and GIF files, and a supported selection should appear in the Image control.
Before you start

Use Excel for Windows with a macro-enabled workbook and a UserForm containing a CommandButton named CommandButton1 and an Image control named Image1. Replace the example folder path with an existing folder; ActiveX controls are not supported in Excel for Mac.

Solution 1Recommended

Use a VBA file picker to load an image into a UserForm

Use this method to open a specific folder, show common image formats, and load a supported file into an ActiveX Image control.

The filter determines which files appear in the picker; it does not guarantee that VBA's LoadPicture function can decode every listed format. PNG support is not reliable, so convert a PNG to JPG or BMP if loading fails.

1
Open the UserForm code

In Excel, press Alt+F11, open your UserForm in the VBA editor, and double-click CommandButton1 to open its Click event.

2
Add the picker code

Replace the Click event with the following code and change C:\YourFolder\ to your folder path: Private Sub CommandButton1_Click() On Error GoTo ImageLoadError With Application.FileDialog(msoFileDialogFilePicker) .AllowMultiSelect = False .Filters.Clear .Filters.Add "Picture files", "*.jpg;*.jpeg;*.png;*.gif" .InitialFileName = "C:\YourFolder\" If .Show = -1 Then Me.Image1.Picture = LoadPicture(.SelectedItems(1)) End If End With Exit Sub ImageLoadError: MsgBox "The selected image could not be loaded. Try converting it to JPG or BMP.", vbExclamation End Sub

3
Run and test the form

Run the UserForm from the VBA editor, click CommandButton1, and select an image. Canceling the dialog leaves the current picture unchanged.

Use a VBA file picker to load an image into a UserForm
PNG loading limitation: A PNG can appear in the file picker but still fail to load. Converting it to JPG is a practical workaround; add *.bmp to the filter if you want to select BMP files.
Free Microsoft Office alternative

Work with spreadsheet images in WPS Office

WPS Office is a free, lightweight alternative for everyday spreadsheet work. Its familiar interface and Microsoft Office format compatibility make it easy to move common files and insert images, although it does not fix Excel ActiveX or VBA compatibility issues.

Insert images directly into spreadsheets without an ActiveX control.Open and edit Microsoft Excel XLSX files for common spreadsheet tasks.Use a familiar interface for a smoother transition from Microsoft Office.Work with a free, lightweight office suite.
microsoft office alternative - wps office

Frequently Asked Questions

Why does a PNG appear in the picker but fail to load?

The FileDialog filter only controls which filenames are shown. VBA LoadPicture does not reliably support PNG files, so convert the image to JPG or BMP and select the converted file.

How do I change the folder that opens first?

Change the path in .InitialFileName to an existing folder, such as C:\Pictures\. Include the trailing backslash so the dialog opens the folder rather than treating the last part as a filename.

What happens if I cancel the file picker?

The code checks whether .Show returns -1 before loading a file. If you click Cancel, no new picture is loaded and the current Image1 picture remains unchanged.

Can I use this code for an ActiveX Image control on a worksheet?

The example uses Me.Image1 on a UserForm. For worksheet controls, put the button event in the worksheet's code module and refer to the image through Me.OLEObjects("Image1").Object.Picture instead.