我的宏正在从Internet应用程序生成excel 2003格式的报告。然后我想读取该文件并从数据中生成指标。当我的Excel文件从IE应用程序生成时,我的宏执行超出它可以读取IE生成的excel文件。我想暂停我的宏,直到IE生成的excel文件完成。
我已经尝试过Application.Wait命令,但它会在等待结束之前停止生成文件。
有人能帮助我吗?
答案 0 :(得分:0)
如果您发布了代码,那将会很有帮助,因此我们可以看到您正在尝试的内容。但与此同时,这里是我几年前写的一些代码,但应该适用于你想做的事情。
'This block holds the program until the output file is ready
OutFile = FilePath & "\" & yftTableFile
i = 0
Set fo = CreateObject("Scripting.FileSystemObject")
Do Until fo.FileExists(OutFile) 'Loop until the output file is created, this could be infinity if there is a problem
Application.Wait (Now + TimeValue("0:00:02")) 'Holds the program for 2 seconds
DoEvents
i = i + 1
If (i = 30) Then
MsgBox ("No output file created within 60 seconds")
Exit Sub
End If
Loop
Application.Wait (Now + TimeValue("0:00:03")) 'Holds the program for 2 seconds to ensure the file is complete
请务必根据需要调整等待时间和计数器。
如果这不起作用并且你知道文件的结尾是什么样的,你可以使用这样的代码循环到文件的末尾几次:
file = FilePath & "\" & FileName 'Path to the initial temps file
fn = FreeFile
Open file For Input As fn
Do Until EOF(fn) 'Reading in the temperatures from the file
Line Input #fn, InpLine
IntTemp = Right(InpLine, 9)
If (IntTemp <> "") Then
OldTemps(i) = CDbl(IntTemp)
End If
Loop
Close fn
答案 1 :(得分:0)
这是一个等待文件的小例程:
C:\TestFolder\Newfile.txt
要创建。
例程还允许用户通过在单元格中设置值 A1
来中止等待Sub WaitABit()
FileIsThere = ""
While FileIsThere = ""
FileIsThere = Dir("C:\TestFolder\Newfile.txt")
DoEvents
If Range("A1") <> "" Then GoTo GiveUp
Wend
GiveUp:
If FileIsThere = "" Then
MsgBox "Wait aborted"
Else
MsgBox "New file is ready"
End If
End Sub