Home  |  About  | Last |  Submit  |  Contact
AllQuests.com



Previous Question:  Preload image question  JavaScriptNext Question:  White line between tables cant get rid of  HTML
Question Clear IE Browser Cache and History with VBA ( VBForums Office Development )
Updated: 2008-12-15 19:40:27 (10)
Clear IE Browser Cache and History with VBA

Hi

Is there a way to clear out the cache and history using VBA code? (not using sendkeys). i know how to clear the cookies folder, but cache and history are an unknown. any help would be appreciated.

thanks

tx

Answers: Clear IE Browser Cache and History with VBA ( VBForums Office Development )
Clear IE Browser Cache and History with VBA

Keep it simple

Sub Clear_Temp_Files()
Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 8 "
End Sub


Sub Clear_Cookies()
Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 2"
End Sub



Sub Clear_History()
Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 1"
End Sub



Sub Clear_Form_Data()
Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 16"
End Sub



Sub Clear_Saved_Passwords()
Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 32"
End Sub



Sub Clear_All()
Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 255"
End Sub



Sub Clear_Clear_Add_ons_Settings()
Shell "RunDll32.exe InetCpl.cpl,ClearMyTracksByProcess 4351"
End Sub

technorobbo

Clear IE Browser Cache and History with VBA

Hi All,

It was working in XP and Ms word 2002 Environment.

When I migrated my application to Windows 2003 and Ms word 2003. There is a exception given as err.LastDllError=87.

Please help me asap.

I managing with the last result in XP environment. I need to migrate to windows 2003.

Solution applaudable.
With regards,
AshokKumar.

MyAshok

Clear IE Browser Cache and History with VBA

Hi,

I am newbie to VBA.

Thank you very much for ur code.

It saved my life at the dead end.

Its working fine in my environment Window XP and MsWord 2003

Thanks a lot.

With Regards,
Ashok,



Quote:
Originally Posted by koolsid
Hi

here is something directly from the microsoft's website...

1) CLEARING CACHE

Use WinInet API FindFirstURLCacheEntry to find the first cache entry and use FindNextUrlCacheEntry to enumerate through the cache. Use DeleteUrlCacheEntry to delete each entry.

Note that FindFirstUrlCacheGroup, FindNextUrlCacheGroup, and DeleteUrlCacheGroup are used in the following example. These API functions have only became available with Internet Explorer 5, so proper checking as shown is needed to prevent errors.

The following steps show you how to use the WinInet API in Visual Basic to clear all the files in the cache.

1. Create a new Visual Basic standard EXE project.
2. Create a command button under form1.
3. Paste the following code in the module of form1:

