簡體   English   中英

當使用VBA直接從Internet Explorer打開CSV文件時,我無法與該文件進行交互。

[英]When using VBA to open a CSV file directly from internet explorer, I cant then Interact with the file.

因此,我已經編寫了一些代碼來通過模擬按鈕單擊從Twitter下載分析數據。 這不是漂亮的代碼,但我現在可以找到所有可用的代碼。

我成功地設法單擊了文件下載,然后出現帶有打開的保存選項的“框架通知欄”。 我成功單擊兩次打開,但是這是我遇到的問題。 問題是我隨后要與剛選擇打開的CSV文件中的數據進行交互,但是直到代碼運行完畢后CSV文件才存在。 我知道必須有一個簡單的解決方案,但我只是不知道要搜索什么。

我嘗試過與Wait和DoEvents一起玩,看看是否有幫助,但到目前為止沒有運氣。 這是我的代碼:

Private Sub CommandButton1_Click()

UserForm1.Hide
Dim appIE As Object
Set appIE = CreateObject("internetexplorer.application")


With appIE
    .Navigate "https://analytics.twitter.com/user/QinetiQ/tweets"
    .Visible = True
End With
Do While appIE.Busy
    DoEvents
Loop
Application.Wait (Now + TimeValue("0:00:04"))

Set HTMLDoc = appIE.document
    Set btn = HTMLDoc.getElementsByClassName("btn btn-default ladda-button")(0)
    btn.Click
    Application.Wait (Now + TimeValue("0:00:07"))
    Application.SendKeys "%{S}"

Dim o As IUIAutomation
    Dim e As IUIAutomationElement
    Set o = New CUIAutomation
    Dim h As Long
    h = appIE.Hwnd
    h = FindWindowEx(h, 0, "Frame Notification Bar", vbNullString)
    If h = 0 Then Exit Sub
    Set e = o.ElementFromHandle(ByVal h)
    Dim iCnd As IUIAutomationCondition
    Set iCnd = o.CreatePropertyCondition(UIA_NamePropertyId, "Open")
    Dim Button As IUIAutomationElement
    Application.Wait (Now + TimeValue("0:00:03"))
    SendKeys "%(o)"
    Dim wb As Workbook
    DoEvents

    Application.Wait (Now + TimeValue("0:00:15"))
    Set wb = GetWB
    Dim ws As Worksheet
    Set ws = wb.ActiveSheet

End Sub

Function GetWB() As Workbook
    Dim wb As Workbook
 wbName = "tweet"

    For Each wb In Application.Workbooks
        If wb.Name Like wbName & "*" Then
            Set GetWB = wb
            MsgBox ("Found it")
            Exit Function
        End If
    Next wb
    MsgBox ("failed to find worksheet")
End Function

我知道我為此使用了一些非常糟糕的技巧和道歉。 請任何人可以幫助,謝謝。

您可以使用Application.OnTime將宏一分為二-這將使您完成宏(以便可以打開CSV文件),然后安排新的宏在幾秒鍾后啟動:

    'This is the end of CommandButton1_Click
    Application.Wait (Now + TimeValue("0:00:03"))
    SendKeys "%(o)"
     Dim wb As Workbook
     DoEvents

    Application.OnTime Now()+TimeSerial(0,0,15), "ContinueDownloadMacro"
    'In 15 seconds the "ContinueDownloadMacro" Sub will start
End Sub

Public Sub ContinueDownloadMacro()
    Dim wb As Workbook, ws As Worksheet

    Set wb = GetWB
    Set ws = wb.ActiveSheet

End Sub

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM