jim smith
jim smith

Reputation: 2454

How to execute js function on website using Excel VBA

Trying to fill a form out (text boxes) using Excel VBA. The first problem is that the form is not recognising the values.

I tried using the below code but, same problem, it still didn't work the form throws up a custom validation 'required' error

Set inputBox = document.getElementById("company_name")
inputBox.Focus
inputBox.FireEvent ("onchange")
inputBox.Value = somevalue.Value

Inspecting the html, I see that there is a custom onchange function

<input name="company_name" tabIndex="1" id="company_name" spellcheck="false"  onselect="ControlSelect(this, event);" onfocus="ControlFocus(this, event);" onblur="ControlBlur(this, event);" onchange="ControlChange(this, event);" oncut="stdControlCut(this, event);" onpaste="stdControlPaste(this, event);" type="text" size="68" maxLength="40" value="company" autocomplete="off" control_owner="PAGE" program_id="CEN100" screen_id="sid0007" page_name="main" control_owner_id="main" defaultvalue="" extendedchars="false" wildcards="false" securitytype="" security="COM_NAME" spellchecker="false" referential="" datatype="" changecase="" paste="true" server_change_enabled="true" valid="undefined" hasChanged="false">

how do I call ControlChange(this, event); from VBA?

I think that it involves the eval function buy not sure how to pass parameters to ControlChange

Upvotes: -1

Views: 1148

Answers (1)

HeyYouCoding
HeyYouCoding

Reputation: 103

you can use ExecScript method of the ("InternetExplorer.Application") object from vba, you can customize the javascript function variable to fire the event onchange with you desired value

Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.navigate "http://www.javascripter.net/faq/confirm.htm"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.ParentWindow.execScript code:="window.confirm = function(){return true;};"
ie.Document.GetElementsByTagName("input")(0).Click

this code sets the window.confirm to true, this avoid the prompt that appears on the "onclick" method on the button from that document. (you can try clicking on that button to confirm that works)

anyway you can try too with your lines

inputBox.Focus
inputBox.Value = somevalue.Value
inputBox.FireEvent ("onchange")

first set the value on the input box, and then fire the event ("onchange"), i suggest to you add a wait loop to wait the javascript function to complete.

Upvotes: 1

Related Questions