WebBrowser Tips 2 (Collection)

zhaozj2021-02-11  177

Get a specific link in the webpage Private submmand1_click () webbrowser1.navigate "http://www.95557.com/svote.htm"end Sub

Private sub webbrowser1_navigatecomplete2 (Byval PDISP As Object, URL As Variant) Dim A for Each A in WebBrowser1.Document.all if a.tagname = "a" Then if a.href = "http://tech.sina.com.cn / Mobile/capture.shtml "TEN A.Click End if End If Nextend Sub

Option explicitprivate m_bdone as boolean

Private submmand1_click () if m_bdone kil Doc as htmldocument2 set doc = WebBrowser1.document Dim ALINK AS HTMLLINKELEMENT SET ALINK = Doc.Links (0) Alink.Click End IFEND SUB

Private Sub Form_Load () WebBrowser1.naviGate "http://www.95557.com/svote.htm"end Sub

Private sub webbrowser1_documentcomplete (byval pdisp as object, url as variant) m_bdone = trueend sub

=============================================================================================================================================================================================================

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 StructuresPrivate const error_cache_find_fail As Long = 0Private Const ERROR_CACHE_FIND_SUCCESS As Long = 1Private Const ERROR_FILE_NOT_FOUND As Long = 2Private Const ERROR_ACCESS_DENIED As Long = 5Private Const ERROR_INSUFFICIENT_BUFFER As Long = 122Private Const MAX_CACHE_ENTRY_INFO_SIZE As Long = 4096Private Const LMEM_FIXED As Long = & H0Private Const LMEM_ZEROINIT As Long = & H40Public Enum eCacheTypeeNormal = & H1 & eEdited = & H8 & eTrackOffline = & H10 & eTrackOnline = & H20 & eSticky = & H40 & eSparse = & H10000eCookie = & H100000eURLHistory = & H200000eURLFindDefaultFilter = 0 & End EnumPrivate Type FILETIMEdwLowDateTime As LongdwH ighDateTime As LongEnd TypePrivate Type INTERNET_CACHE_ENTRY_INFOdwStructSize As LonglpszSourceUrlName As LonglpszLocalFileName As LongCacheEntryType As Long 'Type of entry returneddwUseCount As LongdwHitRate As LongdwSizeLow As LongdwSizeHigh As LongLastModifiedTime As FILETIMEExpireTime As FILETIMELastAccessTime As FILETIMELastSyncTime As FILETIMElpHeaderInfo As LongdwHeaderInfoSize As LonglpszFileExtension As LongdwExemptDelta As LongEnd Type' ----- --------------------- Internet Cache ApiPrivate Declare Function FindfirstURLCAENTRY LIB "Wininet.dll" Alias ​​"

