IE自动化与Excel VBA - iframe

时间:2017-01-25 20:15:44

标签: excel-vba ie-automation vba excel

我最近在这个论坛的帮助下取得了成功,使用Excel VBA实现了IE的自动化。然而,今天,我遇到了一个新问题,可以使用一些帮助。我试图点击一个超链接,将您带到网站上的新页面。我相信超链接(id =“page2”)包含在iframe中,但我不知道如何访问它。

以下是相关的HTML:

<frame id="workspaceFrame" name="workspaceFrame" frameborder="0" marginheight="0" marginwidth="0" noresize="" src="home.aspx">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<body leftmargin="0" rightmargin="0" topmargin="0" bottommargin="0" scroll="no">
<form id="_ctl0" name="_ctl0" method="post" action="listingframes.aspx?isContentSearch=" style="margin:0px;">
<table class="list" style="WIDTH: 100%; HEIGHT: 100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr>
<tr height="100%">
<td>
<iframe id="listingframe" name="listingframe" src="listingredirect.aspx" marginheight="0" marginwidth="0" scrolling="auto" width="100%" height="100%" frameborder="0">
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns:boe="">
<head>
<body onload="onLoad()" style="MARGIN: 0px" ms_positioning="FlowLayout">
<form id="Form1" name="Form1" method="post" action="listing.aspx?listingparentid=1541&showfolders=true">
<input name="__EVENTTARGET" value="" type="hidden">
<input name="__EVENTARGUMENT" value="" type="hidden">
<input name="__VIEWSTATE" value="" type="hidden">
<script language="javascript" type="text/javascript">
<script language="javascript">
<script language="javascript">
<input id="listingparentid" name="listingparentid" type="hidden">
<input id="search" name="search" type="hidden">
<input id="searchType" name="searchType" type="hidden">
<input id="searchText" name="searchText" type="hidden">
<input id="filterType" name="filterType" type="hidden">
<input id="advancedSearch" name="advancedSearch" type="hidden">
<input id="selectedItems" name="selectedItems" type="hidden">
<input id="command" name="command" type="hidden">
<input id="sortby" name="sortby" type="hidden">
<input id="markObj" name="markObj" type="hidden">
<script language="javascript">
<a id="MarkAsReadLinkButton" href="javascript:__doPostBack('MarkAsReadLinkButton','')" style="display:none"></a>
<a id="MarkAsUnreadLinkButton" href="javascript:__doPostBack('MarkAsUnreadLinkButton','')" style="display:none"></a>
<table width="100%" cellspacing="0" cellpadding="0" border="0">
<tbody>
<tr class="reportTitlebar">
<tr>
<td class="main" headers="tr_1 h_select" width="24px" align="center">
<td class="main" headers="tr_1 h_doctype" width="24px" align="center">
<td class="main" headers="tr_1 h_toggle" width="24px" align="center">
<td class="list"> </td>
<td id="tr_1" class="main" headers="h_title">
<a class="list" href="javascript:gotoAction("Report/report_view.aspx?id=933&reportNumber=933&reportType=CrystalEnterprise.Report&windowProperty=fullscreen=yes,location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=yes",933,true,"iv","fullscreen=yes,location=no,scrollbars=yes,menubars=no,toolbars=no,resizable=yes","");">102_RM TSM Invoiced-Sales Report with Customer.rpt</a>
</td>
<td class="list"> </td>
<td class="main" headers="tr_1 h_lastrun">Never run</td>
<td class="list"> </td>
<td class="main" headers="tr_1 h_doctype">Crystal Report</td>
<td class="list"> </td>
<td class="main" headers="tr_1 h_owner">Administrator</td>
<td class="list"> </td>
<td class="main" headers="tr_1 h_instances">0</td>
</tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
<tr>
</tbody>
</table>
<p align="right">
<a id="page1" class="listSelected" disabled="disabled" onclick="if(typeof(changePage) != 'undefined') changePage(1)">1</a>
<a id="page2" class="list" onclick="if(typeof(changePage) != 'undefined') changePage(2)" href="javascript:__doPostBack('page2','')">2</a>

相关的vba代码如下以及我到目前为止所尝试的以及相应的错误。通过反复试验,我发现我感兴趣的子框架位于:Frames(0).Frames(0).Frames(1).Frames(1)

Dim Window          As MSHTML.IHTMLWindow2
Dim button          As HTMLInputButtonElement
Dim iFrame          As HTMLIFrame
Dim ieApp           As SHDocVw.InternetExplorer
Dim ieDoc           As MSHTML.HTMLDocument
Dim button          As HTMLInputButtonElement
Dim div             As HTMLDivElement

' Create a new instance of IE
    Set ieApp = New InternetExplorer

' Uncomment this line for debugging purposes.
    ieApp.Visible = True
' Go to the page we're interested in.
    ieApp.Navigate "MyURL.com"


    Do While ieApp.Busy: DoEvents: Loop
    Do Until ieApp.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

    Set ieDoc = ieApp.Document

' Try #1
' Error: Automation error
    Set Window = ieDoc.Frames(0).Frames(0).Frames(1).Frames(1)
    Call Window.execScript("__doPostBack('page2','');", "JavaScript")

' Try #2
' Error: Object variable or With block variable not set
    Set button = ieDoc.getElementById("page2")
    button.Click

' Try #3
' Error: Automation error
    Set Window = ieDoc.Frames(0).Frames(0).Frames(1).Frames(1)("listingframe")
    Call Window.execScript("__doPostBack('page2','');", "JavaScript")

对于我可能做错了什么或其他建议的任何想法都将非常感激。

TMC

0 个答案:

没有答案