日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Excel VBA操作网页 显示滚动进度条

發布時間:2023/12/18 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Excel VBA操作网页 显示滚动进度条 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
這兩天用Excel VBA實現了登入到網站,提交表單并將搜索結果寫回Excel的功能。在寫回結果之前同事顯示滾動的進度條。
在Excel VBE界面,需要在reference中加入以下3個library
Microsoft Internet Controls
Microsoft HTML Object Library
Microsoft ActiveX Data Objects 2.8 Library
VBA代碼如下:

Sub GoogleSearch()
'This project includes references to "Microsoft Internet Controls" and
'"Microsoft HTML Object Library" and
'"Microsoft ActiveX Data Objects 2.8 Library"

'Variable declarations
Dim myIE As New InternetExplorer 'New '
Dim myURL As String
Dim myDoc As HTMLDocument
Dim strUsername As String
Dim strPassword As String
Dim strKw As String
Dim strHtml As String

'Show process bar
'ShowProcessBar
BeginProgress

'Set starting URL and login string
Sheets("SearchForm").Select
myURL = Range("B1").Value
strUsername = Range("B2").Value
strPassword = Range("B3").Value
strSearch = Range("B5").Value

'Make IE navigate to the URL and make browser visible
myIE.navigate myURL
'myIE.Visible = True

'Wait for the page to load
Do While myIE.Busy Or myIE.readyState <> READYSTATE_COMPLETE
PushProgress (1)
DoEvents
Loop

'Set IE document into object
Set myDoc = myIE.document

'Enter search string on form
myDoc.forms(0).q.Value = strSearch

'Submit form
myDoc.f.submit

'Wait for the page to load
Do While myIE.Busy Or myIE.readyState <> READYSTATE_COMPLETE
PushProgress (1)
DoEvents
Loop

'Save html content to file and then copy data to Excel sheet
strHtml = myDoc.documentElement.outerHTML

'Save html
strFileUrl = ThisWorkbook.Path & "/searchResults.html"
WriteStrToFile strHtml, strFileUrl, "UTF-8"

'Copy data from html file
Workbooks.Open strFileUrl
Selection.UnMerge
Range("A1:L80").Select
HtmlContentValue = Range("A1:L80").Value
ActiveWorkbook.Close (False)
ThisWorkbook.Activate
Sheets("ResultsReports").Select
Range("A1:L80").Value = HtmlContentValue
Selection.UnMerge
Range("A1:L80").Select

'MsgBox "End Search" 'myIE.LocationName
'Normally exit
'End progress bar
Sheets("SearchForm").Select
EndProgress
Sheets("ResultsReports").Select
myIE.Quit
Set myIE = Nothing

End Sub

Private Sub WriteStrToFile(ByVal strText As String, ByVal strPath As String, ByVal strCharSet As String)
'Dim objText As New FileSystemObject
'Please add reference: Microsoft ActiveX Data Objects 2.8 Library
Dim objText As New ADODB.Stream

objText.Type = adTypeText
objText.Open
objText.Charset = strCharSet
objText.WriteText strText, adWriteChar
objText.SaveToFile strPath, adSaveCreateOverWrite
objText.Close
Set objText = Nothing

End Sub

Private Sub BeginProgress()
Range("C6").Value = 0
Range("D6").Value = 10000
'For j = 1 To Range("D6").Value
' Range("C6").Value = j
'Next j
End Sub

Private Sub PushProgress(ByVal pushValue As Integer)
Range("C6").Value = Range("C6").Value + pushValue
If (Range("C6").Value = 9999) Then
Range("C6").Value = 0
End If
End Sub

Private Sub EndProgress()
Range("C6").Value = 10000
Range("D6").Value = 10000
'For j = 1 To Range("D6").Value
' Range("C6").Value = j
'Next j
End Sub


效果圖如下:
[img]http://joeykh.iteye.com/upload/picture/pic/38524/76c699ed-477a-311d-a216-f207a250e234.gif[/img]

關于滾動進度條的制作可參考
http://www.cnblogs.com/jinliangliu/archive/2006/07/15/451314.html

之前沒玩過VB,覺得還挺有意思的。最后制成的Excel在附件中。

總結

以上是生活随笔為你收集整理的Excel VBA操作网页 显示滚动进度条的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。