0

我正在编写一个脚本,该脚本将通过 Outlook Web Access 中的每封电子邮件。消息存储在表格中,可通过单击消息标题访问(如下所示)。

<table><tr><td nowrap class="frst">carrie.stevens@u...&nbsp;</td>
<td nowrap class="sc frst"><h1><a href="#" onClick="onClkRdMsg(this, 'IPM.Note', 0, 0);">Message 1</a></h1>&nbsp;</td><td nowrap class="frst">1/11/2011&nbsp;12:16 PM&nbsp;</td></tr>
<tr><td nowrap>Doodle&nbsp;</td><td nowrap class="sc"><h1><a href="#" onClick="onClkRdMsg(this, 'IPM.Note', 1, 0);">Message 2</a></h1>&nbsp;</td><td nowrap>1/11/2011&nbsp;8:29 AM&nbsp;</td></tr>`
</table>

问题是有一段java脚本用来打开每条消息

onClkRdMsg(this, 'IPM.Note', 0, 0)
onClkRdMsg(this, 'IPM.Note', 1, 0)

我尝试的是:

Dim HTMLDoc As HTMLDocument
Dim oBrowser As InternetExplorer
Sub Login_2_Website()

'The code requires references to the following:
'1 Microsoft Internet Controls
'2. Microsoft HTML Object Library

Dim oHTML_Element As IHTMLElement
On Error GoTo Err_Clear
Set oBrowser = New InternetExplorer

URL_fold = "mywebpage.com"
oBrowser.navigate URL_fold
oBrowser.Visible = True
    Do
    ' Wait till the Browser is loaded
    Loop Until oBrowser.readyState = READYSTATE_COMPLETE
    HTMLDoc.parentWindow.execScript "onClkRdMsg(this, 'IPM.Note', 1, 0)"

Err_Clear:
If Err <> 0 Then
    'Debug.Assert Err = 0
    Err = 0
Err.Clear
Resume Next
End If

End Sub

但它不起作用。也没有:

For Each oHTML_Element In HTMLDoc.getElementsByTagName("h1")
        oHTML_Element.Click
Next

我究竟做错了什么?非常感谢您的帮助,因为我的尝试不起作用 - 加载网页后不会触发 JS 代码。

谢谢你。

4

2 回答 2

0

下面的代码不能正常工作 - 一旦我找出正确的答案,我会更新:

Private Const URL As String = "C:/HTMLPage1.html"
Sub test()
        Dim Browser As InternetExplorer
        Dim Document As HTMLDocument

        Set Browser = New InternetExplorer
        Browser.Visible = True
        Browser.navigate URL

        Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
            DoEvents
        Loop

        Set Document = Browser.Document
        Document.parentWindow.execScript "onClkRdMsg(this, 'IPM.Note', 0, 0)"

Quit:
        Browser.Quit
        Set Document = Nothing
        Set Browser = Nothing
    End Sub

谢谢丹尼尔!

于 2013-07-20T20:10:19.780 回答
0

以下代码适用于我以编程方式单击“消息 1”。 测试时要小心启用脚本执行。

Private Const URL As String = "C:/Temp/HTML/HTMLPage1.html"
Sub test()
        Dim Browser As InternetExplorer
        Dim Document As HTMLDocument

        Set Browser = New InternetExplorer
        Browser.Visible = True
        Browser.navigate URL

        Do While Browser.Busy And Not Browser.readyState = READYSTATE_COMPLETE
            DoEvents
        Loop

        Set Document = Browser.Document

        Dim tds As IHTMLElementCollection
        Dim anchors As IHTMLElementCollection
        Dim tdElement As IHTMLElement
        Dim anchorElement As IHTMLElement

        Set tds = Document.getElementsByClassName("sc")
        For Each tdElement In tds
            Set anchors = tdElement.getElementsByTagName("a")
            For Each anchorElement In anchors
                If (anchorElement.innerText = "Message 1") Then
                    anchorElement.Click
                    GoTo Quit:
                End If
            Next anchorElement
        Next tdElement

        Quit:
        Browser.Quit
        Set Document = Nothing
        Set Browser = Nothing
    End Sub

HTMLPage1.html:

<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />
    <title></title>
    <script>
        function onClkRdMsg(a, b, c, d) {
            alert('Clicked: [onClkRdMsg]...');
        }
    </script>
</head>
<body>
    <table>
        <tr>
            <td nowrap class="frst">carrie.stevens@u...&nbsp;</td>
            <td nowrap class="sc frst">
                <h1>
                    <a href="#" onclick="onClkRdMsg(this, 'IPM.Note', 0, 0);">Message 1</a>
                </h1>
                &nbsp;
            </td>
            <td nowrap class="frst">1/11/2011&nbsp;12:16 PM&nbsp;</td>
        </tr>
        <tr>
            <td nowrap>Doodle&nbsp;</td>
            <td nowrap class="sc">
                <h1>
                    <a href="#" onclick="onClkRdMsg(this, 'IPM.Note', 1, 0);">Message 2</a>
                </h1>
                &nbsp;
            </td>
            <td nowrap>1/11/2011&nbsp;8:29 AM&nbsp;</td>
        </tr>
    </table>
</body>
</html>
于 2013-07-20T19:08:13.670 回答