[英]When using VBA to open a CSV file directly from internet explorer, I cant then Interact with the file.
So I have written some code to download analytic data from twitter by simulating a button click. 因此,我已经编写了一些代码来通过模拟按钮单击从Twitter下载分析数据。 It's not pretty code but its all I can find to work for now. 这不是漂亮的代码,但我现在可以找到所有可用的代码。
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. 问题是我随后要与刚选择打开的CSV文件中的数据进行交互,但是直到代码运行完毕后CSV文件才存在。 I know there must be a simple solution to this but I just don't know what to search for. 我知道必须有一个简单的解决方案,但我只是不知道要搜索什么。
I have tried to play with Wait and DoEvents to see if that helps but no luck so far. 我尝试过与Wait和DoEvents一起玩,看看是否有帮助,但到目前为止没有运气。 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. 请任何人可以帮助,谢谢。
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: 您可以使用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.