How to delete specific cookies with Access VBA
-
Friday, September 21, 2012 7:16 PM
I guess the subject title pretty much tells it all. I need to delete specific cookies using a VBA program. Does anyone have code for this?
Thanks
50% of programming is coding. The other 90% is debugging
All Replies
-
Friday, September 21, 2012 7:38 PM
Out of curiosity, why? Cookies are just files, so you should be able to use the Kill statement on them, but how will you know which ones to delete? Be aware, too, that where the cookies are stored depends on the operating system. By default, they're in C:\Users\<userid>\AppData\Roaming\Microsoft\Windows\Cookies (at least, that's where they are in Vista and Win 7: sorry, don't have machines with any other OS handy!)
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/djsteele (no e-mails, please!)
Co-author Access Solutions — Tips, Tricks, and Secrets from Microsoft Access MVPs (ISBN 978-0-470-59168-0) -
Friday, September 21, 2012 7:53 PMI am using windows 7 pro. I was hoping that the solution would include a way to list cookies and then maybe by knowing the website whose cookies I want to delete, I could determine what one(s) I'd be interested in deleting. If not, I would just delete all cookies created after a certain date or time. As to why, I submit numerous searches to a public website. After hitting a number of searches, it starts displaying a captcha text between every search requiring human intervention
50% of programming is coding. The other 90% is debugging
-
Friday, September 21, 2012 8:31 PMDid you look in the folder I mentioned? As you can see, it's not particularly easy determining which cookie came from which site. On the plus side, though, Kill does work to delete the files.
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/djsteele (no e-mails, please!)
Co-author Access Solutions — Tips, Tricks, and Secrets from Microsoft Access MVPs (ISBN 978-0-470-59168-0) -
Friday, September 21, 2012 11:06 PM
I havent yet but what I'll do is, as soon as the captcha text displays, I will look at the most recent files added to that folder to see if theres one with a name that rings a bell. Or, I guess I could delete 1 file at a time, starting with the latest and going back until search is restored. Then that will tell me what file name to look for in the future (I hope). By the way, at the time I am blocked, can I assume that a new cookie was created with the blocking instructions, or might it be an old cookie that was just updated?
Thanks
50% of programming is coding. The other 90% is debugging
-
Saturday, September 22, 2012 12:46 AM
Sorry, not sure. I suspect it updates an existing cookie, but you'll have to play and find out.
Good luck!
Doug Steele, Microsoft Access MVP
http://www.AccessMVP.com/djsteele (no e-mails, please!)
Co-author Access Solutions — Tips, Tricks, and Secrets from Microsoft Access MVPs (ISBN 978-0-470-59168-0) -
Saturday, September 22, 2012 1:00 AMThanks pal. I will post my findings
50% of programming is coding. The other 90% is debugging
-
Monday, September 24, 2012 4:46 AMModerator
Hi JonWayn,
Welcome to the Access forum.
Please check this link to see if it helps:
http://www.visualbasic.happycodings.com/Files_Directories_Drives/code46.html
List and delete files in the internet cache (inc. cookies) The following code can be used to query and delete files in the internet cache (including cookies). A demonstration routine can be found at the bottom of this post. Note, the enumerated type eCacheType is not supported in Excel 97, but can be changed to a list of Public Constants eg. Public Const eNormal = &H1&. Option Explicit '--------------------------Types, consts and structures Private Const ERROR_CACHE_FIND_FAIL As Long = 0 Private Const ERROR_CACHE_FIND_SUCCESS As Long = 1 Private Const ERROR_FILE_NOT_FOUND As Long = 2 Private Const ERROR_ACCESS_DENIED As Long = 5 Private Const ERROR_INSUFFICIENT_BUFFER As Long = 122 Private Const MAX_CACHE_ENTRY_INFO_SIZE As Long = 4096 Private Const LMEM_FIXED As Long = &H0 Private Const LMEM_ZEROINIT As Long = &H40 Public Enum eCacheType eNormal = &H1& eEdited = &H8& eTrackOffline = &H10& eTrackOnline = &H20& eSticky = &H40& eSparse = &H10000 eCookie = &H100000 eURLHistory = &H200000 eURLFindDefaultFilter = 0& End Enum Private Type FILETIME dwLowDateTime As Long dwHighDateTime As Long End Type Private Type INTERNET_CACHE_ENTRY_INFO dwStructSize As Long lpszSourceUrlName As Long lpszLocalFileName As Long CacheEntryType As Long 'Type of entry returned dwUseCount As Long dwHitRate As Long dwSizeLow As Long dwSizeHigh As Long LastModifiedTime As FILETIME ExpireTime As FILETIME LastAccessTime As FILETIME LastSyncTime As FILETIME lpHeaderInfo As Long dwHeaderInfoSize As Long lpszFileExtension As Long dwExemptDelta As Long End Type '--------------------------Internet Cache API Private Declare Function FindFirstUrlCacheEntry Lib "Wininet.dll" Alias "FindFirstUrlCacheEntryA" (ByVal lpszUrlSearchPattern As String, lpFirstCacheEntryInfo As Any, lpdwFirstCacheEntryInfoBufferSize As Long) As Long Private Declare Function FindNextUrlCacheEntry Lib "Wininet.dll" Alias "FindNextUrlCacheEntryA" (ByVal hEnumHandle As Long, lpNextCacheEntryInfo As Any, lpdwNextCacheEntryInfoBufferSize As Long) As Long Private Declare Function FindCloseUrlCache Lib "Wininet.dll" (ByVal hEnumHandle As Long) As Long Private Declare Function DeleteUrlCacheEntry Lib "Wininet.dll" Alias "DeleteUrlCacheEntryA" (ByVal lpszUrlName As String) As Long '--------------------------Memory API Private Declare Function LocalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal uBytes As Long) As Long Private Declare Function LocalFree Lib "kernel32" (ByVal hMem As Long) As Long Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDest As Any, pSource As Any, ByVal dwLength As Long) Private Declare Function lstrcpyA Lib "kernel32" (ByVal RetVal As String, ByVal Ptr As Long) As Long Private Declare Function lstrlenA Lib "kernel32" (ByVal Ptr As Any) As Long 'Purpose : Deletes the specified internet cache file 'Inputs : sCacheFile The name of the cache file 'Outputs : Returns True on success. Function InternetDeleteCache(sCacheFile As String) As Boolean InternetDeleteCache = CBool(DeleteUrlCacheEntry(sCacheFile)) End Function 'Purpose : Returns an array of files stored in the internet cache 'Inputs : eFilterType An enum which filters the files returned by their type 'Outputs : A one dimensional, one based, string array containing the names of the files Function InternetCacheList(Optional eFilterType As eCacheType = eNormal) As Variant Dim ICEI As INTERNET_CACHE_ENTRY_INFO Dim lhFile As Long, lBufferSize As Long, lptrBuffer As Long Dim sCacheFile As String Dim asURLs() As String, lNumEntries As Long 'Determine required buffer size lBufferSize = 0 lhFile = FindFirstUrlCacheEntry(0&, ByVal 0&, lBufferSize) If (lhFile = ERROR_CACHE_FIND_FAIL) And (Err.LastDllError = ERROR_INSUFFICIENT_BUFFER) Then 'Allocate memory for ICEI structure lptrBuffer = LocalAlloc(LMEM_FIXED, lBufferSize) If lptrBuffer Then 'Set a Long pointer to the memory location CopyMemory ByVal lptrBuffer, lBufferSize, 4 'Call first find API passing it the pointer to the allocated memory lhFile = FindFirstUrlCacheEntry(vbNullString, ByVal lptrBuffer, lBufferSize) '1 = success If lhFile <> ERROR_CACHE_FIND_FAIL Then 'Loop through the cache Do 'Copy data back to structure CopyMemory ICEI, ByVal lptrBuffer, Len(ICEI) If ICEI.CacheEntryType And eFilterType Then sCacheFile = StrFromPtrA(ICEI.lpszSourceUrlName) lNumEntries = lNumEntries + 1 If lNumEntries = 1 Then ReDim asURLs(1 To 1) Else ReDim Preserve asURLs(1 To lNumEntries) End If asURLs(lNumEntries) = sCacheFile End If 'Free memory associated with the last-retrieved file Call LocalFree(lptrBuffer) 'Call FindNextUrlCacheEntry with buffer size set to 0. 'Call will fail and return required buffer size. lBufferSize = 0 Call FindNextUrlCacheEntry(lhFile, ByVal 0&, lBufferSize) 'Allocate and assign the memory to the pointer lptrBuffer = LocalAlloc(LMEM_FIXED, lBufferSize) CopyMemory ByVal lptrBuffer, lBufferSize, 4& Loop While FindNextUrlCacheEntry(lhFile, ByVal lptrBuffer, lBufferSize) End If End If End If 'Free memory Call LocalFree(lptrBuffer) Call FindCloseUrlCache(lhFile) InternetCacheList = asURLs End Function 'Purpose : Converts a pointer an ansi string into a string. 'Inputs : lptrString A long pointer to a string held in memory 'Outputs : The string held at the specified memory address Function StrFromPtrA(ByVal lptrString As Long) As String 'Create buffer StrFromPtrA = String$(lstrlenA(ByVal lptrString), 0) 'Copy memory Call lstrcpyA(ByVal StrFromPtrA, ByVal lptrString) End Function 'Demonstration routine Sub Test() Dim avURLs As Variant, vThisValue As Variant On Error Resume Next 'Return an array of all internet cache files avURLs = InternetCacheList For Each vThisValue In avURLs 'Print files Debug.Print CStr(vThisValue) Next 'Return the an array of all cookies avURLs = InternetCacheList(eCookie) If MsgBox("Delete cookies?", vbQuestion + vbYesNo) = vbYes Then For Each vThisValue In avURLs 'Delete cookies InternetDeleteCache CStr(vThisValue) Debug.Print "Deleted " & vThisValue Next Else For Each vThisValue In avURLs 'Print cookie files Debug.Print vThisValue Next End If End SubHave a nice day.
This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.
Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Yoyo JiangMicrosoft Contingent Staff, Moderator Monday, October 01, 2012 6:03 AM
-
Monday, October 01, 2012 6:04 AMModerator
Hi JonWayn,
I temporarily marked the reply as answer, and you can unmark it if it provides no help.
Have a nice day.
Yoyo Jiang[MSFT]
MSDN Community Support | Feedback to us

