[我正在尝试触发用户在输入字段文本中输入代码代码并按Enter时看到的事件
该行有效:
ie.Document.all("ticker").innertext = "BBDCF230"
但是在按下回车键之后,该网页将来自“ BBDCF230”代码的数据加载到其他字段中。我正在尝试使用:
Application.SendKeys "~" '## send the "Enter" keystroke
但不起作用。有任何想法吗?
Sub dxArray(ticker As String, ByRef premioEstimada() As truple, Optional mostrar As Boolean)
Dim ie As InternetExplorer
Set ie = New InternetExplorer
If mostrar = True Then
ie.Visible = True
Else
ie.Visible = False
End If
Application.StatusBar = "DX: " & ticker
ie.Navigate "https://opcoes.net.br/calculadora-Black-Scholes/" & ticker
Do While ie.Busy = True Or ie.ReadyState <> READYSTATE_COMPLETE 'Equivalent = .ReadyState <> 4
Application.Wait (Now + TimeValue("00:00:01")) 'Wait 1 second, then check again.
Loop
ww1 = ie.Document.all("premioDaOpcao").Value
For i = 0 To UBound(premioEstimada)
ie.Document.all("ticker").innertext = ""
ie.Document.all("ticker").innertext = "BBDCF230"
Application.SendKeys "~" '## send the "Enter" keystroke
ie.Document.all("cotacaoAcao").innertext = Replace(CStr(premioEstimada(i).cotacao), ".", ",")
ie.Document.all("btncalcular").Click
Do While ie.Busy = True Or ie.ReadyState <> READYSTATE_COMPLETE Or ie.Document.all("premioDaOpcao").Value = "" 'Equivalent = .ReadyState <> 4
Application.Wait (Now + TimeValue("00:00:01")) 'Wait 1 second, then check again.
Loop
premioEstimada(i).premioEstimado = ie.Document.all("premioDaOpcao").Value
premioEstimada(i).ticker = ticker
Next i
ie.Quit
Set ie = Nothing`enter code here`
End Sub
[不幸的是,我不懂葡萄牙语,所以我不知道您的宏应该做什么。您使用的输入参数我不知道它们来自哪里以及它们有什么用。
这里是更改股票价格时自动设置值的技术。我不是Sendkeys()的朋友,但是对于此页面,我没有设法仅通过html事件获取值。您同时需要
这里是一个简短的宏,用于将代码值传递给要调用的宏:
Sub TickerTest()
Call EnterTicker("BBDCF230")
End Sub
这是输入代码的宏,并自动设置相应的值:
Private Sub EnterTicker(ticker As String)
Const url As String = "https://opcoes.net.br/calculadora-Black-Scholes/"
Dim browser As Object
Dim htmlDoc As Object
Dim nodeInputTicker As Object
'Initialize Internet Explorer, set visibility,
'call URL and wait until page is fully loaded
Set browser = CreateObject("internetexplorer.application")
browser.Visible = True
browser.navigate url
Do Until browser.ReadyState = 4: DoEvents: Loop
'Manual break to complete the page code
'The last three values are hours, minutes, seconds
Application.Wait (Now + TimeSerial(0, 0, 1))
'Shortening the html document reference
Set htmlDoc = browser.document
'Get the input textbox to enter the ticker value
Set nodeInputTicker = htmlDoc.getElementByID("ticker")
'Enter the ticker value
nodeInputTicker.Value = ticker
'Trigger the keypress html event of the ticker input textbox
Call TriggerEvent(htmlDoc, nodeInputTicker, "keypress")
'Send enter
Application.SendKeys "~"
'Wait to load the corresponding values
Application.Wait (Now + TimeSerial(0, 0, 1))
End Sub
这是触发html事件的过程
Private Sub TriggerEvent(htmlDocument As Object, htmlElementWithEvent As Object, eventType As String)
Dim theEvent As Object
htmlElementWithEvent.Focus
Set theEvent = htmlDocument.createEvent("HTMLEvents")
theEvent.initEvent eventType, True, False
htmlElementWithEvent.dispatchEvent theEvent
End Sub