当使用VBA直接从Internet Explorer打开CSV文件时,我无法与该文件进行交互. [英] When using VBA to open a CSV file directly from internet explorer, I cant then Interact with the file.

查看:45
本文介绍了当使用VBA直接从Internet Explorer打开CSV文件时,我无法与该文件进行交互.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我已经编写了一些代码,通过模拟按钮单击来从Twitter下载分析数据.这不是漂亮的代码,但我现在能找到所有可用的代码.

So I have written some code to download analytic data from twitter by simulating a button click. It's not pretty code but its all I can find to work for now.

我已成功设法单击文件下载,此后出现带有打开的保存选项的框架通知栏".我成功单击了两次打开",但是这是我遇到的问题.问题是我随后要与刚选择打开的CSV文件中的数据进行交互,但是直到代码运行完毕后CSV文件才存在.我知道必须有一个简单的解决方案,但我只是不知道要搜索什么.

I am successfully managing to click the file download, after which a 'frame notification bar' appears with the open, save options. I am successfully clicking open twice, however this is where I run into problems. The problem being that I then want to interact with the data in the CSV file which I have just chosen to open, however the CSV file doesn't come into existence until after the code finishes running. I know there must be a simple solution to this but I just don't know what to search for.

我试图与Wait和DoEvents一起玩,看看是否有帮助,但到目前为止没有运气.这是我的代码:

I have tried to play with Wait and DoEvents to see if that helps but no luck so far. Here is my code:

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

我知道我为此使用了一些非常糟糕的技巧和道歉.谁能帮忙,谢谢.

I know I have used some really bad techniques and apologies for that. Please can anyone help, thankyou.

推荐答案

您可以使用 Application.OnTime 将宏一分为二-这将使您完成宏(以便CSV文件可以打开),然后安排新的宏在几秒钟后启动:

You can split the macro in two by using Application.OnTime - this will let you finish the macro (so that the CSV file can open) and then have a new macro scheduled to start a couple of seconds later:

    '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

这篇关于当使用VBA直接从Internet Explorer打开CSV文件时,我无法与该文件进行交互.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