我已经编写了一个程序,用于打印多个.pdf
,其中包含Excel电子表格列表中不同的文件扩展名。
问题是打印机在线后需要30秒到几分钟才能收到pdf:Application.SendKeys "^p~", False
被调用。
为了达到这个目的,我使用了Application.Wait (Now + TimeValue("0:01:03"))
在关闭文件之前等待一分钟(加上3秒以保证安全)。 对我来说,似乎应该有一个更好的方式,而不仅仅是让程序等待,所以我环视了一下,并且found a question about this lovely gem被称为Application.OnTime
。允许用户在VBA中等待期间终止进程
我试过的one of the answers样本:
Sub test2()
ActiveSheet.Cells(1, 1).Value = ActiveSheet.Cells(1, 1).Value + 1
Application.OnTime Now + TimeValue("00:00:5"), "test2"
End Sub
然而,当我试图阻止上面的代码保存在一个无限循环会和我无法停止它,直到我使用Windows任务管理器杀的excel 。
我希望能够添加一个小消息框或类似的东西,以便用户可以在等待时间之间点击。
因此,当程序等待一分钟时,用户可以手动点击并在下一个pdf上启动程序,或者如果需要提早停止打印,则可以单击另一个按钮退出。事情是这样的:
Sub pdfPrinter()
'...
'Insert all the other code here
'...
Application.SendKeys "^p~", False
Application.OnTime Now + TimeValue("00:01:02"), "pdfPrinter"
continue= MsgBox("Click Retry to print again, or cancel to stop printer.", vbRetryCancel)
If continue = vbRetry Then
Call pdfPrinter
ElseIf continue = vbCancel Then
Exit Sub
End If
End Sub
我想你可能要[模拟多线程(http://www.excelhero.com/blog/2010/05/multi-threaded-vba.html) – 2013-06-20 12:53:00
@mehow:应用。 OnTime确实是异步的,并在主程序的不同主题中运行。可悲的是,在VBA/VB6中实现多线程非常不稳定,需要直接控制windows API。 –