接http://www.csdn.net/expert/topic/689/689202.xml?temp=4.604518E-03的代码:(那边贴不了了)Public Sub SetComboByText(ByVal nIndex As Integer, ByVal szName As String, ByVal szText As String) 'Written by Stephan 'Posted by Chris Kesler
Set m_HTMLDoc = MainIE.document
Dim q As Integer, i As Integer For q = 0 To m_HTMLDoc.Forms(nIndex).length - 1 If (m_HTMLDoc.Forms(nIndex)(q).Name = szName) Then For i = 0 To m_HTMLDoc.Forms(nIndex)(q).length - 1 If m_HTMLDoc.Forms(nIndex)(q).Options(i).Value = szText Then m_HTMLDoc.Forms(nIndex)(q).Options(i).Selected = True Exit For End If Next i End If Next q End SubPublic Function GetRadioState(ByVal nIndex As Integer, ByVal szGroupID As String, ByVal szName As String) As Boolean 'Written by Ultimatum (with inspiration from Stephan)
Set m_HTMLDoc = MainIE.document
Dim q As Integer For q = 0 To m_HTMLDoc.Forms(nIndex).length - 1 If (m_HTMLDoc.Forms(nIndex)(q).Name = szGroupID) And (m_HTMLDoc.Forms(nIndex)(q).Value = szName) Then GetRadioState = m_HTMLDoc.Forms(nIndex)(q).Checked Exit For End If
Next qEnd FunctionPublic Sub SetRadioState(ByVal nIndex As Integer, ByVal szGroupID As String, ByVal szName As String, ByVal bOn As Boolean) 'Written by Stephan 'Posted by Chris Kesler
Set m_HTMLDoc = MainIE.document
Dim q As Integer For q = 0 To m_HTMLDoc.Forms(nIndex).length - 1 If (m_HTMLDoc.Forms(nIndex)(q).Name = szGroupID) And (m_HTMLDoc.Forms(nIndex)(q).Value = szName) Then m_HTMLDoc.Forms(nIndex)(q).Checked = True Exit For End If
Next q
End SubPublic Function GetCheckedRadioFromGroup(ByVal nIndex As Integer, ByVal szGroupID As String) As String 'Written by Ultimatum
Set m_HTMLDoc = MainIE.document
Dim q As Integer For q = 0 To m_HTMLDoc.Forms(nIndex).length - 1 If (m_HTMLDoc.Forms(nIndex)(q).Name = szGroupID) And (m_HTMLDoc.Forms(nIndex)(q).Checked = True) Then GetCheckedRadioFromGroup = m_HTMLDoc.Forms(nIndex)(q).Value Exit For End If
Next q
End FunctionPublic Function GetLink(ByVal nIndex As Integer) As String Set m_HTMLDoc = MainIE.document GetLink = m_HTMLDoc.links(nIndex).href
End FunctionPublic Function GetImage(ByVal nIndex As Integer) As String Set m_HTMLDoc = MainIE.document GetImage = m_HTMLDoc.images(nIndex).src
End FunctionPublic Function GetSource() As String Set m_HTMLDoc = MainIE.document GetSource = m_HTMLDoc.All(0).outerHTML
End FunctionPublic Sub LoadHTML(ByVal szSource As String) MainIE.navigate "about:" & szSource
End SubPublic Function GetTitle() As String Set m_HTMLDoc = MainIE.document GetTitle = m_HTMLDoc.Title
End FunctionPublic Property Get DocumentObject() As Object Set DocumentObject = m_HTMLDoc
End PropertyPrivate Sub Script_Sink_Error() Err.Raise Script.Error.Number, Script.Error.Source, Script.Error.Description
End Sub
感谢您使用微软产品。您可以使用Webbrowser Control显示一个html页,并获得html document内部的对象。当点击html的一个项目如按钮时,Webbrowser Control不直接提供捕获html document内部控件事件的方法,您需要按以下步骤编程响应document对象的事件。1. 添加一个Class Module,自定义一个类clsForward,作用是把document对象的事件传播到document的容器(如Webbrowser Control)中并调用目标过程模块。加入下列代码,其中布尔类型变量bInstantiated是记录是否设定了目标过程的标志,oObject是将要对捕获的事件进行处理的对象,如目标过程模块所在的form,sMethod是目标过程名。Option Explicit Dim oObject As Object Dim sMethod As String Dim bInstantiated As Boolean Private Sub Class_Initialize() bInstantiated = False End Sub Public Sub Set_Destination(oInObject As Object, sInMethod As String) Set oObject = oInObject sMethod = sInMethod bInstantiated = True End Sub Public Sub My_Default_Method() If bInstantiated Then CallByName oObject, sMethod, VbMethod ‘ 调用sMethod指定的VB procedure End If End Sub2. 在VB的Form中加入Webbrowser Control,并写入包含按钮btnMyButton的html,声明clsForward类的对象,设定目标过程为Some_Procedure,在响应按钮事件时调用,加入以下代码,Option Explicit Public Sub Some_Procedure() MsgBox " Some_Procedure was called” End Sub Private Sub Form_Load() WebBrowser1.Navigate2 "about:blank" End Sub Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant) Dim cfForward As clsForward Dim sHTML As String sHTML = "<P>This is some text.</P>" sHTML = sHTML & "<P>And here is a button.</P>" sHTML = sHTML & "<BUTTON ID=btnMyButton>" sHTML = sHTML & "Click this button.</BUTTON>" '将包含HTML代码写入浏览器 WebBrowser1.Document.body.innerHTML = sHTML '将事件响应类连接到页面的按钮btnMyButton上 Set cfForward = New clsForward cfForward.Set_Destination Me, "Some_Procedure" WebBrowser1.Document.All("btnMyButton").onclick = cfForward End Sub以下链接提供了详细信息和例子。详细信息请参考以下链接: Handling Events in Visual Basic Applications http://msdn.microsoft.com/workshop/browser/webbrowser/tutorials/forward.asp CallByName Function http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctcallbynamefunction.asp - 微软全球技术中心 VB技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款 (http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。 为了为您创建更好的讨论环境,请参加我们的用户满意度调查 (http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。
http://www.microsoft.com&item=member&login=www.21code.com&passwordhash=1017207122&downloadsrv=china_bjtelcom&file=&[email protected]/codebase/go.php?data=dmJjb2RlL3ZiY2RqbS9IeXBlckFwcC5aSVA=
'Written by Stephan
'Posted by Chris Kesler
Set m_HTMLDoc = MainIE.document
Dim q As Integer, i As Integer For q = 0 To m_HTMLDoc.Forms(nIndex).length - 1
If (m_HTMLDoc.Forms(nIndex)(q).Name = szName) Then
For i = 0 To m_HTMLDoc.Forms(nIndex)(q).length - 1
If m_HTMLDoc.Forms(nIndex)(q).Options(i).Value = szText Then
m_HTMLDoc.Forms(nIndex)(q).Options(i).Selected = True
Exit For
End If
Next i
End If
Next q
End SubPublic Function GetRadioState(ByVal nIndex As Integer, ByVal szGroupID As String, ByVal szName As String) As Boolean
'Written by Ultimatum (with inspiration from Stephan)
Set m_HTMLDoc = MainIE.document
Dim q As Integer For q = 0 To m_HTMLDoc.Forms(nIndex).length - 1 If (m_HTMLDoc.Forms(nIndex)(q).Name = szGroupID) And (m_HTMLDoc.Forms(nIndex)(q).Value = szName) Then
GetRadioState = m_HTMLDoc.Forms(nIndex)(q).Checked
Exit For
End If
Next qEnd FunctionPublic Sub SetRadioState(ByVal nIndex As Integer, ByVal szGroupID As String, ByVal szName As String, ByVal bOn As Boolean)
'Written by Stephan
'Posted by Chris Kesler
Set m_HTMLDoc = MainIE.document
Dim q As Integer For q = 0 To m_HTMLDoc.Forms(nIndex).length - 1 If (m_HTMLDoc.Forms(nIndex)(q).Name = szGroupID) And (m_HTMLDoc.Forms(nIndex)(q).Value = szName) Then
m_HTMLDoc.Forms(nIndex)(q).Checked = True
Exit For
End If
Next q
End SubPublic Function GetCheckedRadioFromGroup(ByVal nIndex As Integer, ByVal szGroupID As String) As String
'Written by Ultimatum
Set m_HTMLDoc = MainIE.document
Dim q As Integer For q = 0 To m_HTMLDoc.Forms(nIndex).length - 1 If (m_HTMLDoc.Forms(nIndex)(q).Name = szGroupID) And (m_HTMLDoc.Forms(nIndex)(q).Checked = True) Then
GetCheckedRadioFromGroup = m_HTMLDoc.Forms(nIndex)(q).Value
Exit For
End If
Next q
End FunctionPublic Function GetLink(ByVal nIndex As Integer) As String
Set m_HTMLDoc = MainIE.document
GetLink = m_HTMLDoc.links(nIndex).href
End FunctionPublic Function GetImage(ByVal nIndex As Integer) As String
Set m_HTMLDoc = MainIE.document
GetImage = m_HTMLDoc.images(nIndex).src
End FunctionPublic Function GetSource() As String
Set m_HTMLDoc = MainIE.document
GetSource = m_HTMLDoc.All(0).outerHTML
End FunctionPublic Sub LoadHTML(ByVal szSource As String)
MainIE.navigate "about:" & szSource
End SubPublic Function GetTitle() As String
Set m_HTMLDoc = MainIE.document
GetTitle = m_HTMLDoc.Title
End FunctionPublic Property Get DocumentObject() As Object
Set DocumentObject = m_HTMLDoc
End PropertyPrivate Sub Script_Sink_Error()
Err.Raise Script.Error.Number, Script.Error.Source, Script.Error.Description
End Sub
Dim sMethod As String
Dim bInstantiated As Boolean Private Sub Class_Initialize()
bInstantiated = False
End Sub Public Sub Set_Destination(oInObject As Object, sInMethod As String)
Set oObject = oInObject
sMethod = sInMethod
bInstantiated = True
End Sub Public Sub My_Default_Method()
If bInstantiated Then
CallByName oObject, sMethod, VbMethod ‘ 调用sMethod指定的VB procedure
End If
End Sub2. 在VB的Form中加入Webbrowser Control,并写入包含按钮btnMyButton的html,声明clsForward类的对象,设定目标过程为Some_Procedure,在响应按钮事件时调用,加入以下代码,Option Explicit Public Sub Some_Procedure()
MsgBox " Some_Procedure was called”
End Sub Private Sub Form_Load()
WebBrowser1.Navigate2 "about:blank"
End Sub Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant) Dim cfForward As clsForward Dim sHTML As String
sHTML = "<P>This is some text.</P>"
sHTML = sHTML & "<P>And here is a button.</P>"
sHTML = sHTML & "<BUTTON ID=btnMyButton>"
sHTML = sHTML & "Click this button.</BUTTON>" '将包含HTML代码写入浏览器
WebBrowser1.Document.body.innerHTML = sHTML '将事件响应类连接到页面的按钮btnMyButton上
Set cfForward = New clsForward
cfForward.Set_Destination Me, "Some_Procedure"
WebBrowser1.Document.All("btnMyButton").onclick = cfForward
End Sub以下链接提供了详细信息和例子。详细信息请参考以下链接:
Handling Events in Visual Basic Applications
http://msdn.microsoft.com/workshop/browser/webbrowser/tutorials/forward.asp
CallByName Function
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbenlr98/html/vafctcallbynamefunction.asp
- 微软全球技术中心 VB技术支持本贴子以“现状”提供且没有任何担保,同时也没有授予任何权利。具体事项可参见使用条款
(http://support.microsoft.com/directory/worldwide/zh-cn/community/terms_chs.asp)。
为了为您创建更好的讨论环境,请参加我们的用户满意度调查
(http://support.microsoft.com/directory/worldwide/zh-cn/community/survey.asp?key=(S,49854782))。