我尝试使用以下VBA代码打开网站:
Dim IE As Object
Dim doc As Object
Dim strURL As String
Set IE = CreateObject("InternetExplorer.Application")
With IE '
.Visible = True
.navigate "https://Google.com"
Do Until .readyState = 4
DoEvents
Loop
它适用于“ google”等网站。
但是当我尝试打开公司PLM之类的特定网站“ Agile(https://agileplm.XXXX.com/Agile/default/login-cms.jsp)”时抛出错误 “远程服务器计算机不存在或不可用”
我可以在资源管理器上打开网页,但是从下面一行执行时抛出错误
Do Until .readyState = 4
DoEvents
Loop
这是出于对站点的任何保护吗?
答案 0 :(得分:2)
我对此进行了早期绑定,并创建了对象InternetExplorerMedium而不是InternetExplorer,它似乎可以正常工作。
答案 1 :(得分:0)
尝试使用下面的示例代码进行测试可能会帮助您解决问题。
Sub Automate_IE_Load_Page()
'This will load a webpage in IE
Dim i As Long
Dim URL As String
Dim IE As Object
Dim objElement As Object
Dim objCollection As Object
'Create InternetExplorer Object
Set IE = CreateObject("InternetExplorer.Application")
'Set IE.Visible = True to make IE visible, or False for IE to run in the background
IE.Visible = True
'Define URL
URL = "https://agileplm.xxxx.com/Agile/default/login-cms.jsp"
'Navigate to URL
IE.Navigate URL
' Statusbar let's user know website is loading
Application.StatusBar = URL & " is loading. Please wait..."
' Wait while IE loading...
'IE ReadyState = 4 signifies the webpage has loaded (the first loop is set to avoid inadvertently skipping over the second loop)
Do While IE.ReadyState = 4: DoEvents: Loop 'Do While
Do Until IE.ReadyState = 4: DoEvents: Loop 'Do Until
'Webpage Loaded
Application.StatusBar = URL & " Loaded"
'Unload IE
Set IE = Nothing
Set objElement = Nothing
Set objCollection = Nothing
End Sub
参考:
Automate Internet Explorer (IE) Using VBA
如果问题仍然存在,则尝试提供详细信息,无论是在打开页面时遇到问题还是在检查IE的就绪状态时遇到错误。我们将尝试提供进一步的建议。
答案 2 :(得分:0)
问题是 Internet Explorer 与 VBA 断开连接(在我的情况下是一些内部安全设置)。解决方案是将 Internet Explorer 重新连接到 VBA:
在 IE 不再响应行之后包含此代码,现有的 Internet Explorer 窗口将被分配给 = IEObject1
Dim shellWins As SHDocVw.ShellWindows
Dim explorer As SHDocVw.InternetExplorer
Set shellWins = New SHDocVw.ShellWindows
For Each explorer In shellWins
If explorer.Name = "Internet Explorer" Then
Set IEObject1 = explorer
Debug.Print explorer.LocationURL
Debug.Print explorer.LocationName
End If
Next
Set shellWins = Nothing
Set explorer = Nothing
如果您打开了多个 IE 窗口,则此代码会选择最后一个。如果您需要多个打开的窗口,您可以使用 URL 或 LocationName 在窗口之间进行选择。