vba获取htmldom,vba–在HTMLElement上使⽤
getElementByI。。。
我也不喜欢
所以使⽤javascript:
Public Function GetJavaScriptResult(doc as HTMLDocument, jsString As String) As String
Dim el As IHTMLElement
Dim nd As HTMLDOMTextNode
Set el = ateElement("INPUT")
Do
el.ID = GenerateRandomAlphaString(100)
Loop ElementById(el.ID) Is Nothing
el.Style.display = "none"
Set nd = Document.appendChild(el)
doc.parentWindow.ExecScript "ElementById('" & el.ID & "').value = " & jsString
GetJavaScriptResult = ElementById(el.ID).Value
End Function
Function GenerateRandomAlphaString(Length As Long) As String
Dim i As Long
Dim Result As String
Randomize Timer
For i = 1 To Length
Result = Result & Chr(Int(Rnd(Timer) * 26 + 65 + Round(Rnd(Timer)) * 32))
Next ihtml document是什么
GenerateRandomAlphaString = Result
End Function
如果您有任何问题,请告诉我们我将上下⽂从⼀个⽅法改为⼀个函数.
顺便说⼀句,你使⽤什么版本的IE?我怀疑你在< IE8.如果您升级到IE8,我认为它将更新shdocvw.dll到ieframe.dll,您将能够使⽤document.querySelector / All. 编辑 评论回应不是⼀个真正的评论:
在VBA中基本上做这个的⽅法是遍历⼦节点.问题是你没有得到正确的返回类型.你可以通过⾃⼰的类(另外)实现IHTMLElement和IHTMLElementCollection来解决这个问题.但是,对于我来说,这样做并没有得到报酬,所以这样做太多了.如果您确定,请阅读VB6 / VBA的Implements关键字.
Public Function getSubElementsByTagName(el As IHTMLElement, tagname As String) As Collection
Dim descendants As New Collection
Dim results As New Collection
Dim i As Long
getDescendants el, descendants
For i = 1 To descendants.Count
If descendants(i).tagname = tagname Then
results.Add descendants(i)
End If
Next i
getSubElementsByTagName = results
End Function
Public Function getDescendants(nd As IHTMLElement, ByRef descendants As Collection) Dim i As Long
descendants.Add nd
For i = 1 To nd.Children.Length
getDescendants nd.Children.Item(i), descendants
Next i
End Function
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论