0

我从excel中的项目列表自动创建pdf。一切正常,但有时由于程序关闭原始文件太快,创建的 pdf 文件已损坏。我意识到我可以增加时间,Application.Wait但如果列表很长,有时会增加很多时间。我正在尝试使用FileLen和循环读取文件大小,直到该值大于 0,然后关闭文件。我下面的代码抛出一个错误,说Next without For

任何帮助表示赞赏。

For WaitTime = 0 To 0.5
If FileLen(SavePDFFldr & "\" & Description & ".pdf") > 10 Then
    Application.SendKeys "^(q)", True
    Application.SendKeys "{numlock}%s", True                    ' Closes the template when the file is done saving
    Application.SendKeys "{Tab}", True
    Application.SendKeys "{Enter}", True
Else: Next WaitTime

在此之前设置 SavePDFFldr 和 Description 的位置。

4

2 回答 2

1

根据评论,您可以添加一个“无限”循环

    Do While FileLen(SavePDFFldr & "\" & Description & ".pdf") < 10
        ' Dangerous this could end up in a endless loop
        ' we just loop and give other applications time
        ' to do something
        DoEvents
    Loop

 ' Hopefully you get here 
        Application.SendKeys "^(q)", True
        Application.SendKeys "{numlock}%s", True                    ' Closes the template when the file is done saving
        Application.SendKeys "{Tab}", True
        Application.SendKeys "{Enter}", True
于 2020-06-02T16:02:02.400 回答
0

评论中提出的问题可以这样回答。您向循环添加第二个退出条件,几秒钟后退出循环。

Sub LoopTest()

Dim a As Long
Dim startTime As Date
Dim endTime As Date
    startTime = Now
    endTime = startTime + TimeValue("00:00:03")
    Do While a < 10
        ' Without a second condition this is an endless loop

        ' this example will leave the loop after ~3 seconds
        If Now > endTime Then
            MsgBox "Done"
            Exit Do
        End If

        'DoEvents
    Loop
End Sub
于 2020-06-03T07:18:31.113 回答