0

I am trying to load a .gif image in the webbrowser control of a userform. I can import the .gif from a normal path as seen in the Code1, but I want to navigate to embedded .gif which is "object 6". How do I do it? I tried Code2, but could not. Thank you

Code1:

Private Sub UserForm_Initialize()

Me.WebBrowser1.Navigate2 "C:\Pictures\Splash.gif"

End Sub

Code2

Private Sub UserForm_Initialize()

Me.WebBrowser1.Navigate2 OLEObjects("Object 6")

End Sub
Goku
  • 89
  • 7
  • If your image is static you can easily do it with the picture tool but I'm guessing your gif is animated. That makes it a lot more difficult and to be honest I'm not sure it can actually be done with the web browser tool. Please prove me wrong someone cause I would actually like to know if it's possible as well. – Simon May 27 '21 at 09:31
  • @Simon Hi Simon, yes my gif is animated and that is why I cannot use picture tool. Yes, it can be done with the web browser tool but from the normal folder path. What I want to achieve is to load the embedded gif in the workbook. Thanks – Goku May 27 '21 at 09:42
  • I know it can be done by loading a path, I was referring to loading it from within a sheet. – Simon May 27 '21 at 09:55

1 Answers1

2

If your gif is not too large you could try converting it to a data uri:

https://ezgif.com/image-to-datauri

Store the converted image as text in a worksheet cell, then you can do something like this:

Private Sub UserForm_Activate()
    With Me.WebBrowser1
        .Navigate "about:blank"
        While .ReadyState <> 4 Or .Busy: DoEvents: Wend
        .Document.body.innerhtml = Sheet4.Range("A1").Value
    End With
End Sub

Tested and works for me.

Some info on datauri limits: Data protocol URL size limitations

Max you can store in a cell is 32k characters, but you could split a larger string over multiple cells.

Tim Williams
  • 154,628
  • 8
  • 97
  • 125
  • Cheers for the @ (though it didn't work lol). I had a thought to maybe insert a gif into the sheet as a picture (as normal). Then when the form loads it could export that gif to maybe the VBA Environ or something then the webbrowser can load the gif from there. Is it possible to export a moving image out of excel? I know static images can be done through loading it into a chart then exporting but that doesn't work for gifs. – Simon May 28 '21 at 00:15
  • "it didn't work lol" - works fine for me; do you care to try to get it working for you? – Tim Williams May 28 '21 at 00:18
  • Sorry I meant your @ didn't notify me. I managed to get your way working after I realised the code I got was more than 32k characters. What about the rest of my comment though? Is there a way to do that? You're the man of all knowledge lol – Simon May 28 '21 at 00:38
  • I've never tried it that way but the image needs to be a file somewhere accessible to the HTML hosted in the web browser control: outside of a regular file (or hosting it somewhere) I don't know how you'd make that work. – Tim Williams May 28 '21 at 00:41
  • @Tim Williams Works perfect! Thanks have a good time! :) – Goku May 31 '21 at 18:56