FindFirstUrlCacheEntryA "(ByVal lpszUrlSearchPattern As String, lpFirstCacheEntryInfo As Any, lpdwFirstCacheEntryInfoBufferSize As Long) As LongPrivate Declare Function FindNextUrlCacheEntry Lib" Wininet.dll "Alias" FindNextUrlCacheEntryA "(ByVal hEnumHandle As Long, lpNextCacheEntryInfo As Any, lpdwNextCacheEntryInfoBufferSize As Long) As LongPrivate Declare Function FindCloseUrlCache LIB "Wininet.dll" (BYVAL HENUMHANDLE AS Long) As longprivate declare function deleteurlcacheentry lib "Wininet.dll" Alias ​​"Deleteurlcacheentrya" (Byval Lpszurlname AS String) AS Long '---------------------------------------------------------------------------------------------------------------- ------------ Memory APIPrivate Declare Function LocalAlloc Lib "kernel32" (ByVal uFlags As Long, ByVal uBytes As Long) As LongPrivate Declare Function LocalFree Lib "kernel32" (ByVal hMem As Long) As LongPrivate Declare Sub copyMemory lib "kernel32" Alias ​​"RTLmoveMemory" (PDEST AS Any, PSource As Any, Byval Dwlength As Long) Private Declare Function Lstrcpya Lib "Kernel 32" (Byval Retval AS String, ByVal PTR AS LO ng) As LongPrivate 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.'Author: Andrew Baker'Date: 03/08/2000 19: 14'Notes: 'Revisions: Function InternetDeleteCache (sCacheFile As String) As BooleanInternetDeleteCache = 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'Author: Andrew Baker'Date: 03/08/2000 19: 14'Notes: 'Revisions: Function InternetCacheList (Optional eFilterType As eCacheType = eNormal) As VariantDim ICEI As INTERNET_CACHE_ENTRY_INFODim lhFile As Long, lBufferSize As Long, lptrBuffer As LongDim sCacheFile As StringDim asURLs () As String, lNumEntries As Long'Determine required buffer sizelBufferSize = 0lhFile = FindFirstUrlCacheEntry (0 &, ByVal 0 &, lBufferSize) If (lhFile = ERROR_CACHE_FIND_FAIL) And (Err.LastDllError = ERROR_INSUFFICIENT_BUFFER) Then'Allocate memory for ICEI structurelptrBuffer = LocalAlloc (LMEM_FIXED, lBufferSize) If lptrBuffer Then'Set a Long pointer to the memory locationCopyMemory ByVal lptrBuffer , lbuffersize, 4'call first find api passing it the pointer to the allocated memorylhfile = FindfirstURLCAENTRY (V bNullString, ByVal lptrBuffer, lBufferSize) '1 = successIf lhFile <> ERROR_CACHE_FIND_FAIL Then'Loop through the cacheDo'Copy data back to structureCopyMemory ICEI, ByVal lptrBuffer, Len (ICEI) If ICEI.CacheEntryType And eFilterType ThensCacheFile = StrFromPtrA (ICEI.lpszSourceUrlName) lNumEntries = lNumEntries 1If lNumEntries = 1 ThenReDim asURLs (1 To 1) ElseReDim Preserve asURLs (1 To lNumEntries) End IfasURLs (lNumEntries) = sCacheFileEnd If'Free memory associated with the last-retrieved fileCall LocalFree (lptrBuffer) 'Call FindNextUrlCacheEntry with buffer Size set to 0. '

Call will fail and return required buffer size.lBufferSize = 0Call FindNextUrlCacheEntry (lhFile, ByVal 0 &, lBufferSize) 'Allocate and assign the memory to the pointerlptrBuffer = LocalAlloc (LMEM_FIXED, lBufferSize) CopyMemory ByVal lptrBuffer, lBufferSize, 4 & Loop While FindNextUrlCacheEntry (lhFile, ByVal lptrBuffer, lBufferSize) End IfEnd IfEnd If'Free memoryCall LocalFree (lptrBuffer) Call FindCloseUrlCache (lhFile) InternetCacheList = asURLsEnd 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'Author: Andrew Baker'Date: 03/08/2000 19: 14'Notes:' Revisions: Function StrFromPtrA (ByVal lptrString As Long) As String'Create bufferStrFromPtrA = String $ ( Lstrlena (Byval LPTRSTRING), 0) 'Copy MemoryCall Lstrcpya (Byval Stromptra, Byval LPTRSTRING) End Function'Demcture Routinesub Test () Dim Avurls As Varian t, vThisValue As VariantOn Error Resume Next'Return an array of all internet cache filesavURLs = InternetCacheListFor Each vThisValue In avURLs'Print filesDebug.Print CStr (vThisValue) Next'Return the an array of all cookiesavURLs = InternetCacheList (eCookie) If MsgBox ( " Delete cookies? ", vbQuestion vbYesNo) = vbYes ThenFor Each vThisValue In avURLs'Delete cookiesInternetDeleteCache CStr (vThisValue) Debug.Print" Deleted "& vThisValueNextElseFor Each vThisValue In avURLs'Print cookie filesDebug.Print vThisValueNextEnd IfEnd Sub

============================================================================================================================================================================================================= ===== Analyze web content, get