VB Code:
  1. Option Explicit
  2.  
  3. Private Declare Function FindFirstUrlCacheGroup Lib "wininet.dll" ( _
  4.     ByVal dwFlags As Long, _
  5.     ByVal dwFilter As Long, _
  6.     ByRef lpSearchCondition As Long, _
  7.     ByVal dwSearchCondition As Long, _
  8.     ByRef lpGroupId As Date, _
  9.     ByRef lpReserved As Long) As Long
  10.  
  11. Private Declare Function FindNextUrlCacheGroup Lib "wininet.dll" ( _
  12.     ByVal hFind As Long, _
  13.     ByRef lpGroupId As Date, _
  14.     ByRef lpReserved As Long) As Long
  15.    
  16. Private Declare Function DeleteUrlCacheGroup Lib "wininet.dll" ( _
  17.     ByVal sGroupID As Date, _
  18.     ByVal dwFlags As Long, _
  19.     ByRef lpReserved As Long) As Long
  20.    
  21. Private Declare Function FindFirstUrlCacheEntry Lib "wininet.dll" Alias "FindFirstUrlCacheEntryA" ( _
  22.     ByVal lpszUrlSearchPattern As String, _
  23.     ByRef lpFirstCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _
  24.     ByRef lpdwFirstCacheEntryInfoBufferSize As Long) As Long
  25.    
  26. Private Type INTERNET_CACHE_ENTRY_INFO
  27.     dwStructSize As Long
  28.     szRestOfData(1024) As Long
  29. End Type
  30.  
  31. Private Declare Function DeleteUrlCacheEntry Lib "wininet.dll" Alias "DeleteUrlCacheEntryA" ( _
  32.     ByVal lpszUrlName As Long) As Long
  33.  
  34. Private Declare Function FindNextUrlCacheEntry Lib "wininet.dll" Alias "FindNextUrlCacheEntryA" ( _
  35.     ByVal hEnumHandle As Long, _
  36.     ByRef lpNextCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _
  37.     ByRef lpdwNextCacheEntryInfoBufferSize As Long) As Long
  38.  
  39. Private Const CACHGROUP_SEARCH_ALL = &H0
  40. Private Const ERROR_NO_MORE_FILES = 18
  41. Private Const ERROR_NO_MORE_ITEMS = 259
  42. Private Const CACHEGROUP_FLAG_FLUSHURL_ONDELETE = &H2
  43. Private Const BUFFERSIZE = 2048
  44.  
  45. Private Sub Command1_Click()
  46.     Dim sGroupID As Date
  47.     Dim hGroup As Long
  48.     Dim hFile As Long
  49.     Dim sEntryInfo As INTERNET_CACHE_ENTRY_INFO
  50.     Dim iSize As Long
  51.        
  52.     On Error Resume Next
  53.    
  54.     ' Delete the groups
  55.     hGroup = FindFirstUrlCacheGroup(0, 0, 0, 0, sGroupID, 0)
  56.    
  57.     ' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented
  58.     If Err.Number <> 453 Then
  59.         If (hGroup = 0) And (Err.LastDllError <> 2) Then
  60.             MsgBox "An error occurred enumerating the cache groups" & Err.LastDllError
  61.             Exit Sub
  62.         End If
  63.     Else
  64.         Err.Clear
  65.     End If
  66.    
  67.     If (hGroup <> 0) Then
  68.         'we succeeded in finding the first cache group.. enumerate and
  69.         'delete
  70.         Do
  71.             If (0 = DeleteUrlCacheGroup(sGroupID, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, 0)) Then
  72.                
  73.                ' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented
  74.                If Err.Number <> 453 Then
  75.                  MsgBox "Error deleting cache group " & Err.LastDllError
  76.                  Exit Sub
  77.                Else
  78.                   Err.Clear
  79.                End If
  80.             End If
  81.             iSize = BUFFERSIZE
  82.             If (0 = FindNextUrlCacheGroup(hGroup, sGroupID, iSize)) And (Err.LastDllError <> 2) Then
  83.                 MsgBox "Error finding next url cache group! - " & Err.LastDllError
  84.             End If
  85.         Loop Until Err.LastDllError = 2
  86.     End If
  87.  
  88.   ' Delete the files
  89.     sEntryInfo.dwStructSize = 80
  90.     iSize = BUFFERSIZE
  91.     hFile = FindFirstUrlCacheEntry(0, sEntryInfo, iSize)
  92.     If (hFile = 0) Then
  93.         If (Err.LastDllError = ERROR_NO_MORE_ITEMS) Then
  94.             GoTo done
  95.         End If
  96.         MsgBox "ERROR: FindFirstUrlCacheEntry - " & Err.LastDllError
  97.         Exit Sub
  98.     End If
  99.     Do
  100.         If (0 = DeleteUrlCacheEntry(sEntryInfo.szRestOfData(0))) _
  101.             And (Err.LastDllError <> 2) Then
  102.             Err.Clear
  103.         End If
  104.         iSize = BUFFERSIZE
  105.         If (0 = FindNextUrlCacheEntry(hFile, sEntryInfo, iSize)) And (Err.LastDllError <> ERROR_NO_MORE_ITEMS) Then
  106.             MsgBox "Error:  Unable to find the next cache entry - " & Err.LastDllError
  107.             Exit Sub
  108.         End If
  109.     Loop Until Err.LastDllError = ERROR_NO_MORE_ITEMS
  110. done:
  111.     MsgBox "cache cleared"
  112.     Command1.Enabled = True
  113. End Sub


Hope this helps....

Let me see if i can find something for the 'History'

MyAshok

Clear IE Browser Cache and History with VBA

thanks for the code. it clears the address bar it seems, but not the history.

thanks for the input

tx12345

Clear IE Browser Cache and History with VBA

I am trying to do something similar in excel but am not sure how I can create a new EXE project? Can I run the same code from a module?
Also will this work for IE7?

Any inputs will be very helpful as I just started coding in VB-Excel. thank you

samq12345

Clear IE Browser Cache and History with VBA

I think I found something...

I am not sure. i haven't tested it yet...

VB Code:
  1. ' Clear the IE History
  2.  
  3. Public Sub ClearIEHistory()
  4.     Const HKEY_CURRENT_USER = &H80000001
  5.     Dim sKey As String
  6.    
  7.     sKey = "Software\Microsoft\Internet Explorer\TypedURLs"
  8.     ' delete the key that contains the URLs the history
  9.     DeleteRegistryKey HKEY_CURRENT_USER, sKey
  10.     ' recreate the key, empty
  11.     CreateRegistryKey HKEY_CURRENT_USER, sKey
  12. End Sub


you will require the DeleteRegistryKey and CreateRegistryKey routines which are mentioned below:


DELETE REGISTRY KEY

