Excel VBA - How to fit image size on excel WebBrowser control

Anonymous
2024-04-04T14:10:43+00:00

Dear all,

I have a WebBrowser control placed on my Userform which is named WebAppLauch and used to display an image located in my hard drive using this code:

    With Me.WebAppLauch 

        .AddressBar = False 

        .StatusBar = False 

        .Visible = False 

        .Navigate "C:\Users\myname\Desktop\Loading Progress\Launching_Application_Images\Launching_Application.gif" 

    End With

It seems to work but the image is bigger than WebBrowser control so I need to fit the image size to fit in the WebBrowser control size:

How can I fix it?

Thanks

Microsoft 365 and Office | Excel | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments
{count} votes
Answer accepted by question author
  1. Anonymous
    2024-04-04T14:42:51+00:00

    Hi, I am a Microsoft user just like yourself and I will try my best to help you as much as I can today.

    Define HTML Content: Create HTML content that displays the image and sets its size to fit within the dimensions of the WebBrowser control. You'll embed this HTML content into the WebBrowser control.

    Update Your VBA Code: Modify your VBA code to load the HTML content into the WebBrowser control instead of directly navigating to the image file. Here's the updated VBA code:

    With Me.WebAppLauch . AddressBar = False . StatusBar = False . Visible = True ' Set to true to make sure the control is visible . Navigate "about:blank" ' Navigate to a blank page initially Do While . ReadyState <> 4 Or . Busy DoEvents ' Wait until the WebBrowser control has finished loading Loop

    ' Define HTML content with CSS to fit the image within the control's dimensions Dim htmlContent As String htmlContent = "<html><head><style type='text/css'>" & _ "body { margin: 0; padding: 0; }" & _ "img { max-width: 100%; max-height: 100%; display: block; margin: auto; }" & _ "</style></head><body>" & _ "<img src='file:///C:/Users/myname/Desktop/LoadingProgress/Launching_Application_Images/Launching_Application.gif' />" & _ "</body></html>"

    ' Inject HTML content into the WebBrowser control . Document.body.innerHTML = htmlContent End With

    In this code:

    The Navigate "about:blank" line loads a blank page initially to prepare the WebBrowser control. The htmlContent variable contains the HTML and CSS code to display the image and ensure it fits within the control's dimensions. The img CSS style sets the maximum width and height of the image to 100% of the container size, ensuring that the image fits within the WebBrowser control. The src attribute of the img tag points to the file path of your image. Replace "C:/Users/myname/Desktop/LoadingProgress/Launching_Application_Images/Launching_Application.gif" with the actual file path of your image.

    See if that helps! Please let us know if you have any questions. Microsoft Support is available for further assistance if the issue continues.

    Best regards Deeksha

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful