Load URL image into UserForm

Lewzerrrr

Active Member
Joined
Jan 18, 2017
Messages
256
Hi,

I have a list of serial #’s that can be embedded into a URL that has an image attached. As of now, I’ve got a worksheet change that when double click on cell that contains serial #, it’ll scrap the image into a new workbook for my users to view what it is.

I’d like to steer away from that and have the image pull onto a userform, is this possible?

I tried to use an image control but found out this only works if it’s downloaded..

Web Browser control is a no go aswell as it pulls the whole web browser with scroll bars, I’d like it quite fixed to a medium size square.

Thanks,
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
See if this gets you started. Userform with an Image control (named Image1). All code in the Userform module.

Code:
Option Explicit

[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=If]#If[/URL]  VBA7 Then
Private 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
Private Declare PtrSafe Function DeleteUrlCacheEntry Lib "Wininet.dll" Alias "DeleteUrlCacheEntryA" _
    (ByVal lpszUrlName As String) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=Else]#Else[/URL] 
Private 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
Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" Alias "DeleteUrlCacheEntryA" _
    (ByVal lpszUrlName As String) As Long
[URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=End]#End[/URL]  If

Private Const BINDF_GETNEWESTVERSION As Long = &H10


Private Sub UserForm_Initialize()

    Dim imageURL As String
    Dim fileName As String
        
    imageURL = "https://www.mrexcel.com/forum/images/misc/mrexcel_logo.gif"
    fileName = Environ("temp") & "" & Mid(imageURL, InStrRev(imageURL, "/") + 1)
    
    If DownloadFile(imageURL, fileName) Then
        Image1.Picture = LoadPicture(fileName)
    Else
        MsgBox "Error downloading " & imageURL
    End If
    
End Sub


Private Function DownloadFile(URL As String, LocalFileName As String) As Boolean
    
    Dim RetVal As Long
    
    DeleteUrlCacheEntry URL
    RetVal = URLDownloadToFile(0, URL, LocalFileName, BINDF_GETNEWESTVERSION, 0)
    DownloadFile = (RetVal = 0)

End Function
 
Upvote 0
Thanks John, I’ll try it out when I get on my computer,

If fails, what do you think would be a fancy way to display the image?

I don’t want it to direct to another sheet or workbook nor is there space to appear on the active sheet.
 
Upvote 0
Another option would be IE.

A back slash is missing in the fileName = line. It should be:

Code:
    fileName = Environ("temp") & "\" & Mid(imageURL, InStrRev(imageURL, "/") + 1)
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,242
Members
452,623
Latest member
russelllowellpercy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top