簡體   English   中英

VBA Excel 在一個 IE 窗口中打開多個網站

[英]VBA Excel open multitude websites in the one IE window

我設法使用 VBA Excel 打開 Internet Explorer。

我的代碼如下所示:

 Sub IE()
 Dim ie As object
 Dim location
 'Dim button
 Set ie = CreateObject("InternetExplorer.Application")
 With ie
     .Visible = True
     .Navigate ("https://fulcrum/NewBuild/Record.aspx?ID=" & Range("B1").Value)
     .Navigate ("http://gcommswebmapgb/portal/")
     .Top = 5
     .Left = 5
     .Height = 1300
     .Width = 1900
     While ie.ReadyState <>4
         Do Events
     Wend
     Set location = .document.getElementById("__VIEWSTATE")
     'Set button = .document.getElementById("btnContainer").Children(0)
     'button.Click
     While ie.ReadyState <> 4
         DoEvents
     Wend
  End With
  Set ie = Nothing   

 End Sub

在這種情況下,第二個鏈接被打開,第一個完全省略。 最重要的是,我想在同一個窗口中打開它們(作為不同的選項卡)。

我在這里找到了一些解決方案:

Excel VBA 控件 IE

VBA Excel將數據輸入到已經打開的ie窗口中

看起來需要一些功能的地方。 有人可以幫忙嗎?

我建議您將兩個 URL 存儲在如下變量中。

 Dim url1, url2 As String
 url1 = "https://fulcrum/NewBuild/Record.aspx?ID=" & Range("B1").Value
 url2 = "http://gcommswebmapgb/portal/"

然后嘗試將 URL 變量傳遞給.Navigate沒有圓括號,如下所示。

 With ie
     .Visible = True
     .Navigate url1
     .Navigate url2, CLng(2048)

它將修復您的語法錯誤,網址將在 2 個選項卡中打開。

修改后的代碼:

 Sub ie()
 Dim ie As Object
 Dim location
 'Dim button
 Dim url1, url2 As String
 url1 = "https://fulcrum/NewBuild/Record.aspx?ID=" & Range("B1").Value
 url2 = "http://gcommswebmapgb/portal/"
 Set ie = CreateObject("InternetExplorer.Application")
 With ie
     .Visible = True
     .Navigate url1
     .Navigate url2, CLng(2048)
    ' .Top = 5
    ' .Left = 5
    ' .Height = 1300
    ' .Width = 1900
     While ie.ReadyState <> 4
         'Do Events
     Wend
     'Set location = .document.getElementById("__VIEWSTATE")
     'Set button = .document.getElementById("btnContainer").Children(0)
     'button.Click
     While ie.ReadyState <> 4
         DoEvents
     Wend
  End With
  Set ie = Nothing

 End Sub

輸出:

在此處輸入圖片說明

此外,您需要檢查您的代碼是否正確引用了正確的選項卡以執行更多代碼。 請注意,您不能同時自動化兩個頁面。 您需要使用 VBA 代碼切換選項卡以在特定頁面上執行代碼。

暫無
暫無

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

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