VBA & Macro Problems

Load a Picture from a URL into an Excel VBA UserForm

Tauseeq MagsiTauseeq Magsi Nov 25, 2026 870 views

Question details

Display a picture from a URL or worksheet in an Excel VBA UserForm Image control.

Load a Picture from a URL into an Excel VBA UserForm
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A UserForm needs to show a specific picture referenced by a URL or already displayed on a worksheet.
Observed behavior
The Image control requires a picture object, while pictures placed on a worksheet are typically shapes rather than image objects stored directly in cells.
Before you start

Identify the direct URL or local file path for the picture and add an Image control named Image1 to the UserForm. The download example requires Windows Excel, access to the URL, and permission to run trusted macros.

Solution 1Recommended

Download the URL image and load it into the UserForm

Use this Windows VBA approach to download a supported image file before displaying the UserForm.

LoadPicture reads a local file and returns the picture object required by the Image control; it does not load an image directly from a URL. Use a direct image URL and a format supported by LoadPicture, such as BMP or a compatible JPG.

1
Add the download declaration

In Excel, press Alt+F11, choose Insert > Module, and add this Windows API declaration: #If VBA7 Then Public Declare PtrSafe Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As LongPtr, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As LongPtr) As Long #Else Public Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long #End If

2
Add the UserForm code

In the VBA editor, open the UserForm's code window and add the following. Replace the example URL with a direct link to a trusted BMP image: Private Sub UserForm_Initialize() Dim imageUrl As String Dim imagePath As String Dim result As Long imageUrl = "https://example.com/image.bmp" imagePath = Environ$("TEMP") & "\userform_image.bmp" result = URLDownloadToFile(0, imageUrl, imagePath, 0, 0) If result <> 0 Then MsgBox "The image could not be downloaded." Exit Sub End If On Error GoTo LoadError Me.Image1.Picture = LoadPicture(imagePath) Exit Sub LoadError: MsgBox "The downloaded file could not be loaded as a picture." End Sub

3
Test the form

Save the workbook as a macro-enabled .xlsm file, then run the UserForm from the VBA editor. Confirm that Image1 displays the downloaded picture.

Download the URL image and load it into the UserForm
Check the file format: The URL must return an actual image file, not a web page. LoadPicture supports BMP, GIF, ICO, WMF, and many JPG files; PNG and TIFF may require conversion or additional GDI+ code. The example uses a fixed temporary filename and may overwrite a file with that name.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and support for common Microsoft Office formats, making it easy to move everyday documents between office suites. It does not fix Excel-specific VBA UserForm issues; macro support and compatibility can vary.

Open and edit spreadsheets with a familiar interfaceWork with Microsoft Excel formats such as XLSX and XLSInsert pictures into spreadsheets for visual reportsMove everyday Office documents to a free, lightweight suite
microsoft office alternative - wps office

Frequently Asked Questions

Can LoadPicture open an image URL directly?

No. Download the image to a local file first, then assign it with Me.Image1.Picture = LoadPicture(localPath).

How do I show a picture that is already on a worksheet?

If you have the original image file, use its path with LoadPicture in the UserForm code. A worksheet picture is typically a shape, so extracting an embedded picture for use in the control requires a separate method.

Why will my PNG not load in the UserForm?

VBA LoadPicture does not reliably support PNG. Convert the image to a supported format such as BMP, or use additional GDI+ code to handle PNG.

Does the URL download example work on a Mac?

No. The example calls the Windows urlmon library. On another platform, save a supported image locally and use LoadPicture where supported, or use a platform-specific download method.