1

i already created a sample but i dont know how to pass value from js to vba using webbrowser control

Dim GetValFromJS As String

Private Sub UserForm_Initialize()
    Dim NEWHTML As String
    Dim i As Integer
    
    NEWHTML = "<HTML>"
    NEWHTML = NEWHTML & "<body>"
    For i = 1 To 5
        NEWHTML = NEWHTML & "<a href='#' onclick='return GetVal(" & i & ")'>" & i & "</a>"
        NEWHTML = NEWHTML & "<br>"
    Next i
    NEWHTML = NEWHTML & "<script type='text/javascript'>"
    NEWHTML = NEWHTML & "function GetVal(StrVal) {"
    NEWHTML = NEWHTML & "alert(StrVal);"
    '<---- how can i pass the value of StrVal to GetValFromJS
    NEWHTML = NEWHTML & "return false;"
    NEWHTML = NEWHTML & "}"
    NEWHTML = NEWHTML & "</script>"
    
    NEWHTML = NEWHTML & "</body>"
    NEWHTML = NEWHTML & "</html>"
    
    With WebBrowser1
        .Navigate2 "about:blank"
        .Document.Write NEWHTML
    End With
End Sub

instead of creating an alert in JS, i would like to pass the value from JS to VB Variable which is GetValFromJS

EDIT

THIS IS FROM USERFORM

Option Explicit
    
    Dim o As clsHtmlText '<< instance of our "withEvents" class
    
    Private Sub UserForm_Activate()
        Dim NewHTML As String
        Dim i As Integer
        
        Dim el As MSHTML.HTMLAnchorElement
        With Me.wb1
            .Navigate "about:blank"
            WaitFor wb1
            
        NewHTML = "<HTML>"
        NewHTML = NewHTML & "<body>"
        For i = 1 To 5
            NewHTML = NewHTML & "<a href='#' id='txtHere' onclick='return GetVal()' value='" & i & "'>" & i & "</a>"
            NewHTML = NewHTML & "<br>"
        Next i
        '<--- purpose of this script is not to reload the page
        NewHTML = NewHTML & "<script type='text/javascript'>"
        NewHTML = NewHTML & "function GetVal() {"
        NewHTML = NewHTML & "return false;"
        NewHTML = NewHTML & "}"
        NewHTML = NewHTML & "</script>"
    
        NewHTML = NewHTML & "</body>"
        NewHTML = NewHTML & "</html>"
            
            .Document.Open "text/html"
            'or you can load a page from a URL/file
            'Note: local pages need "mark of the web" in the markup
            '.Document.Write "<html><input type='text' size=10 id='txtHere'></html>"
            .Document.Write NewHTML
            .Document.Close
            WaitFor wb1
    
            Set el = .Document.getElementById("txtHere")
    
            Set o = New clsHtmlText
            o.SetText el '<< assign the textbox so we can monitor for change events
    
        End With
    
    End Sub
    
    'utility sub to ensure page is loaded and ready
    Sub WaitFor(IE)
        Do While IE.ReadyState < 4 Or IE.Busy
            DoEvents
        Loop
    End Sub

CLASS MODULE:

Option Explicit

Private WithEvents txt As MSHTML.HTMLAnchorElement  '.HTMLInputElement

Public Sub SetText(el)
    Set txt = el
End Sub

Private Function txt_onclick() As Boolean
    MsgBox "changed: " & txt.Value
End Function

it's working :) but only the first link fires the messagebox.. the rest not

4
  • See: stackoverflow.com/questions/43987268/… for one method of communicating from an embedded webbrowser back to VBA. Commented Feb 21, 2021 at 3:35
  • thank you @TimWilliams. but how can change it to Anchor tag instead of input type.. and at the same time once i click the link, it wont refresh or reload Commented Feb 21, 2021 at 3:57
  • Its now working @TimWilliams :) thank you so much.. the only issue i encountered right now is only one link fires messagebox.. the rest doesnt. Commented Feb 21, 2021 at 4:16
  • An element's id is supposed to be unique - you're only getting the first link element. I will post something in a little while. Commented Feb 21, 2021 at 4:31

1 Answer 1

3

For your userform:

'You don't need a separate class if you're working in an 
'  "object" module (a form or sheet module for example)
Private WithEvents txt As MSHTML.HTMLInputElement

'This is triggered when the 'onchange' event fires for 
'  the input in the hosted webbrowser control
Private Function txt_onchange() As Boolean
    MsgBox "Value from web page:" & txt.Value
End Function


Private Sub UserForm_Activate()
    Dim html As String
    Dim i As Integer
    
    With Me.wb1
        .Navigate "about:blank"
        WaitFor wb1
        
        html = "<html><body>"
        For i = 1 To 5
            html = html & "<a href='#' id='txtHere' " & _
                   "onclick='SendVal(this);return false;'>Value " & i & "</a><br>"
        Next i
        
        html = html & "<input type='hidden' id='txtOutput' size='10'>" 'for sending data out
        html = html & "<script type='text/javascript'>"
        'set the input value and trigger its change event
        html = html & "function SendVal(el) {var txt=document.getElementById('txtOutput');" & _
                               "txt.value = el.innerText;txt.fireEvent('onchange');}"
        html = html & "</script></body></html>"
    
        .Document.Open "text/html"
        .Document.Write html
        .Document.Close
        WaitFor wb1
        
        Set txt = .Document.getElementById("txtOutput") 'set up event capture
    End With
End Sub
    
'utility sub to ensure page is loaded and ready
Sub WaitFor(IE)
    Do While IE.ReadyState < 4 Or IE.Busy
        DoEvents
    Loop
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.