Search code examples
excelvbainternet-explorerautomationdispatchevent

dispatchEvent - VBA IE automation


I have a question I can't seem to figure out the answer to regarding how to dispatch an event in VBA.

On the website I am trying to navigate via VBA, there is a dynamic table id="G_grdProfile". In the table html, there are events listed. The table itself is dynamic. At minimum there will always be a row with the id="grdProfile_r_0". Within that row, there are cells that can be clicked. There can be id="grdProfile_rc_0_0", grdProfile_rc_0_1", etc.

I assume I need to dispatch the event in relation to the row or individual cell within the row, but I can't seem to get it to work. From using developer tools in Chrome and IE, it looks like either the ondblclick or onmousedown events are fired when a cell (other than 17) is clicked. And .click does not return an error, but does not do anything.

My code is:

     With ie
 While .Busy Or .ReadyState <> 4: DoEvents: Wend
 Application.Wait (Now + TimeValue("0:00:02"))
 Set objLink = .Document.frames("mainParent").Document.forms("AgentProfileList").Document.getElementById("G_grdProfile").Document.getElementById("grdProfile_r_0").Document.getElementById("grdProfile_rc_0_2")
 objLink.dispatchEvent ("onmousedown")
 End With        

The HTML referenced above for this section is:

<TABLE onmouseup='igtbl_cellClickUp(event,"grdProfile");' onmouseover='igtbl_cellMouseOver(event,"grdProfile");' ondblclick='igtbl_cellDblClick(event,"grdProfile");' onmousemove='igtbl_cellMouseMove(event,"grdProfile");' onmousedown='igtbl_cellClickDown(event,"grdProfile");' onmouseout='igtbl_cellMouseOut(event,"grdProfile");' id=G_grdProfile onselectstart='igtbl_selectStart(event,"grdProfile");' style="WIDTH: 978px; TABLE-LAYOUT: fixed; POSITION: relative" oncontextmenu='igtbl_cellContextMenu(event,"grdProfile");' cellSpacing=0 cellPadding=0 border=0 bandNo="0"><COLGROUP>

<TR id=grdProfile_r_0 style="HEIGHT: 20px" DataKey="XXXXX3658"><TD id=grdProfile_rc_0_0 class="ig_2e49b359_r1 GRSHand"><NOBR><SPAN id=fup_agentstdsearch_3765545><A onclick='javascript:xfupshow("fup_agentstdsearch_3765545");' href="javascript:void(0);"><IMG border=0 src="https://xchange.reged.com/ctiacom/images/flag000.gif"></A></SPAN>&nbsp;&nbsp;</NOBR></TD>
<TD id=grdProfile_rc_0_1 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none" iDV="False"><NOBR><INPUT tabIndex=-1 onpropertychange='igtbl_chkBoxChange(event,"grdProfile");' type=checkbox></NOBR></TD>
<TD id=grdProfile_rc_0_2 class="ig_2e49b359_r1 GRSHand"><NOBR>*LastName*</NOBR></TD>
<TD id=grdProfile_rc_0_3 class="ig_2e49b359_r1 GRSHand"><NOBR>*FIrstName*</NOBR></TD>
<TD id=grdProfile_rc_0_4 class="ig_2e49b359_r1 GRSHand"><NOBR>*MiddleName*</NOBR></TD>
<TD id=grdProfile_rc_0_5 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>3765545</NOBR></TD>
<TD id=grdProfile_rc_0_6 class="ig_2e49b359_r1 GRSHand"><NOBR>*ID*</NOBR></TD>
<TD id=grdProfile_rc_0_7 class="ig_2e49b359_r1 GRSHand"><NOBR>*ID2*</NOBR></TD>
<TD id=grdProfile_rc_0_8 class="ig_2e49b359_r1 GRSHand"><NOBR>&nbsp;</NOBR></TD>
<TD id=grdProfile_rc_0_9 class="ig_2e49b359_r1 GRSHand"><NOBR>*ID3*</NOBR></TD>
<TD id=grdProfile_rc_0_10 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>*ID2*</NOBR></TD>
<TD id=grdProfile_rc_0_11 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>*Location*</NOBR></TD>
<TD id=grdProfile_rc_0_12 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>PROD</NOBR></TD>
<TD id=grdProfile_rc_0_13 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>0</NOBR></TD>
<TD id=grdProfile_rc_0_14 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>7040</NOBR></TD>
<TD id=grdProfile_rc_0_15 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>&nbsp;</NOBR></TD>
<TD id=grdProfile_rc_0_16 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>&nbsp;</NOBR></TD>
<TD id=grdProfile_rc_0_17 class="ig_2e49b359_r1 GRSHand"><NOBR><A onclick="viewProfile('3765545','*LastName* *FirstName*');" href="javascript:void(0);">View Profile Summary</A></NOBR></TD>
<TD id=grdProfile_rc_0_18 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>&nbsp;</NOBR></TD>
<TD id=grdProfile_rc_0_19 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>&nbsp;</NOBR></TD>
<TD id=grdProfile_rc_0_20 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>&nbsp;</NOBR></TD>
<TD id=grdProfile_rc_0_21 class="ig_2e49b359_r1 GRSHand" style="DISPLAY: none"><NOBR>&nbsp;</NOBR></TD></TR>

Any ideas, or am I even using dispatchEvent properly?


Solution

  • I figured out what needed to occur was a few mouse events had to fire to move forward.

    I ended up setting the cell in the table that I wanted to click and used fireEvent to fire all of the onmouse* events that had to happen.

         Set objLink = IE1.document.frames("mainParent").document.forms("AgentProfileList").document.getElementById("grdProfile_r_0").document.getElementById("grdProfile_rc_0_2")
     objLink.fireEvent ("onmouseover")
     objLink.fireEvent ("onmousedown")
     objLink.fireEvent ("onmouseup")