简体   繁体   English

VBA / Internet Explorer / Javascript:如何从同一Internet Explorer打开JavaScript窗口?

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

I am working on intranet. 我正在Intranet上工作。 Using VBA I have to close some date from a list. 使用VBA,我必须关闭列表中的某些日期。

  • I have selected the dates. 我选择了日期。 Then clicked on a link to close (not button) which triggers a javascript. 然后单击一个链接以关闭(而非按钮),从而触发一个javascript。

Selected all dates: 选择所有日期:

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

Clicked on the link to close date. 单击链接以关闭日期。

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

Part of javascript 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;
    }
  • it opens a new window. 它会打开一个新窗口。

How do I either open the javascript window in the same window using VBA or when it opens in a new window how do get elements or set value or click on the element of the new window? 如何使用VBA在同一窗口中打开javascript窗口,或者在新窗口中打开该窗口时如何获取元素或设置值或单击新窗口中的元素?

I have tried to use shell window to search for all the opened window with title and locationurl but I didnt find that particular window for some reason. 我试图使用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

Please advise. 请指教。

Hey I think I have a solution for you. 嘿,我想我有一个解决方案。

The following function allows you to activate the window you want so that you can analyze its HTML code. 以下功能允许您激活所需的窗口,以便可以分析其HTML代码。

  1. Do your code and then a new webpage will open 执行您的代码,然后将打开一个新网页
  2. Call this function ie = ActivateWindow(path) and put the path of your new page as an argument 调用此函数, ie = ActivateWindow(path)并将新页面的路径作为参数
  3. The function will go through all the open windows from your computer and activate the one containing this path 该功能将通过计算机中所有打开的窗口,并激活包含此路径的窗口

     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')

The _self parameter indicates that the URL will replace the current page _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?

You can pass parameters by querystring 您可以通过querystring传递参数

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

In the example above you're passing two parameters ( hotelid=2 and roomtype=3 ) 在上面的示例中,您传递了两个参数( hotelid=2 and roomtype=3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM