【问题标题】:Download Excel direct to drive (C:\) (No save as option require) VBA Excel将 Excel 直接下载到驱动器 (C:\) (无需另存为选项) VBA Excel
【发布时间】:2019-06-13 20:36:52
【问题描述】:

我是 VBA 新手。请帮我将excel文件直接下载到我的驱动器中。我只能选择单击元素并提示“另存为”对话框。

Application.StatusBar = "保存 - Dashboard.xlsx" 设置 InputElement = doc.querySelector("span.export[class='export excel'")

    If Not InputElement Is Nothing Then

    InputElement.Click

我每小时运行一次此代码,我无法每小时点击另存为按钮。

我正在考虑下载自动下载器,但没有取得多大成功。因为,我有管理员权限,但不符合政策。

Public Sub OpenIE_Login()

Set IE = New InternetExplorer

IE.Visible = True

IE.Navigate cURL



Do While IE.readyState <> READYSTATE_COMPLETE Or IE.Busy: DoEvents: Loop ''' ATTENTION - PAUSE HERE


    Set doc = IE.Document



    Set LoginForm = doc.forms(0)

    Set InputElement = doc.querySelector("input#userName[id='userName']")

    If Not InputElement Is Nothing Then
        InputElement.Value = cUsername
    End If

'

设置 InputElement = doc.querySelector("input.field[type='password']")

    If Not InputElement Is Nothing Then
        InputElement.Value = cPassword
    End If

Application.StatusBar = "Saving - WokingHours.xlsx"

设置 InputElement = doc.querySelector("span.export[class='export excel'") '将 TempStr 调暗为字符串 如果不是 InputElement 什么都不是,那么 'TempStr = InputElement.Value 输入元素.点击

结束子

【问题讨论】:

    标签: download internet-explorer-11


    【解决方案1】:

    您没有发布实际下载文件的代码。在帖子标题中,您说的是 VBA Excel,但您的代码使用的是 IE 对象。

    如果您有文件的 URL,则可以参考下面的示例下载文件。

    Private Declare PtrSafe 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
    Sub demo()
    
    Dim strURL As String
    Dim LocalFilePath As String
    Dim DownloadStatus As Long
    
        strURL = "http://data.iana.org/TLD/tlds-alpha-by-domain.txt"
        LocalFilePath = "C:\Users\panchals\Desktop\sample.txt"
        DownloadStatus = URLDownloadToFile(0, strURL, LocalFilePath, 0, 0)
        If DownloadStatus = 0 Then
            MsgBox "File Downloaded. Check in this path: " & LocalFilePath
        Else
            MsgBox "Download File Process Failed"
        End If
    End Sub
    

    您需要根据自己的需要修改代码。

    参考:

    Download Files with VBA URLDownloadToFile

    【讨论】:

    • 谢谢你的回答!!就像我说的,我是 VBA 的新手。代码在 excel fly 中,当它到达“InputElement.Click”弹出窗口时,会询问 1.Open、2 Save 和 3 SaveAS。是否可以直接保存到我的驱动器中?
    • 如果您在点击时有文件的 URL,那么您可以使用上面的代码将文件直接保存到您的驱动器。它不会向您显示另存为弹出窗口。我建议你直接运行示例代码来了解它。
    • 感谢您的回复,我按照您的建议做了,但没有取得太大的成功。在链接中,扩展名未定义(.xls)
    猜你喜欢
    • 1970-01-01
    • 2019-09-27
    • 2021-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    相关资源
    最近更新 更多