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

私は何を間違っていますか?私の試みがうまくいかないので、あなたの助けは非常に高く評価されます - Webページがロードされた後にJSコードがトリガーされません.

ありがとうございました。

4

2 に答える 2

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 に答える
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 に答える