0

I am trying to fire the event we see when user writes a ticker code in a input field text and press enter

the line works:

ie.Document.all("ticker").innertext = "BBDCF230"

But just after, pressing enter, the webpage loads data from "BBDCF230" code into the others fields. I am trying to do this using :

Application.SendKeys "~" '## send the "Enter" keystroke

but its not working. Any Ideas?

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
2
  • I don't know what is your issue. If you use "https://opcoes.net.br/calculadora-Black-Scholes/" & ticker you have what you want. The rest of your code has a lot of errors. Two examples: You cannot use Sendkeys() if IE is not visible and to set the value of an input text box you must use Value not innerText. But like I say, you only need the url with the ticker at it's end. Commented Jun 8, 2020 at 10:47
  • Thanks for the answer. Perhaps I was not clear in my post. Ok, I will make IE visible, this is not the problem. Okay, I'll use "value" instead of "innertext". The code I put in is a kind of draft. My main need is to fill the "ticker" field with different codes and trigger the "click enter" event without having to navigate directly to "opcoes.net.br/calculadora-Black-Scholes/ticker". Commented Jun 9, 2020 at 9:59

1 Answer 1

1

Unfortunately, I don't understand what your macro is supposed to do, since I don't speak Portuguese. You use input parameters that I don't know where they come from and what they are good for.

Here is the technique to set the values automatically when the ticker value is changed. I am not a friend of Sendkeys(), but for this page I did not manage to get the values only via html events. You need both

Here is a short macro to pass a ticker value to the macro to be called:

Sub TickerTest()
  Call EnterTicker("BBDCF230")
End Sub

This is the macro to enter the ticker and automatically set the corresponding values:

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

This is the procedure to trigger a html event

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.