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
idis supposed to be unique - you're only getting the first link element. I will post something in a little while.