VBA:与IE中的javascript弹出窗口进行交互

时间:2019-03-15 17:18:17

标签: excel vba internet-explorer automation popup

我开始自动执行报告。它正在与我们Intranet上的网站进行交互以进行报告。一旦获得与下拉列表的交互以执行它,便会弹出一个弹出窗口,在该弹出窗口中,我需要输入预先生成的文本数据,然后单击“确定”,以便继续进行操作。

这是Intranet网页上的代码段:

                            <pre>
                            <form action="/Report/MarkJob" method="post">
                                <select name="status">
                                    <option value="">--Select One--</option>
                                    <option value="Mark Completed">Mark Completed</option>
                                    <option value="Non Voting">Non Voting</option>
                                    <option value="Foreign Client">Foreign Client</option>
                                    <option value="Dump / Purge">Dump / Purge</option>
                                    <option value="Job Closed">Job Closed</option>
                                    <option value="Test Job">Test Job</option>
                                    <option value="Unflagged for N&amp;A">Unflagged for N&amp;A</option>
                                    <option value="Email Only">Email Only</option>
                                    <option value="Canada NTC">Canada NTC</option>
                                    <option value="Bulk Shipment">Bulk Shipment</option>
                                    <option value="Not Following Day 40">Not Following Day 40</option>
                                    <option value="No Options">No Positions</option>
                                </select>
                                <input data-val="true" data-val-number="The field ID must be a number." data-val-required="The ID field     is required." id="item_ID" name="item.ID" type="hidden" value="16973962"><input id="comments"   name="comments" type="hidden" value="">
                                <input type="submit"    value="Select" onclick="javascript:return ShowMessage();">
                            </form>
                        </td>
                    </tr>
                </tbody>
            </table>
            <br>
            <br>
        </div>
    </div>
</section>
<script>
function submitdata(){
    var valtext = $("#PPNumber").val(); //you can do also by  getelementbyid
    window.location.href = '/Report/Search/' + valtext;
}
function ShowMessage() {
    var result = prompt("Please insert a comment if required.", "");
    if (result == null) {
        return false; //break out of the function early
    }
    document.getElementById('comments').value = result;
    return true;
}
</script>

和网页截图:

webpage screenshot

这是我尝试的代码:

Sub DropDayMacro()

    '
    ' DropDayMacro Macro
    '

    Dim ie As InternetExplorerMedium
    Dim A As String

    ActiveCell.Select
    Do Until ActiveCell.Value = ""
        JobNumber = Mid(ActiveCell.Value, 1, 6)
        Set ie = New InternetExplorerMedium
        ie.Visible = True
        ie.Navigate "http://10.23.16.37/Report/Search"
        Do While ie.Busy: DoEvents: Loop
        Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
        ie.document.getElementById("PPNumber").Value = JobNumber
        ie.document.parentWindow.execScript "submitdata();", "javascript"
        Do While ie.Busy: DoEvents: Loop
        Do Until ie.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop
        na = ActiveCell.Offset(0, 2).Value
        Status = ActiveCell.Offset(0, 3).Value
        If Status = "CL" Then
            A = "Job Closed"
        ElseIf na = "N" Then
            A = "Unflagged for N&A"
        End If
        ie.document.getElementsByTagName("select").Item(0).Value = A
        ie.document.parentWindow.execScript "ShowMessage();", "javascript"
        ie.document.getElementById("item_ID").Value = "AS PER PROXY PLUS NOTES"
        ActiveCell.Offset(1, 0).Select
    Loop

End Sub

1 个答案:

答案 0 :(得分:0)

从线程的描述来看,您似乎想将值分配给HTML提示符。

您可以参考下面的示例,以帮助您实现这一目标。

VBA代码:

Private Const msFILENAME As String = "C:\Users\Administrator\Desktop\66.html"

Public Sub demo()

'* Tools->References
'SHDocVw    Microsoft Internet Controls C:\Windows\SysWOW64\ieframe.dll

    Dim oIE As InternetExplorerMedium
    Set oIE = New InternetExplorerMedium

    oIE.Visible = True
    oIE.navigate msFILENAME
    While oIE.Busy Or oIE.readyState < 4
        DoEvents
    Wend

    Stop
    Call oIE.document.parentWindow.execScript("function prompt(){return 'demo_value_from_VBA'}")
    Stop
   ' oIE.Quit
End Sub

HTML代码:

<!DOCTYPE html> <html> <body>

<p>Click the button to demonstrate the prompt box.</p>

<button id="btn1" onclick="myFunction()">Try it</button>

<p id="demo"></p>

<script> function myFunction() {   var person = prompt("Please enter your name", "Harry Potter");   if (person != null) {
    document.getElementById("demo").innerHTML =
    "Hello " + person + "! How are you today?";   } } </script>

</body> </html>

输出:

enter image description here

此外,您可以根据需要尝试修改此代码示例。