Artik
Active Member
- Joined
- Jun 5, 2012
- Messages
- 300
Hello exceloholics.
I need a solution using the WebBrowser control on Userform (Windows 11, MSO 365). As with loading the control with images I didn't have major problems, but with the response to clicking on images I fell. I tried all day to get Copilot to write the missing code. But it was like kicking myself with a horse, I had to lose.
I need functionality so that when clicking on images loaded into a WebBrowser control, the selected image displays in the other control. Unfortunately, HTML, JavaScript is not my garden so I can't think of much here. Here is the code that loads the images into the WebBrowser.
In the attachment I also show a solution using Frame and dynamically adding Image controls. I would like to achieve the same functionality in the second UserForm.
Why can't I use the solution with Frame? Because there are more than 4K possible images to display (at most 20 in one session), which have different formats that are not supported by the Image control (including PNG).
And also an overview image
Artik
I need a solution using the WebBrowser control on Userform (Windows 11, MSO 365). As with loading the control with images I didn't have major problems, but with the response to clicking on images I fell. I tried all day to get Copilot to write the missing code. But it was like kicking myself with a horse, I had to lose.
I need functionality so that when clicking on images loaded into a WebBrowser control, the selected image displays in the other control. Unfortunately, HTML, JavaScript is not my garden so I can't think of much here. Here is the code that loads the images into the WebBrowser.
VBA Code:
Private Sub UserForm_Initialize()
Call LoadClickableImagesInWebBrowser
End Sub
Sub LoadClickableImagesInWebBrowser()
Dim folderPath As String
Dim imgSpacing As String
Dim htmlContent As String
Dim fileName As String
Dim filePath As String
Dim imgTag As String
Dim FSO As Object
Dim folder As Object
Dim file As Object
Dim i As Long
folderPath = ThisWorkbook.Path & "\"
imgSpacing = "6px"
htmlContent = "<html><body style='margin:0;padding:0;'>"
Set FSO = CreateObject("Scripting.FileSystemObject")
Set folder = FSO.GetFolder(folderPath)
For Each file In folder.Files
fileName = file.Name
If LCase(fileName) <> LCase("NoPicture.jpg") And LCase(fileName) <> LCase(ThisWorkbook.Name) And (fileName Like "[!~]*") Then
i = i + 1
filePath = folderPath & fileName
imgTag = "<img id='img" & i & "' src='" & filePath & "' width='70' height='70' style='display:block; margin-bottom:6px;'>"
htmlContent = htmlContent & imgTag
End If
Next file
htmlContent = htmlContent & "</body></html>"
On Error GoTo Err_Hndl
Me.WebBrowser2.Navigate "about:blank"
Do While Me.WebBrowser2.ReadyState <> 4
DoEvents
Loop
Me.WebBrowser2.Document.Open
Me.WebBrowser2.Document.Write htmlContent
Me.WebBrowser2.Document.Close
Me.WebBrowser2.Document.body.Style.overflowY = "auto"
Exit Sub
Err_Hndl:
Debug.Print Err.Number & " ; " & Err.Description
End Sub
Why can't I use the solution with Frame? Because there are more than 4K possible images to display (at most 20 in one session), which have different formats that are not supported by the Image control (including PNG).
And also an overview image
Artik