用于打印的VBA语句仅在调试模式下执行



strong text我有一个简单的单词表单要打印。我将打印机名称传递给变量opinter中的一个子例程(myprint(。变量printcomplete将传回成功打印数据的打印机的名称。

此逻辑在我处于调试模式时工作,但在不处于调试模式的情况下似乎绕过了打印命令。我尝试添加延迟,在打印声明之后添加了MSGBOX(之前打印完成=opinter>Exit Sub(。MSGBOX确实显示了,所以它应该已经执行了print语句。我没有在我的printerr下得到消息框。

知道为什么它不会在调试模式下打印出来吗?

这是我的代码:

'Print MS Word Form to one of three networked printers                
Sub MyPrint(oprinter, printcomplete)                                                  
Dim sPrinter As String            
On Error GoTo myprinterr               
Sleep (5000)
With Dialogs(wdDialogFilePrintSetup)                    
sPrinter = .Printer                     
.Printer = oprinter                    
.DoNotSetAsSysDefault = True                
.Execute                
Sleep (5000)
Application.PrintOut FileName:=""                    
.Printer = sPrinter                
.Execute                                                       
End With                                                          
Sleep (5000)
MSGBOX "Did it print? "
printcomplete = oprinter                                
Exit Sub
myprinterr:
MsgBox "oops  printer error on: " & oprinter

End Sub


在代码中,我注意到了几行代码:

CreateObject("Excel.Application").Wait (Now + TimeValue("00:00:05"))    'delay to try to get print to work when not in debug mode

不需要每次都创建一个新的Excel应用程序实例。

如果你想引入任何延迟,你可以使用睡眠窗口API功能。它将挂起当前线程的执行,直到超时间隔过去。

最新更新