Click a button or execute a JavaScript function using VBA

I am trying to get my VBA program to interact with a page using JavaScript and have been successful in the past. After a recent update, it no longer works. At this point, I need to either A) press the button programmatically, or B) execute the function that the button calls. The hard part is that the button does not have a declared name. There are others on the sheet indicated in the source code with the names, and I can interact perfectly with them. Here's the html code from the page source:

<input type="button" class="buttonForm" value='Run Report' onclick="exportData(workOrderSearchForm)"/> 

As you can see, it does not declare the name of the button. In the past, it worked:

 Set ie = CreateObject("InternetExplorer.application") ie.document.all(79).Click 

With "79" being the position number index. Now it seems that the button has been changed to "81", but just inserted:

 ie.document.all(81).Click 

not working for some reason. I know what function I want to execute: exportData (workOrderSearchForm), but I don’t know how to do this outside of the "click" method.

I was looking for decent documentation regarding an IE application object, but cannot find a good source. Is there a way to execute a function?

+3
javascript vba excel-vba excel
source share
5 answers

Try the following:

 Dim CurrentWindow As HTMLWindowProxy: Set CurrentWindow = ie.Document.parentWindow Call CurrentWindow.execScript("exportData(workOrderSearchForm)") 

But first, you will need to enable the Microsoft HTML object library in the Links section (Tools> Links)

+10
source share

You can scroll through all input tags and identify the corresponding one by checking the "most identifying" attribute (id, name, type, innerHTML, ... whatever). Here's the Sub () that I use in an Excel worksheet, which automatically registers on the website

 Sub FormAction(Doc As MSHTML.HTMLDocument, ByVal Tag As String, ByVal Attrib As String, ByVal Match As String, ByVal Action As String) Dim ECol As MSHTML.IHTMLElementCollection Dim IFld As MSHTML.IHTMLElement Dim Tmp As String Set ECol = Doc.getElementsByTagName(Tag) For Each IFld In ECol ' cycle thru all <[tag]> elements If VarType(IFld.getAttribute(Attrib)) <> vbNull Then ' does it contain the attribute If Left(IFld.getAttribute(Attrib), Len(Match)) = Match Then If Action = "/C/" Then IFld.Click Else IFld.setAttribute "value", Action End If Exit Sub End If End If Next End Sub 

The function takes the following parameters:

  • Doc .... HTML DOM Object
  • Tag .... tag you want to work on (usually input, image, a, ...)
  • Attrib .... the attribute whose value you want to map
  • Match .... matching attribute value
  • Action ..... if the "/ C /" action .Click () is executed on the matching tag, otherwise the value of the action is placed in the value attribute of the tag

If you want to include any attribute containing JavaScript code (for example, "onclick"), do not forget that the code that you see in the HTML source is embedded in an anonymous function, for example

 function anonymous() { onclick="exportData(workOrderSearchForm)"; } 

You need to consider this with the Instr () function or similar if you want to connect, for example. "exportData (workOrder".

Also very important: give enough time to go to the page and for the DOM object to be loaded. I note in my company that after changing pages, loading times sometimes increase dramatically.

I have a good success:

 Sub MyMainSub() Dim Browser As SHDocVw.InternetExplorer Dim HTMLDoc As MSHTML.HTMLDocument ' my other Dim's ' start browser Set Browser = New SHDocVw.InternetExplorer Browser.navigate "http://www.whatever.com" WaitForBrowser Browser, 5 ' wait for browser, but not more than 5 sec ' gain control over DOM object Set HTMLDoc = Browser.document WaitForBrowser Browser, 5 ' wait for browser, but not more than 5 sec ' do actions ' FormAction HTMLDoc, w, x, y, z End Sub Sub WaitForBrowser(Browser As SHDocVw.InternetExplorer, Optional TimeOut As Single = 10) Dim MyTime As Single MyTime = Timer ' seconds since midnight Do While Browser.Busy Or (Timer <= MyTime + TimeOut) DoEvents ' go do something else Loop If Browser.Busy Then MsgBox "I waited for " & Timer - MyTime & " seconds, but browser still busy" & vbCrLf & _ "exititing Login sequence now" End End If End Sub 

I hope this inspires you to create your solution.

Good luck MikeD

+1
source share

Like this:

 ie.window.exportData(ie.document.forms.workOrderSearchForm) 
0
source share

Ten similar situations, no change, even if you click the mouse, sino en unchange. ¿Alguna sugerencia?

0
source share

I have a similar situation, but there is no action or event in the click event, the action is in the onchange event. Anyone have any suggestions for me?

0
source share

All Articles