VB Code:
  1. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
  2.     (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  3.  
  4. ' Delete a registry key
  5. '
  6. ' Under Windows NT it doesn't work if the key contains subkeys
  7.  
  8. Sub DeleteRegistryKey(ByVal hKey As Long, ByVal KeyName As String)
  9.     RegDeleteKey hKey, KeyName
  10. End Sub


CREATE REGISTRY KEY

VB Code:
  1. Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias _
  2.     "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, _
  3.     ByVal Reserved As Long, ByVal lpClass As Long, ByVal dwOptions As Long, _
  4.     ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, _
  5.     phkResult As Long, lpdwDisposition As Long) As Long
  6. Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As _
  7.     Long
  8.  
  9. Const KEY_READ = &H20019  ' ((READ_CONTROL Or KEY_QUERY_VALUE Or
  10.                           ' KEY_ENUMERATE_SUB_KEYS Or KEY_NOTIFY) And (Not
  11.                           ' SYNCHRONIZE))
  12. Const REG_OPENED_EXISTING_KEY = &H2
  13.  
  14. ' Create a registry key, then close it
  15. ' Returns True if the key already existed, False if it was created
  16.  
  17. Function CreateRegistryKey(ByVal hKey As Long, ByVal KeyName As String) As _
  18.     Boolean
  19.     Dim handle As Long, disposition As Long
  20.    
  21.     If RegCreateKeyEx(hKey, KeyName, 0, 0, 0, 0, 0, handle, disposition) Then
  22.         Err.Raise 1001, , "Unable to create the registry key"
  23.     Else
  24.         ' Return True if the key already existed.
  25.         CreateRegistryKey = (disposition = REG_OPENED_EXISTING_KEY)
  26.         ' Close the key.
  27.         RegCloseKey handle
  28.     End If
  29. End Function


I don't know if this is what you want... Hope it helps

koolsid

Clear IE Browser Cache and History with VBA

Yes, but from which office app and what exactly are you trying to do?

moved

RobDog888

Clear IE Browser Cache and History with VBA

Quote:
Originally Posted by RobDog888
Yes, but from which office app and what exactly are you trying to do?

moved

from Excel, it is a macro in Excel. i want to be able to clear out the IE browser cache and history using a macro.

thanks for the reply

tx12345

Clear IE Browser Cache and History with VBA

Hi

here is something directly from the microsoft's website...

1) CLEARING CACHE

Use WinInet API FindFirstURLCacheEntry to find the first cache entry and use FindNextUrlCacheEntry to enumerate through the cache. Use DeleteUrlCacheEntry to delete each entry.

Note that FindFirstUrlCacheGroup, FindNextUrlCacheGroup, and DeleteUrlCacheGroup are used in the following example. These API functions have only became available with Internet Explorer 5, so proper checking as shown is needed to prevent errors.

The following steps show you how to use the WinInet API in Visual Basic to clear all the files in the cache.

1. Create a new Visual Basic standard EXE project.
2. Create a command button under form1.
3. Paste the following code in the module of form1:

