【发布时间】:2020-07-07 02:37:41
【问题描述】:
我正在尝试为 excel 创建一个 Web 查询,但遇到了一些障碍。我想出了如何登录网页并单击链接以转到正确的 portalID。但是,我无法获取特定报告。
要访问特定报告,我需要单击表格中的链接,但它似乎使用了 javascript。该表的 HTML 如下。此表中大约有 10 个单元格,但为了保持代码简洁,我只包括了前 2 个单元格。如您所见,每个单元格都有自己唯一的 ID,因此我可以使用 VBA 轻松过滤到正确的单元格元素。但是我似乎无法弄清楚如何实际单击单元格。
<table submenu='1' border='0' cellpadding='6' cellspacing='0'
id='ctl02n4c73594f1bf740b982340da2a792ff62_MainM'
class="ctl02n4c73594f1bf740b982340da2a792ff62Island"
style=' background:#141311; border-width:0px; width:100%;
cursor:Default;' onselectstart="javascript:igmenu_selectStart();"
onmouseover="javascript:igmenu_mouseover(this, event);"
onmouseout="javascript:igmenu_mouseout(this, event);"
onmousedown="javascript:igmenu_mousedown(this, event);"
onmouseup="javascript:igmenu_mouseup(this, event);"igLevel='0'>
<tr>
<td align='center' id='ctl02n4c73594f1bf740b982340da2a792ff62_1'
igTag='4eddf201-f6ed-4e11-b2ff-6a742128909c'
class="n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Class"style="
white-space: nowrap; ;" igHov='n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Hover'
igTop='1'>Welcome</td>
<td align='center' id='ctl02n4c73594f1bf740b982340da2a792ff62_2'
igTag='e0d4474e-87f8-42cc-ab47-38751029052d'
class="n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Class"style="
white-space: nowrap; ;" igHov='n_4c73594f_1bf7_40b9_8234_0da2a792ff62_Hover'
igTop='1'>Dashboard</td>
</tr>
</table>
这是我的 vba,用于指向我需要单击的单元格。看起来我需要调用表的“onmouseup”事件。如何将我需要的单元格与表格的 onmouseup 事件“链接”?
Sub clickTable(toFind As String, ie As Object)
'"toFind" is the ID of the cell to find, ie is the IE application
Dim ieDoc As Object 'ieDocDocument
Dim tdCollection As Object 'table that has the javascript events and contains
the cells I want to click
Dim cell As Object 'specific "clickable" cell in the table to "click"
Set ieDoc = ie.document
Set tdCollection = ieDoc.getelementbyID("ctl02n4c73594f1bf740b982340da2a792ff62_MainM").getElementsByTagName("td")
For Each cell In tdCollection
If cell.ID = toFind Then
cell.Click
Exit Sub
End If
Next
End Sub
【问题讨论】:
标签: javascript excel vba click