提问人:Keesler Water Walker 提问时间:3/18/2020 最后编辑:Keesler Water Walker 更新时间:3/19/2020 访问量:340
Office 2016 VBA 无法打开 Internet Explorer shell 窗口,但在 Office 2013 中工作
Office 2016 VBA fails to open Internet Explorer shell window but works in Office 2013
问:
我从我的前任那里继承了这个 VBA 脚本。它在 Excel 2013 中对我来说效果很好,直到最近我被告知我可能需要在家工作。快来了解一下,我新访问的 VPN 桌面的 Office 2016 环境不喜欢这个脚本。当它到达时,我不断收到“远程服务器计算机未知或不可用”。.ReadyState <> READYSTATE_COMPLETE
导航没有失败,因为我可以看到它成功导航到 URL 的窗口,并且我可以正确地与它交互。奇怪的是,如果我将 URL 更改为“www.google.com”,我会得到一个有效的就绪状态结果。
我还需要弄清楚如何后期绑定 Shell Windows,以便它同时与 v15 和 v16 库一起使用。
此脚本的目的是自动执行
1.通过 Web 界面
2 打开内部数据库。操作并运行位于网页
3 上的 java 脚本。关闭浏览器窗口而不关闭任何其他浏览器窗口
这可以通过查找页面元素(例如页面上的搜索框或特定按钮)并与之交互来修改以供其他人使用。
编辑:
其他测试显示,暂停和跳过循环并恢复会导致此脚本在 Office 2016 中工作。因此,唯一的问题是没有循环,当脚本尝试运行交互时,页面尚未准备好进行下一步。等待 5 秒钟代替循环创可贴的这个问题。找出无法读取的原因将解决此问题。DBurl
Do While
IETab1 = SWs.Count
.ReadyState
Declare PtrSafe Function apiShowWindow Lib "user32" Alias "ShowWindow" _
(ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Sub OpenWebDB()
Dim ieApp As Object
Dim SWs As ShellWindows
Dim IETab1 As Integer
Dim JScript As String
Dim CurrentWindow As Object
Dim DBurl As String
Dim tNow As Date, tOut As Date
DBurl = "My.Database.url"
Set SWs = New ShellWindows
tNow = Now
tOut = tNow + TimeValue("00:00:15")
If ieApp Is Nothing Then
Set ieApp = CreateObject("InternetExplorer.Application")
With ieApp
.Navigate DBurl
Do While tNow < tOut And .ReadyState <> READYSTATE_COMPLETE
DoEvents
tNow = Now
Loop
IETab1 = SWs.Count
End With
End If
If Not tNow < tOut Then GoTo DBFail
On Error GoTo DBFail
Set CurrentWindow = SWs.Item(IETab1 - 1).Document.parentWindow
JScript = "javascript: DoSomething"
Call CurrentWindow.execScript(JScript)
On Error GoTo 0
SWs.Item(IETab1 - 1).Quit
Set ieApp = Nothing
Set SWs = Nothing
Exit Sub
DBFail:
MsgBox (DBurl & vbCrLf & "took too long to connect or failed to load correctly." & vbCrLf & _
"Please notify the Database manager if this issue continues."), vbCritical, "DB Error"
SWs.Item(IETab1 - 1).Quit
Set ieApp = Nothing
Set SWs = Nothing
End Sub
答:
尝试从“执行时”条件中删除。或者,使用 While 语句等待页面完成:tNow < tOut
While IE.ReadyState <> 4
DoEvents
Wend
此脚本的目的是自动执行一个过程 1. 通过 Web 界面在 DBurl 打开一个内部数据库 2. 操作并运行位于网页上的 java 脚本 3. 关闭浏览器窗口,而不关闭任何其他浏览器窗口
此外,根据脚本的意图,我建议您可以引用以下代码(它可以遍历选项卡,并根据标题关闭特定选项卡):
Sub TestClose()
Dim IE As Object, Data As Object
Dim ticket As String
Dim my_url As String, my_title As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.Navigate "https://www.microsoft.com/en-sg/" '1st tab
.Navigate "https://www.bing.com", CLng(2048) '2nd
.Navigate "https://www.google.com", CLng(2048) '3rd
While IE.ReadyState <> 4
DoEvents
Wend
'wait some time to let page load
Application.Wait (Now + TimeValue("0:00:05"))
'get the opened windows
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
'loop through the window and find the tab
For x = 0 To (IE_count - 1)
On Error Resume Next
'get the location and title
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
'debug to check the value
Debug.Print x
Debug.Print my_title
'find the special tab based on the title.
If my_title Like "Bing" & "*" Then
Set IE = objShell.Windows(x)
IE.Quit 'call the Quit method to close the tab.
Exit For 'exit the for loop
Else
End If
Next
End With
Set IE = Nothing
End Sub
评论
ieApp
tNow < tOut
评论
Do While
IETab1 = SWs.Count