

#Image resize url code#
You can find the code for the IsURLValid function here. MsgBox "The image sizes were successfully retrieved from the given URL!", vbInformation, "Done" Cells(i + 6, 5) = IEPageImgElements(i).Height Cells(i + 6, 4) = IEPageImgElements(i).Width Cells(i + 6, 3) = IEPageImgElements(i).Title Cells(i + 6, 2) = IEPageImgElements(i).src 'The data should be placed at the sixth row and below. 'Loop through all the elements and write the data in the sheet.įor i = 0 To IEPageImgElements.Length - 1 Set IE = CreateObject("InternetExplorer.Application") 'Create a new Internet Explorer instance. 'This indeed is happening but in an asynchronous way. 'The wait is used to make the user believe that the process is still running. 'Disable screen flickering and make the cursor wait. MsgBox "Sorry, the URL you provided is not valid!", vbCritical, "URL Error" Set sht = ThisWorkbook.Sheets("Scraping A Web Page") 'Set the sheet that contains the url of the page to be scrapped. 'images that the page contains (src, title, width, and height). 'Scrapes a given web page (URL) and returns information about the It performs similar functionality to this workbook. The GetImageSizesByScrapingWebPage macro, on the other hand, creates a new instance of Internet Explorer, navigates to the given web page (URL), and returns information about all the images that the page contains (source, title, width, and height). MsgBox "The image sizes were retrieved successfully!", vbInformation, "Done"Įnd Sub Get image sizes by scraping a web page Cells(i, 4) = IEPageImgElements(0).Height Cells(i, 3) = IEPageImgElements(0).Width 'Get the size and write it into the sheet. 'Check if images were found (technically only one image will be found since you using the exact image URL). Set IEPageImgElements = IEPage.getElementsByTagName("img") 'Find all the images using the tag name property. 'Wait until the web page is fully loaded.ĭo Until IE.ReadyState = 4 'READYSTATE_COMPLETE in early binding MsgBox "Sorry, it was impossible to start Internet Explorer!", vbCritical, "Internet Explorer Error" Set sht = ThisWorkbook.Sheets("Known Image URLs") 'Set the sheet that contains the known URLs. 'corresponding images, particularly the width and height of each image. 'Loops through the given image URLs and returns information about the Based on the given image URLs, it creates a new instance of Internet Explorer, navigates to each URL, and returns the width and the height of each image. The GetImageSizesFromKnownURLs macro is actually a loop. The workbook you will find in the Downloads section is ready to use in either of the two ways you prefer.īoth macros using late binding, so no reference to an external library is required (e.g., Microsoft Internet Controls).
#Image resize url how to#
How to scrape a page for all the images and retrieve the relevant information (URL, title, width, and height).How to retrieve the width and height of an image by knowing its URL.The macros below, along with the accompanying workbook, demonstrate: I decided to fulfil Delphine’s request by developing two code snippets that many of you might find useful. Delphine asked me if it is possible to retrieve similar size information from online images (using their URLs). About a year ago, I published a post where I demonstrated several ways of retrieving the image dimensions manually and programmatically using VBA.
