簡體   English   中英

VBA / Internet Explorer / Javascript:如何從同一Internet Explorer打開JavaScript窗口?

[英]VBA / Internet Explorer / Javascript: How to open a javascript window from same internet explorer?

我正在Intranet上工作。 使用VBA,我必須關閉列表中的某些日期。

  • 我選擇了日期。 然后單擊一個鏈接以關閉(而非按鈕),從而觸發一個javascript。

選擇所有日期:

html.getElementsByName("selectall")(0).Click

單擊鏈接以關閉日期。

html.getElementsByTagName("a")(0).Click Do While ie.Busy = True Or ie.readyState <> 4: DoEvents: Loop

JavaScript的一部分

{
        //alert(xxx)
        document.first.deletepids.value = xxx
        window.open('closeallocation.asp?hotelid='+hotelid+'&roomtype='+roomtype+'&allocxid='+allocxid,'','toolbar=no,scrollbars=yes,width=500,height=330,top=50,left=80')
        //window.open('closeallocation.asp?deletepids='+xxx+'&hotelid='+hotelid+'&allocxid='+allocxid)
    }
    else
    {
        if(confirm("Room(s) already having bookings for required dates.Do you wish to continue ?"))
        {
            document.first.deletepids.value = xxx
            window.open('closeallocation.asp?hotelid='+hotelid+'&roomtype='+roomtype+'&allocxid='+allocxid,'','toolbar=no,scrollbars=yes,width=500,height=330,top=50,left=80')
        }
        else
            return false;
    }
  • 它會打開一個新窗口。

如何使用VBA在同一窗口中打開javascript窗口,或者在新窗口中打開該窗口時如何獲取元素或設置值或單擊新窗口中的元素?

我試圖使用Shell窗口來搜索所有帶有title和locationurl的打開的窗口,但是由於某種原因我沒有找到那個特定的窗口。

Dim ow As ShellWindows
Dim fw As Variant
Dim html As HTMLDocument
Dim ie As InternetExplorer

Set ie = New InternetExplorer
Set ow = New ShellWindows

    For Each fw In CreateObject("Shell.application").Windows

        'On Error GoTo ErrorMsg
        If fw = "Internet Explorer" Then
            If InStr(fw.LocationURL, "displayhotelallocsummary.asp") <> 0 Then
            Exit For
            End If
        End If
    Next fw

Set ie = fw
Set html = ie.document

請指教。

嘿,我想我有一個解決方案。

以下功能允許您激活所需的窗口,以便可以分析其HTML代碼。

  1. 執行您的代碼,然后將打開一個新網頁
  2. 調用此函數, ie = ActivateWindow(path)並將新頁面的路徑作為參數
  3. 該功能將通過計算機中所有打開的窗口,並激活包含此路徑的窗口

     Function ActivateWindow (sLocation As String) As SHDocVw.InternetExplorer Dim objShellWindows As New SHDocVw.ShellWindows Dim window As Object, Good_one As SHDocVw.InternetExplorer For Each window In objShellWindows On Error Resume Next ' We add this so that if there's an issue in the procedure to get the typeName of the window, we just skip it since it s not the window we re looking for anyway If TypeName(window.document) = "HTMLDocument" Then If UCase(window.document.Location) = UCase(sLocation) Then Set Good_one = window Exit For End If End If Next Set ActivateWindow= Good_one End Function 

1. How do I open the javascript window in the same window?

window.open('closeallocation.asp?hotelid=2&roomtype=3','_self','toolbar=no,scrollbars=yes,width=500,height=330,top=50,left=80')

_self參數指示該URL將替換當前頁面

2. Or when it opens in a new window how do get elements or set value or click on the element of the new window?

您可以通過querystring傳遞參數

var myWindow = window.open('closeallocation.asp?hotelid=2&roomtype=3','','toolbar=no,scrollbars=yes,width=500,height=330,top=50,left=80');

在上面的示例中,您傳遞了兩個參數( hotelid=2 and roomtype=3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM