0

我正在尝试触发我们在用户在输入字段文本中写入股票代码并按 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
4

1 回答 1

1

不幸的是,我不明白你的宏应该做什么,因为我不会说葡萄牙语。您使用的输入参数我不知道它们来自哪里以及它们有什么用处。

这是在代码值更改时自动设置值的技术。我不是 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
于 2020-06-09T13:31:40.173 回答