【问题标题】:VBA / Internet Explorer / Javascript: How to open a javascript window from same internet explorer?VBA / Internet Explorer / Javascript:如何从同一个 Internet Explorer 打开一个 javascript 窗口?
【发布时间】:2017-04-08 15:00:19
【问题描述】:

我正在使用 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

请指教。

【问题讨论】:

  • 我真的没有粗鲁的意思,但你应该认真考虑升级你所知道的关于 Web 开发的一切。这看起来很像我在 1999 年左右所做的事情,当时也感觉很糟糕。这是新软件,还是您从大约 10 到 15 年前开始制作它的人那里继承的?通过使用现代方法重做,您将为您的客户/雇主节省很多钱,并为您自己减轻很多压力。
  • 嗨安德烈斯。我一点也不生气。我完全理解你的担心。你不是第一个对我的代码有这个想法的人。但是 Javascript 是由第三方编写的,我只是想让数据变得优秀。公司不想花更多的钱聘请一名专家程序员。所以我一边学习一边工作。我很高兴我正在学习。听起来你一定是一个非常优秀的程序员。你认为这个问题有解决方案吗?

标签: javascript vba internet-explorer browser-automation childwindow


【解决方案1】:

嘿,我想我有一个适合你的解决方案。

下面的函数可以让你激活你想要的窗口,这样你就可以分析它的 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
    

【讨论】:

  • 嗨 Seb,当我用鼠标指针单击关闭链接时,它会打开一个弹出窗口。但是当我使用 vba 代码单击 Internet Explorer 弹出链接并要求允许一次弹出窗口时。如何使用 vba 代码点击链接像鼠标一样点击或选择链接并发送键输入以便我可以应用您的代码?
  • 我不确定我是否理解您的回答。当您要使用的窗口已经存在但不是活动窗口时,我发布的代码是有效的。只要它存在,即使互联网打开一个弹出窗口,它也应该可以工作
  • 这里的问题是 IE 阻止我打开弹出窗口。当我点击代码时,它正在请求许可。但是如果我用鼠标单击它会打开弹出窗口。我该如何解决?他们不是同一个问题,但一个正在阻止另一个。
  • 好的,这样做:Dim objShell As ObjectDim IE_count As IntegerSet objShell = CreateObject("Shell.Application") IE_count = objShell.Windows.CountMsgBox IE_count ' number of internet tabs and folder open (visible or not)。在单击打开弹出窗口的按钮之前输入此行并检查您获得的数字。然后放在弹窗+你感兴趣的窗口打开后告诉我你得到了哪些数字
【解决方案2】:

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?

可以通过查询字符串传递参数

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)

【讨论】:

  • 当我用鼠标点击关闭链接时,它会打开一个弹出窗口。但是当我使用 vba 代码单击 Internet Explorer 弹出链接并要求允许一次弹出窗口时。如何使用vba代码点击链接像鼠标一样点击或者选择链接然后sendkey回车?
  • 我认为你得到了弹出窗口,因为 VBA 需要在浏览器中配置特殊权限......你应该改用 javascript...... javascript 中这样的东西应该可以工作document.getElementsByTagName("a")[0].click();
  • 嗨巴斯托斯,我应该在哪里编写这段代码?在我的 vba 代码中?
  • 不,它是 javascript,VBA 不会编译它...您必须在 javascript 代码块中运行它...
  • 但我无法在 javascript 中运行它。运行 javascript 的那个网页我无权访问它。它是一个可以的软件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-17
  • 2017-05-26
相关资源
最近更新 更多