【问题标题】:How to click on open on the open/save/cancel dialog box- IE and VBA如何在打开/保存/取消对话框上单击打开-IE 和 VBA
【发布时间】:2023-12-18 14:11:02
【问题描述】:

我正在尝试从我公司的网站下载一份报告以用于报告目的。

步骤:

  1. 打开 IE
  2. 去下载链接
  3. 点击解压按钮
  4. 点击IE对话框中的打开按钮(保存/打开/取消框)
  5. 将数据复制到我的活动工作簿的工作表 1
  6. 关闭 IE

我已经完成了第 3 步。我在第 4 步遇到问题。我尝试了以下解决方案,但它们对我不起作用。

How to check if Open/Save/Cancel bar appeared

Automate saveas dialogue for IE9 (vba)

使用的代码:

Sub ExtractGLfile()

    Set ie = New InternetExplorerMedium
    Dim DLCPortalGL As String
    DLCPortalGL = "link"
    ie.Visible = True
    ie.navigate (DLCPortalGL)

    Do
        DoEvents
    Loop Until ie.readyState = READYSTATE_COMPLETE

    ie.document.getElementById("ButtonID").Click

我需要关于第 4 步的帮助 - 点击打开/保存/取消按钮的打开。

更新:我可以使用以下代码下载文件

    Application.SendKeys "%{O}", True
newHour = Hour(Now())
newMinute = Minute(Now())
newSecond = Second(Now()) + 10
waitTime = TimeSerial(newHour, newMinute, newSecond)
Application.Wait waitTime

SendKeys "{TAB}", True
SendKeys "{ENTER}", True

但是,当文件打开时,我收到错误:enter image description here 有什么建议吗?

【问题讨论】:

  • 目前无法回答您的问题。您需要包含Minimal, Complete and Verifiable Example
  • @QHarr 我感谢您的反馈。我为前面的问题道歉。谢谢
  • 您好,感谢您的回复。使用我之前描述的 sn-p 工具查看相关的 HTML 会有所帮助,还可以让您详细说明什么是不起作用的意思?您的点击正在工作并且文件已下载,但现在您无法按另存为/打开对话框?当你说在 sheet1 中打开时……你想将数据传输到 sheet1 吗?您正在下载的文档是否只有一张纸?你打算用这些数据做什么?它会去哪里?
  • 注意到了。是的,单击正在工作,我无法按活动工作簿工作表 1 中的保存/打开选项卡。是的,我想为我的报告传输工作表 1 中的数据。这些数据是我报告的输入。
  • 有很多关于 SO 的问答,用于点击 IE 的保存/打开对话框部分。看看那些。如果您可以找到与下载相关的 URL,那么下面的答案是直接下载的一种方法。然后,您可以将文件作为打开目标。

标签: excel vba ms-access internet-explorer web-scraping


【解决方案1】:

如果你想简单地下载一个页面或文件,你可以使用这个快速函数DownloadFile

Option Compare Database
Option Explicit

' API declarations.
'
Private Declare Function URLDownloadToFile Lib "Urlmon" Alias "URLDownloadToFileA" ( _
    ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) _
    As Long

' Download a file or a page with public access from the web. 
' Returns 0 if success, error code if not. 
' 
' If parameter NoOverwrite is True, no download will be attempted 
' if an existing local file exists, thus this will not be overwritten. 
' 
' Examples: 
' 
' Download a file: 
'   Url = "https://www.codeproject.com/script/Membership/ProfileImages/%7Ba82bcf77-ba9f-4ec3-bbb3-1d9ce15cae23%7D.jpg" 
'   FileName = "C:\Test\CodeProjectProfile.jpg" 
'   Result = DownloadFile(Url, FileName) 
' 
' Download a page: 
'   Url = "https://www.codeproject.com/Tips/1022704/Rounding-Values-Up-Down-By-Or-To-Significant-Figur?display=Print" 
'   FileName = "C:\Test\CodeProject1022704.html" 
'   Result = DownloadFile(Url, FileName) 
' 
' Error codes: 
' -2146697210   "file not found". 
' -2146697211   "domain not found". 
' -1            "local file could not be created." 
' 
' 2004-12-17. Gustav Brock, Cactus Data ApS, CPH. 
' 2017-05-25. Gustav Brock, Cactus Data ApS, CPH. Added check for local file. 
' 2017-06-05. Gustav Brock, Cactus Data ApS, CPH. Added option to no overwrite the local file. 
' 
Public Function DownloadFile( _ 
    ByVal Url As String, _ 
    ByVal LocalFileName As String, _ 
    Optional ByVal NoOverwrite As Boolean) _ 
    As Long 

    Const BindFDefault  As Long = 0 
    Const ErrorNone     As Long = 0 
    Const ErrorNotFound As Long = -1

    Dim Result  As Long

    If NoOverwrite = True Then 
        ' Page or file should not be overwritten. 
        ' Check that the local file exists. 
        If Dir(LocalFileName, vbNormal) <> "" Then 
            ' File exists. Don't proceed. 
            Exit Function 
        End If 
    End If     

    ' Download file or page. 
    ' Return success or error code. 
    Result = URLDownloadToFile(0, Url & vbNullChar, LocalFileName & vbNullChar, BindFDefault, 0)   

    If Result = ErrorNone Then 
        ' Page or file was retrieved. 
        ' Check that the local file exists. 
        If Dir(LocalFileName, vbNormal) = "" Then 
            Result = ErrorNotFound 
        End If 
    End If   

    DownloadFile = Result 

End Function

可以在here 找到完整的故事——以及关于缓存与否的信息。

【讨论】:

  • 我无法使用 url 下载,因为我需要点击下载按钮。我需要点击一个提取按钮。
  • 是的,但请检查按钮运行的代码。它可能只是指向 excel 文件的链接。或者在此处提供 URL 供我们使用。
  • 所以,我添加了下面的代码并且它工作了。 Application.SendKeys "%{O}", True newHour = Hour(Now()) newMinute = Minute(Now()) newSecond = Second(Now()) + 10 waitTime = TimeSerial(newHour, newMinute, newSecond) Application.Wait waitTime SendKeys "{TAB}", True SendKeys "{ENTER}", True
  • 好的。但是Access VBA中没有Application.Wait
最近更新 更多