繁体   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