How To Speed-up Internet Explorer Using VBA & VBScript
Web scraping is the software technique of extracting information from websites and converting unstructured data into a structured format. For example, a VBA procedure would simulate human exploration of the World Wide Web by automatically requesting web pages while controlling Internet Explorer. This process can be time consuming, if multiple requests are made.
Web Scraping is a growing concept in today's networked world, especially for automating data harvesting from websites, which do not offer any API protocol.
Internet Explorer provides users with the option to disable the loading of pictures from a webpage. Disabling this feature allows your browser to move much faster because images, which can take a while to display due to their significant file size, are not loaded or shown. If you plan to extract data only, you may be interested just in the text information of a webpage.
Here is how Show pictures in Internet Explorer can be disabled, if a webpage must be loaded quickly several times or a slow network/internet connection is being used.
Web Scraping is a growing concept in today's networked world, especially for automating data harvesting from websites, which do not offer any API protocol.
Internet Explorer provides users with the option to disable the loading of pictures from a webpage. Disabling this feature allows your browser to move much faster because images, which can take a while to display due to their significant file size, are not loaded or shown. If you plan to extract data only, you may be interested just in the text information of a webpage.
Here is how Show pictures in Internet Explorer can be disabled, if a webpage must be loaded quickly several times or a slow network/internet connection is being used.
- Click the Tools menu and select Internet Options from the menu list.
- Navigate to Advanced tab and scroll down to Multimedia section.
- Unmark the Show pictures checkbox.
- Click OK. The setting takes effect immediately upon refresh.
Please read about our ZuluTrade Forex ETL (Extract, Transform and Load) demo
The Show pictures setting can be toggled by using the VBA code below to change a registry key. It has been tested with Excel 2010 (in both 32/64 bit versions) & IE9 (32/64 bit) under Windows 7.
Code comments and recommended readings:
The registry is used by applications (e.g. Internet Explorer) and Windows to store configuration data.
Although VBA includes the SaveSetting and GetSetting functions to save and retrieve information from the registry, these functions only operate on a specific section of the registry, the Visual Basic and VBA Program Settings of the HKEY_CURRENT_USER root key.
The code below demonstrates how to use 32/64 bit Windows API functions to set values anywhere in the registry e.g. as required to toggle the 'Show pictures' option of Internet Explorer.
The Win64 conditional compilation constant is used to test whether code is running as 32-bit or as 64-bit.
In versions of VBA prior to Excel 2010, there was no specific pointer data type so the Long data type was used. And because the Long data type is always 32-bits, it breaks when used on a system with 64-bit memory, because the upper 32-bits may be truncated or may overwrite other memory addresses.
Either of these situations can result in unpredictable behavior or system crashes. To resolve this, VBA now contains a true pointer data type: LongPtr. This new data type enables developers to write valid Declare statements for execution with the 64-bit version of Office 2010.
For more details read: Compatibility Between the 32-bit and 64-bit Versions of Office 2010
The registry is used by applications (e.g. Internet Explorer) and Windows to store configuration data.
Although VBA includes the SaveSetting and GetSetting functions to save and retrieve information from the registry, these functions only operate on a specific section of the registry, the Visual Basic and VBA Program Settings of the HKEY_CURRENT_USER root key.
The code below demonstrates how to use 32/64 bit Windows API functions to set values anywhere in the registry e.g. as required to toggle the 'Show pictures' option of Internet Explorer.
- How To Use the Registry API to Save and Retrieve Settings
The Win64 conditional compilation constant is used to test whether code is running as 32-bit or as 64-bit.
In versions of VBA prior to Excel 2010, there was no specific pointer data type so the Long data type was used. And because the Long data type is always 32-bits, it breaks when used on a system with 64-bit memory, because the upper 32-bits may be truncated or may overwrite other memory addresses.
Either of these situations can result in unpredictable behavior or system crashes. To resolve this, VBA now contains a true pointer data type: LongPtr. This new data type enables developers to write valid Declare statements for execution with the 64-bit version of Office 2010.
For more details read: Compatibility Between the 32-bit and 64-bit Versions of Office 2010
Option Explicit 'http://www.spreadsheet1.com/web-scraping.html #If VBA7 Then Private Declare PtrSafe Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As LongPtr, ByVal lpSubKey As String, phkResult As LongPtr) As Long Private Declare PtrSafe Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As LongPtr, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long Private Declare PtrSafe Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As LongPtr) As Long #Else Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long #End If Private Const REG_SZ = 1 Private Const HKEY_CURRENT_USER = &H80000001 Private Sub SaveString(hKey As Long, _ strpath As String, _ strValue As String, _ strdata As String) #If Win64 Then Dim keyhand As LongLong #Else Dim keyhand As Long #End If Dim x As Long x = RegCreateKey(hKey, strpath, keyhand) x = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata)) x = RegCloseKey(keyhand) End Sub Sub ShowPicturesInIE() Call SaveString(HKEY_CURRENT_USER, _ "Software\Microsoft\Internet Explorer\Main", _ "Display Inline Images", _ "yes") End Sub Sub NoPicturesInIE() Call SaveString(HKEY_CURRENT_USER, _ "Software\Microsoft\Internet Explorer\Main", _ "Display Inline Images", _ "no") End Sub
VBScript
The Show pictures can also be toggled by running the VBScript code below:
- Save each code snippet in a file with .vbs extension and name both files accordingly.
- Open Windows Explorer and navigate to the folder containing the script files.
- Click NoPicturesInIE.vbs to stop pictures from loading or ShowPicturesInIE.vbs to restore default IE behaviour.
- Internet Explorer must be closed (if already open) and reopened, before the 'Show Pictures' setting takes effect.
'Show pictures in Internet Explorer strKey = "HKCU\Software\Microsoft\Internet Explorer\Main\Display Inline Images" CreateObject("WScript.Shell").RegWrite strKey, "yes", "REG_SZ" 'No pictures in Internet Explorer strKey = "HKCU\Software\Microsoft\Internet Explorer\Main\Display Inline Images" CreateObject("WScript.Shell").RegWrite strKey, "no", "REG_SZ"
Speed-up Web Scraping Even Further!
Active Scripts are programs written in JavaScript, or sometimes Microsoft's VBScript and ActiveX, that enable Web sites to add specific functionality e.g. display interactive charts. Active scripting is sometimes disabled for security purposes e.g. to avoid exploitation of vulnerabilities, until a specific patch is issued.
Irrespective of security issues, if Active Scripts are disabled for performance alone, Internet Explorer 9 will load pages containing scripts even faster, as no code has to be executed. Several hours can be saved by using this tip, if thousands of web pages have to be scraped. Please note:
Irrespective of security issues, if Active Scripts are disabled for performance alone, Internet Explorer 9 will load pages containing scripts even faster, as no code has to be executed. Several hours can be saved by using this tip, if thousands of web pages have to be scraped. Please note:
- The Show pictures setting is completely independent from Active Scripts. Maximum time savings may be achieved by using both tips.
- Active Scripts may have to be executed in order to load more content into the browser, so study the page source code carefully and experiment before toggling this setting.
How To Kill All IExplore.exe Processes At Once
Normally you should keep track of Internet Explorer objects in your code and terminate them with the .Quit method. However, if you end-up with dozens of IE processes, visible or hidden, the snippet below can close all instances of Internet Explorer from VBA.
Shell ("C:\Windows\system32\cmd.exe /c wmic process where name='iexplore.exe' call terminate")