0

我正在使用Intranet。使用VBA我必須從列表中關閉某個日期。VBA/Internet Explorer/Javascript:如何從同一個Internet Explorer打開一個JavaScript窗口?

  • 我選擇了日期。然後點擊鏈接關閉(而不是按鈕),這觸發了一個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窗口,或者在新窗口中打開javascript窗口時如何獲取元素或設置值或單擊新窗口的元素?

我試圖使用shell窗口搜索所有打開的窗口與標題和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 

請指教。

+0

我真的不是故意是粗魯的,但你應該認真考慮升級你對web開發的所有知識。這看起來很像我在1999年左右所做的,而且也感覺不好。這是一款新軟件,還是您從10年前開始製作軟件的人那裏繼承?通過用現代方法重做這一點,您將爲您的客戶/僱主節省大量金錢,並且您自己承受很大的壓力。 –

+0

嗨安德烈斯。我根本沒有被冒犯。我完全理解你的關心。你不是第一個對我的代碼有這種想法的人。但是Javascript是由第三方編寫的,我只是想讓數據達到最佳效果。公司不希望花更多的錢聘用和專家程序員。所以我正在同時學習和工作。我很高興我正在學習。 聽起來像你一定是一個非常好的程序員。你認爲這個問題有解決方案嗎? – Arif

回答

0

嘿,我想我有你的解決方案。

以下功能允許您激活所需的窗口,以便分析其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 
    
+0

嗨Seb,當我用鼠標點擊CLOSE鏈接時,它會打開一個彈出窗口。但是當我使用vba代碼點擊鏈接Internet Explorer彈出窗口並要求允許一次彈出窗口。 如何使用vba代碼點擊鏈接點擊鼠標或選擇鏈接和sendkey輸入,以便我可以應用您的代碼? – Arif

+0

我不確定我是否理解你的答案。當您想要在ALREADY上工作的窗口存在時,我發佈的代碼有效,但不在活動窗口中。只要它存在,即使互聯網打開彈出窗口,它應該工作 – Seb

+0

這裏的問題是IE阻止我打開彈出窗口。當我點擊Code時,它正在請求許可。但如果我用鼠標點擊,它會打開彈出窗口。我如何解決它?他們不是同樣的問題,但一個是阻止另一個。 – Arif

0

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

0123的一個
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

+0

當我用鼠標點擊CLOSE鏈接時,它會打開一個彈出窗口。但是當我使用vba代碼點擊鏈接Internet Explorer彈出窗口並要求允許一次彈出窗口。如何使用vba代碼點擊鏈接點擊鼠標或選擇鏈接和sendkey輸入? – Arif

+0

我想你會得到彈出窗口,因爲VBA需要在瀏覽器中配置特殊權限...你應該使用javascript而不是...在JavaScript中的這樣的東西應該''document.getElementsByTagName(「a」)[0] .click();' –

+0

嗨Bastos,我應該在哪裏寫這段代碼?在我的VBA代碼? – Arif

相關問題