Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

525
Views
KeyDown en WebBrowser (Excel VBA)

¿Hay alguna manera de saber qué tecla se presiona si se usa un navegador web en un formulario de usuario?

Gracias por adelantado :)

Editar: básicamente quiero abrir un formulario de usuario diferente en KeyDown, pero WebBrowser lo impide. Tal vez pueda ejecutar un Sub de mi UserForm desde WebBrowser.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Bien, aquí hay un ejemplo muy básico para mostrar que puede enviar un mensaje a la página HTML desde VBA y enviar un mensaje en la otra dirección:

Formulario de usuario:

 Option Explicit Dim obj As webEvents 'must be Global to remain in scope... 'send a message to the browser page Private Sub CommandButton1_Click() 'would need to escape the text sent if it contains (eg) ' Me.WebBrowser1.Document.parentWindow.execScript "MessageIn('" & Me.TextBox1.Text & "')" End Sub 'receive a message from the browser page Public Sub HandleBrowserMessage(s As String) MsgBox "From web:" & vbLf & s End Sub Private Sub UserForm_Initialize() Me.WebBrowser1.Navigate "about:blank" 'load a blank document Application.Wait Now + TimeSerial(0, 0, 1) With Me.WebBrowser1.Document 'load some HTML .Open "text/html" .write Sheet1.Range("A1").Value .Close End With Set obj = New webEvents 'new class instance Set obj.ParentForm = Me 'for the callback Set obj.HtmlOutput = Me.WebBrowser1.Document.getElementById("msgOut") End Sub

Módulo de clase webEvents :

 Option Explicit Public ParentForm As Object 'not As userform! Public WithEvents HtmlOutput As MSHTML.HTMLInputElement 'handling click since change seems unreliable Private Function HtmlOutput_onclick() As Boolean ParentForm.HandleBrowserMessage CStr(HtmlOutput.Value) End Function

HTML en la celda A1:

 <html><body> <input type="hidden" id="msgOut" /><br> To VBA: <input type="text" size="20" id="msg" /> <button onclick="MessageOut()">Send to VBA</button><br><br> From VBA: <span id="showMessage" style="color:#F00"></span><br> <script> function MessageIn(s){ document.getElementById("showMessage").innerText = s; } function MessageOut(s){ var el = document.getElementById("msgOut"); el.value = document.getElementById("msg").value; el.click(); } </script> </body></html>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!