【问题标题】:how can i pause a macro wile IE generated file is open如何在 IE 生成的文件打开时暂停宏
【发布时间】:2014-05-29 22:16:58
【问题描述】:

我的宏正在从 Internet 应用程序生成 excel 2003 格式的报告。然后我想读取该文件并从数据中生成指标。当我从 IE 应用程序生成的 excel 文件时,我的宏执行超出了它可以读取 IE 生成的 excel 文件的范围。我想暂停我的宏,直到 IE 生成的 excel 文件完成。

我尝试了 Application.Wait 命令,但它会停止生成文件,直到等待结束。

有人可以帮助我吗?

【问题讨论】:

  • 发布您的实际代码。

标签: excel vba


【解决方案1】:

如果您发布代码会很有帮助,这样我们就可以看到您正在尝试什么。但与此同时,这是我多年前编写的一些代码,但应该可以满足您的需求。

    '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

【讨论】:

  • 这个例子的问题是文件没有保存到任何地方。当 IE 生成文件时,我会得到一个新对话框,无论我是要保存还是打开文件。我得到另一个对话框“您尝试打开的文件与文件扩展名不同。...在打开文件之前受信任的来源。您要打开文件吗?”带有是、否和取消按钮。当我单击是时,它会打开文件。我想暂停我的宏,直到我在此对话框中单击“是”。我的宏没有暂停。如果我添加 Application.Wait,那么在计时器结束之前,我不会得到上面提到的两个对话框。
【解决方案2】:

这是一个等待文件的小程序:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-25
    • 2015-11-07
    • 1970-01-01
    相关资源
    最近更新 更多