Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

528
Vistas
KeyDown in WebBrowser (Excel VBA)

Is there a way to know what key is pressed if a WebBrowser is used in a UserForm?

Thanks in advance :)

Edit: I basically want to open a different UserForm on KeyDown, but the WebBrowser prevents that. Maybe I can run a Sub of my UserForm from the WebBrowser.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

OK here's a very basic example to show you can send a message to the HTML page from VBA, and send a message in the other direction:

UserForm:

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

Class module 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 in Cell 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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda