html - 如何避免在每个循环上打开 IE?
问题描述
在过去几天我在这里得到了很大的帮助之后,我设法通过在 excel 中选择一个范围并遍历该范围来完成下载 xlsm 文件的代码。现在我使用了 IE 方法,它为每个条目打开一个新的 IE 实例。我怎样才能避免这种情况?我的范围内有 50 多个条目。
有没有办法不打开 IE 但仍然抓取我需要的 objID 在线数据?
Sub DownloadUpdate_Reviews()
Dim i As Range
Dim Rng As Range
Dim versch As String
Dim ordner As String
Dim dlURL As String
Dim enumm As String
Dim objID As String
Dim HTMLDoc As MSHTML.HTMLDocument
Dim ie As InternetExplorerMedium
Dim ifrm As MSHTML.HTMLDocument
Dim ifrm2 As MSHTML.HTMLDocument
Dim HttpReq As Object
'Select range
On Error Resume Next
Set Rng = Application.InputBox( _
Title:="Select Range", _
prompt:="Select cell range with the E-numbers to download", _
Type:=8)
On Error GoTo 0
If Rng Is Nothing Then Exit Sub
'Limit of allowed number of blank cells
If WorksheetFunction.CountBlank(Rng) > 10 Then
MsgBox "Too many blank cells in range.Limit is set to 10. Please dont select a whole column as range"
GoTo Toomanyblanks
End If
'Saving location
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Select where to save"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show = -1 Then
ordner = .SelectedItems(1)
End If
End With
Application.ScreenUpdating = False
Set ie = New InternetExplorerMedium
'Skip blank cells in range
For Each i In Rng
If i = "" Then
GoTo Blank_i
End If
versch = i.Offset(0, -1)
'Get the objID
enumm = i
'Set ie = New InternetExplorerMedium
ie.Visible = True
ie.navigate "https://plm.corp.int:10090/enovia/common/emxFullSearch.jsp?pageConfig=tvc:pageconfig//tvc/search/AutonomySearch.xml&tvcTable=true&showInitialResults=true&cancelLabel=Close&minRequiredChars=3&genericDelete=true&selection=multiple&txtTextSearch=" & [i] & "&targetLocation=popup"
While ie.readyState <> 4 Or ie.Busy: DoEvents: Wend
'choosing the right frame
Set HTMLDoc = ie.document
Set ifrm = HTMLDoc.frames(0).frames(1).frames(0).document
'Debug.Print HTMLDoc.frames(0).frames(1).frames(0).Name
'getting the specific object ID
objID = ifrm.getElementsByName("emxTableRowId")(0).Value
'Debug.Print objID
'start download
dlURL = "https://plm.corp.int:10090/enovia/tvc-action/downloadMultipleFiles?objectId=" & [objID] & ".xlsm"
Set HttpReq = CreateObject("Microsoft.XMLHTTP")
HttpReq.Open "GET", dlURL, False
HttpReq.send
dlURL = HttpReq.responseBody
If HttpReq.Status = 200 Then
Set oStrm = CreateObject("ADODB.Stream")
oStrm.Open
oStrm.Type = 1
oStrm.Write HttpReq.responseBody
oStrm.SaveToFile [ordner] & "\" & [i] & "_" & [versch] & ".xlsm", 2 ' 1 = no overwrite, 2 = overwrite"
oStrm.Close
End If
Blank_i:
Next
'quit InternetExplorer
ie.Quit
Set ie = Nothing
Toomanyblanks:
End Sub
以下行中出现错误:找不到成员
Set ifrm = HTMLDoc.frames(0).frames(1).frames(0).document
但如果我用 F8 手动导航代码,它就可以工作。我猜,因为它有更多的执行时间?!
解决方案
好吧,如果涉及一些 javascript 并且尚未准备好,并且需要更多时间,则可能是这样。一种解决方法是尝试
Set ifrm = HTMLDoc.frames(0).frames(1).frames(0).document
直到它起作用(并且最大时间限制为 5 秒,因此您不会陷入无限循环)。
Dim TmrStart As Single
TmrStart = Timer 'initialize timer
Set ifrm = Nothing 'absolutely necessary otherwise the old frame could stay referenced.
Do
On Error Resume Next
Set ifrm = HTMLDoc.frames(0).frames(1).frames(0).document
On Error Goto 0
Loop While TmrStart + 5 > Timer AND ifrm Is Nothing
If ifrm Is Nothing Then
Msgbox "The iframe was not found within 5 seconds. It finally failed."
End If
所以这将尝试找到 iframe,直到找到它,但最多 5 秒。如果它更早地找到它,那么它会继续。
推荐阅读
- kotlin - 在 Kotlin 中,在函数中返回双精度时我不应该使用“return”吗?
- r - R,如何使用for循环使列表中的每个元素由同一命令执行
- amazon-web-services - 将 ASG 中的 EC2 实例列入白名单以访问 AWS WAF
- pytorch - 如何解决 pytorch/pylint 错误:torch.tensor 不可调用?
- python - Django 对图像的多个 AJAX 查询
- mysql - 如何创建 100% 高度的正文、可滚动的 HTML 表格和服务器端加载的元素?
- acumatica - 如何向 Acumatica PXDataFieldParam 对象声明和添加参数?
- android - 带有适用于 Android 的口味的 Gradle applicationId
- r - 以前可以工作的代码,但现在 ggplot 空了
- julia - 使用 Julia 解决学生期末项目分配问题