我已经从代码板上删除了将密钥发送到IE11保存/打开框的代码,当您导出文件但该文件不起作用(正在发送到主浏览器)时,该框会弹出。即使手动尝试(使用ALT + S),我也无法激活“保存/打开”框并发送S按钮。
您是否需要一些设置才能将键发送到此弹出窗口?
我在下面放置了代码的精简版
谢谢
Public Declare PtrSafe Function SetForegroundWindow Lib "user32" (ByVal HWND As LongPtr) As LongPtr
Sub OpenIE()
Dim objIE As InternetExplorer
Set objIE = New InternetExplorer
Dim HWNDSrc As LongPtr
HWNDSrc = objIE.HWND
SetForegroundWindow HWNDSrc
'From https://stackoverflow.com/questions/56893185/controlling-ie11-do-you-want-to-open-save-vba
Do While objIE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
'send Alt-S to save
Application.SendKeys "%{S}"
'Make sure IE is not busy
Do While objIE.Busy
Application.Wait DateAdd("s", 1, Now)
Loop
请参考以下示例代码,我们可以先使用getElementbyId方法找到下载按钮,然后单击它以显示下载提示,然后,我们可以使用Application.SendKeys "%{s}"
命令单击“保存”按钮。
Sub downloadfile()
Dim IE As Object, Data As Object
Dim ticket As String
Set IE = CreateObject("InternetExplorer.Application")
With IE
.Visible = True
.navigate ("https://dillion132.github.io/default.html")
While IE.ReadyState <> 4
DoEvents
Wend
'Trigger the download button to download the file
IE.Document.getElementbyId("btnDowloadReport").Click
'wait the download prompt appear
Application.Wait (Now + TimeValue("00:00:03"))
'
Application.SendKeys "%{s}"
'Waiting for the site to load.
'loadingSite
End With
Set IE = Nothing
End Sub
网页内容:
<a id="btnDowloadReport" href="https://research.google.com/pubs/archive/44678.pdf" download>Download</a>