In addition to the techniques described in Rest to Excel library, it is possible to use an internet explorer object to access the DOM directly from VBA.

This condensed article is from an original by a guest contributor,  Petros Chatzipantazis of spreadsheet1.com.

Background

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. VBA does not have any way to set these options of an internet explorer object directly. This is because they are actually controlled from a registry entry. Here is how to toggle this setting, by tweaking the registry contents.

Code comments and recommended reading

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.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Option Explicit
 
#If Win64 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

Thanks again to Petros for his contribution. Why not join our forum, follow the blog or follow me on Twitter to ensure you get updates when they are available.