循环时VBA打印外部文件无法正常工作

时间:2016-06-23 03:18:01

标签: vba shell printing

得到了这个很棒的VBA 1行命令来打印外部文件而不打开它。

CreateObject("Shell.Application").Namespace(0).ParseName("F:\testprint.pdf").InvokeVerb ("Print")

完美的作品!现在,我试图插入循环以按特定顺序打印一堆文件,它打印第一个并停止,没有给出错误,但宏似乎卡住了一段时间。

我的代码:

Sub PrintFile()

    For r = 1 To 3

        FileToPrint = Sheet7.Cells(1 + r, 5).Value

        If FileToPrint = "" Then
            GoTo MainLoop
        Else
            Debug.Print r; " - "; FileToPrint
            CreateObject("Shell.Application").Namespace(0).ParseName(FileToPrint).InvokeVerb ("Print")
            Debug.Print r; " --- "; FileToPrint
        End If
        MainLoop:
    Next r

End Sub

有任何建议,想法吗?

由于

1 个答案:

答案 0 :(得分:0)

您需要在开始另一个打印作业之前完成第一个打印作业。

#If VBA7 Then
    Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
    Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If

Sub PrintFile()
    Dim objShell, WshShell
    Set objShell = CreateObject("Shell.Application")


    For r = 1 To 3

        FileToPrint = Sheet7.Cells(1 + r, 5).Value

        If FileToPrint = "" Then
            GoTo MainLoop
        Else
            Debug.Print r; " - "; FileToPrint
            objShell.Namespace(0).ParseName(FileToPrint).InvokeVerb ("Print")
            Sleep 300
            Debug.Print r; " --- "; FileToPrint
        End If
MainLoop:
    Next r

End Sub