VB Code:
  1. Option Explicit
  2.  
  3. Private Declare Function FindFirstUrlCacheGroup Lib "wininet.dll" ( _
  4.     ByVal dwFlags As Long, _
  5.     ByVal dwFilter As Long, _
  6.     ByRef lpSearchCondition As Long, _
  7.     ByVal dwSearchCondition As Long, _
  8.     ByRef lpGroupId As Date, _
  9.     ByRef lpReserved As Long) As Long
  10.  
  11. Private Declare Function FindNextUrlCacheGroup Lib "wininet.dll" ( _
  12.     ByVal hFind As Long, _
  13.     ByRef lpGroupId As Date, _
  14.     ByRef lpReserved As Long) As Long
  15.    
  16. Private Declare Function DeleteUrlCacheGroup Lib "wininet.dll" ( _
  17.     ByVal sGroupID As Date, _
  18.     ByVal dwFlags As Long, _
  19.     ByRef lpReserved As Long) As Long
  20.    
  21. Private Declare Function FindFirstUrlCacheEntry Lib "wininet.dll" Alias "FindFirstUrlCacheEntryA" ( _
  22.     ByVal lpszUrlSearchPattern As String, _
  23.     ByRef lpFirstCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _
  24.     ByRef lpdwFirstCacheEntryInfoBufferSize As Long) As Long
  25.    
  26. Private Type INTERNET_CACHE_ENTRY_INFO
  27.     dwStructSize As Long
  28.     szRestOfData(1024) As Long
  29. End Type
  30.  
  31. Private Declare Function DeleteUrlCacheEntry Lib "wininet.dll" Alias "DeleteUrlCacheEntryA" ( _
  32.     ByVal lpszUrlName As Long) As Long
  33.  
  34. Private Declare Function FindNextUrlCacheEntry Lib "wininet.dll" Alias "FindNextUrlCacheEntryA" ( _
  35.     ByVal hEnumHandle As Long, _
  36.     ByRef lpNextCacheEntryInfo As INTERNET_CACHE_ENTRY_INFO, _
  37.     ByRef lpdwNextCacheEntryInfoBufferSize As Long) As Long
  38.  
  39. Private Const CACHGROUP_SEARCH_ALL = &H0
  40. Private Const ERROR_NO_MORE_FILES = 18
  41. Private Const ERROR_NO_MORE_ITEMS = 259
  42. Private Const CACHEGROUP_FLAG_FLUSHURL_ONDELETE = &H2
  43. Private Const BUFFERSIZE = 2048
  44.  
  45. Private Sub Command1_Click()
  46.     Dim sGroupID As Date
  47.     Dim hGroup As Long
  48.     Dim hFile As Long
  49.     Dim sEntryInfo As INTERNET_CACHE_ENTRY_INFO
  50.     Dim iSize As Long
  51.        
  52.     On Error Resume Next
  53.    
  54.     ' Delete the groups
  55.     hGroup = FindFirstUrlCacheGroup(0, 0, 0, 0, sGroupID, 0)
  56.    
  57.     ' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented
  58.     If Err.Number <> 453 Then
  59.         If (hGroup = 0) And (Err.LastDllError <> 2) Then
  60.             MsgBox "An error occurred enumerating the cache groups" & Err.LastDllError
  61.             Exit Sub
  62.         End If
  63.     Else
  64.         Err.Clear
  65.     End If
  66.    
  67.     If (hGroup <> 0) Then
  68.         'we succeeded in finding the first cache group.. enumerate and
  69.         'delete
  70.         Do
  71.             If (0 = DeleteUrlCacheGroup(sGroupID, CACHEGROUP_FLAG_FLUSHURL_ONDELETE, 0)) Then
  72.                
  73.                ' To avoid error using it with IE4 as FindFirstUrlCacheGroup is not implemented
  74.                If Err.Number <> 453 Then
  75.                  MsgBox "Error deleting cache group " & Err.LastDllError
  76.                  Exit Sub
  77.                Else
  78.                   Err.Clear
  79.                End If
  80.             End If
  81.             iSize = BUFFERSIZE
  82.             If (0 = FindNextUrlCacheGroup(hGroup, sGroupID, iSize)) And (Err.LastDllError <> 2) Then
  83.                 MsgBox "Error finding next url cache group! - " & Err.LastDllError
  84.             End If
  85.         Loop Until Err.LastDllError = 2
  86.     End If
  87.  
  88.   ' Delete the files
  89.     sEntryInfo.dwStructSize = 80
  90.     iSize = BUFFERSIZE
  91.     hFile = FindFirstUrlCacheEntry(0, sEntryInfo, iSize)
  92.     If (hFile = 0) Then
  93.         If (Err.LastDllError = ERROR_NO_MORE_ITEMS) Then
  94.             GoTo done
  95.         End If
  96.         MsgBox "ERROR: FindFirstUrlCacheEntry - " & Err.LastDllError
  97.         Exit Sub
  98.     End If
  99.     Do
  100.         If (0 = DeleteUrlCacheEntry(sEntryInfo.szRestOfData(0))) _
  101.             And (Err.LastDllError <> 2) Then
  102.             Err.Clear
  103.         End If
  104.         iSize = BUFFERSIZE
  105.         If (0 = FindNextUrlCacheEntry(hFile, sEntryInfo, iSize)) And (Err.LastDllError <> ERROR_NO_MORE_ITEMS) Then
  106.             MsgBox "Error:  Unable to find the next cache entry - " & Err.LastDllError
  107.             Exit Sub
  108.         End If
  109.     Loop Until Err.LastDllError = ERROR_NO_MORE_ITEMS
  110. done:
  111.     MsgBox "cache cleared"
  112.     Command1.Enabled = True
  113. End Sub


Hope this helps....

Let me see if i can find something for the 'History'

koolsid

Clear IE Browser Cache and History with VBA

thanks

i found something very similar, but the history option is still somewhat elusive.

also, was wondernig if there is a way to tweak the code so it is not form dependent.

tx

tx12345

Previous Question:  Preload image question  WebDeveloper.com  JavaScriptNext Question:  White line between tables cant get rid of  WebDeveloper.com  HTML

- Source: Clear IE Browser Cache and History with VBA VBForums Office Development
- Previous Question: Preload image question WebDeveloper.com JavaScript
- Next Question: White line between tables cant get rid of WebDeveloper.com HTML