Private Sub Command1_Click()
check (WebBrowser1)
End SubSub check(wb) '<< 這裡用wb 為什麼錯誤, 應該寫什麼來定義 webbrowser??For i = 0 To wb.Document.All.length - 1
If UCase(wb.Document.All(i).tagName) = "A" Then
Set ggrepa = CreateObject("VBScript.RegExp")
ggrepa.Pattern = Text3.Text
ggrepa.Global = True
Set ggmat = ggrepa.Execute(wb.Document.All(i).href)
For Each ggresult In ggmat
wb.Document.All(i).Click
Next
End If
Next iEnd Sub
程序中有很多個webbrowser都要用到check中的代碼 不想重複代碼多次 eg: check(webbrowser1),check(webbrowser2),check(webbrowser3),.... 而wb每次代表指定的webbrowser
(1)Sub check(wb)-----wb是WebBrowser的实例,属对象变量,要声明它的类型.比如Sub check(wb As WebBrowser)或者Sub check(wb As Object);
(2)check (WebBrowser1)应为check WebBrowser1 ---没括号,或 Call check (WebBrowser1);
下面的代码运行就很好:
'引用Microsoft VBScript Regular Expressions 5.5
Private Sub Command1_Click()
check WebBrowser1
End SubSub check(wb As Object) '<< 這裡用wb 為什麼錯誤, 應該寫什麼來定義 webbrowser??For i = 0 To wb.Document.All.length - 1
If UCase(wb.Document.All(i).tagName) = "A" Then
Set ggrepa = CreateObject("VBScript.RegExp")
ggrepa.Pattern = Text3.Text
ggrepa.Global = True
Set ggmat = ggrepa.Execute(wb.Document.All(i).href)
For Each ggresult In ggmat
wb.Document.All(i).Click
Next
End If
Next iEnd SubPrivate Sub Form_Load()
WebBrowser1.Navigate "http://topic.csdn.net/u/20090428/02/a6e8839a-ca06-42ff-9b25-18c1daac14e6.html"
End Sub
Dim lnk As Object
For Each lnk In wb.Document.links
If InStr(lnk.href, Text3.Text) Then lnk.Click
Next
End Sub