【问题标题】:VBA Data Download From Web w/out Affecting Other Tasks?从 Web 下载 VBA 数据而不会影响其他任务?
【发布时间】:2016-02-10 02:33:11
【问题描述】:

是否可以使用 VBA Excel 从网站下载数据而不影响其他任务?我想要实现的是能够按下一个按钮并继续处理其他任务。现在,当我运行下面的代码时,我无法执行其他任务,否则代码将中断。感谢大家的帮助/意见!

Public Sub Get_File()

    Dim sFiletype As String     'Fund type reference
    Dim sFilename As String     'File name (fund type + date of download), if "" then default
    Dim sFolder As String       'Folder name (fund type), if "" then default
    Dim bReplace As Boolean     'To replace the existing file or not
    Dim sURL As String          'The URL to the location to extract information
    Dim pURL As String
    Dim Cell, Rng As Range
    Dim Sheet As Worksheet

    Dim oBrowser As InternetExplorer
    Set oBrowser = New InternetExplorer

    Dim StartTime As Double
    Dim SecondsElapsed As Double

    StartTime = Timer

    'Initialize variables
    Set Rng = Range("I2:I15")
    Set Sheet = ActiveWorkbook.Sheets("Macro_Button")

    For Each Cell In Rng
        If Cell <> "" Then
        sFiletype = Cell.Value
        sFilename = sFiletype & "_" & Format(Date, "mmddyyyy")
        sFolder = Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:Z15"), 2, False)
        bReplace = True
        sURL = "www.preqin.com"
        pURL = Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:Z15"), 16, False)

        'Download using the desired approach, XMLHTTP / IE
            If Application.WorksheetFunction.VLookup(Cell.Value, Sheet.Range("I2:Z15"), 15, False) = 1 Then
            Call Download_Use_IE(oBrowser, sURL, pURL, sFilename, sFolder, bReplace)
            Else
            Call Download_NoLogin_Use_IE(oBrowser, pURL, sFilename, sFolder, bReplace)
            End If

        Else: GoTo Exit_Sub
        End If
    Next

Exit_Sub:

    'Close IE
    oBrowser.Quit

    'Determine how many seconds code took to run
    SecondsElapsed = Round(Timer - StartTime, 2)
    MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation

End Sub

Private Sub Download_Use_IE(oBrowser As InternetExplorer, _
                            ByRef sURL As String, _
                            ByRef pURL As String, _
                            Optional ByRef sFilename As String = "", _
                            Optional ByRef sFolder As String = "", _
                            Optional ByRef bReplace As Boolean = True)

    Dim hDoc As HTMLDocument
    Dim objInputs As Object
    Dim ele As Object

    On Error GoTo ErrorHandler

    oBrowser.Visible = True

    'Navigate to URL
    Call oBrowser.navigate(sURL)
    While oBrowser.Busy Or oBrowser.readyState <> 4: DoEvents: Wend

    'Skips log in step if already signed into website
    On Error GoTo LoggedIn

    'Enter username
    oBrowser.document.getElementById("ctl00_ctl00_cphSiteHeader_ucLoginForm_user_email").Value = "XXX"
    oBrowser.document.getElementById("ctl00_ctl00_cphSiteHeader_ucLoginForm_user_password").Value = "XXX"

    'Submit the sign in
    oBrowser.document.getElementById("ctl00_ctl00_cphSiteHeader_ucLoginForm_btnLogin").Click
    'Wait for website to load
    While oBrowser.Busy Or oBrowser.readyState <> 4: DoEvents: Wend

LoggedIn:

'Initial data export
    oBrowser.navigate (pURL)
    'Wait for website to load
    While oBrowser.Busy Or oBrowser.readyState <> 4: DoEvents: Wend
    'Set the htmldocument
    Set hDoc = oBrowser.document

    'Loop and click the download file button
    Set objInputs = oBrowser.document.getElementsbyTagName("input")
    For Each ele In objInputs
        If ele.Title Like "Download Data to Excel" Then
            ele.Click
        End If
    Next

    'Wait for dialogue box to load
    While oBrowser.Busy Or oBrowser.readyState > 3: DoEvents: Wend
    Application.Wait (Now + TimeValue("0:00:02"))

    'IE 9+ requires to confirm save
    Call Download(oBrowser, sFilename, sFolder, bReplace)

Exit Sub

ErrorHandler:
    'Resume
    Debug.Print "Sub Download_Use_IE() " & Err & ": " & Error(Err)
End Sub

【问题讨论】:

    标签: vba excel internet-explorer-11


    【解决方案1】:

    试试

    DoEvents
    

    据我所知,使用后台进程和 excel 并不容易。

    干杯。

    【讨论】:

      【解决方案2】:

      您不能在运行宏的工作簿上工作。如果您想在宏运行时在 Excel 中工作,您可以打开另一个 Excel 实例,或运行宏的工作簿的只读副本。 This question has been asked and answered before on here

      很难从您的问题中确定您是在谈论 Excel 中的“其他任务”还是一般来说只是在您的计算机上。我上面的段落回答了您是否可以在宏运行时在 Excel 中执行任务。

      【讨论】:

      • 一般电脑。发生的情况是,如果我单击屏幕上的其他位置阅读文章等,则宏停止/返回错误。
      • 好的,我不确定这是否会有所帮助,但您是否尝试过在开始时关闭 screenupdating 并在结束时重新打开?
      • 不幸的是没有工作 - 我在上面提供了额外的代码以防万一......
      • 当您说“阅读文章等”时,您是指在浏览器中进行其他活动还是在屏幕上点击任何程序?
      • 任何程序 - 它可以只是滚动另一个 Excel 工作簿、浏览另一个网站、将 PPT 组放在一起等。想知道宏(从网络检索数据并将其保存到特定文件夹)可以在后台运行而不会被中断。
      猜你喜欢
      • 2019-01-20
      • 1970-01-01
      • 1970-01-01
      • 2021-09-07
      • 2018-01-26
      • 2020-11-08
      • 2021-08-15
      • 1970-01-01
      • 2021-05-07
      相关资源
      最近更新 更多