【问题标题】:Printing PDF files with VBA使用 VBA 打印 PDF 文件
【发布时间】:2018-11-27 03:13:40
【问题描述】:

我是使用 VBA 编码的新手。这是我未完成的代码,用于在包含具有 3 个不同标题“DN”“INV”和“PO”的文档的文件夹中打印文档。我一直在寻找打印 PDF 文档的代码/方法。我尝试使用 invokeverb "&print" 函数,但它似乎不起作用。有人可以教我如何打印出来吗?非常感谢:)

附: “DN”需要打印一次,“INV”需要打印6次,“PO”需要打印2次。

'' To set the path to the current folder

set shApp = CreateObject("shell.application")

currentPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".") 

set shFolder = shApp.NameSpace( currentPath )

'' To set the items in the current folder as "files"

set files = shFolder.Items()

''Start of code''

'msgbox("Starting Script")

for each files in files

        ' If name contains "DN" '
        if inStr(files, "DN") then
            'print out 1 time'
        end if
        ' if name contains "INV" '
        if inStr(files, "INV") then
            'print out 6 times'
        end if
        ' if name contains "PO" '
        if inStr(files, "PO") then
            'print out 2 times'
        end if
next
MsgBox("completed")

【问题讨论】:

    标签: vba vbscript


    【解决方案1】:

    Kajkrow 的 VBA 代码运行良好。我需要打印到特定的打印机,所以,如果有人在看这个,我找到了一个适合我的解决方案,简单地使用“printto”而不是“print”作为 ShellExecute 的动词,并提供特定打印机的名称文件名后面的第四个参数中的名称。

    Call apiShellExecute(Application.hwnd, "printto", strPathAndFilename, "my printer name", vbNullString, 0)
    

    【讨论】:

    • 我知道这是不久前的事情,但是在使用未找到打印机信息时出现错误。我试过多次输入打印机名称,想知道打印机名称应该是什么样子。是IP吗?如果这有任何改变,这台打印机位于具有多台计算机连接的网络上。
    • 您应该在 Windows 控制面板上查看打印机名称。有每台打印机的名称,您必须在此处使用相同的名称,至少对我有用。
    【解决方案2】:

    哟,

    我发现了这个:https://www.ozgrid.com/forum/forum/help-forums/excel-general/90407-printing-a-file-using-vba-code

    Option Explicit
    
    Declare Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (    ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _    ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
    
    Public Sub PrintFile(ByVal strPathAndFilename As String)
    
       Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, vbNullString, vbNullString, 0)
    
    End Sub
    
    Sub Test()
    
       PrintFile ("C:\Test.pdf")
    
    End Sub
    

    但这只能让您在默认打印机上打印。

    我测试过了。它有效:

    Declare Function apiShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
       ByVal hwnd As Long, _
       ByVal lpOperation As String, _
       ByVal lpFile As String, _
       ByVal lpParameters As String, _
       ByVal lpDirectory As String, _
       ByVal nShowCmd As Long) _
       As Long
    
    Public Sub PrintFile(ByVal strPathAndFilename As String)
    
       Call apiShellExecute(Application.hwnd, "print", strPathAndFilename, vbNullString, vbNullString, 0)
    
    End Sub
    
    Sub PrintPDF()
    
    '' To set the path to the current folder
    
    Set shApp = CreateObject("shell.application")
    
    'currentPath = CreateObject("Scripting.FileSystemObject").GetAbsolutePathName(".")
    currentPath = Application.ActiveWorkbook.Path
    
    Set shFolder = shApp.Namespace(currentPath)
    
    '' To set the items in the current folder as "files"
    
    Set Files = shFolder.Items()
    
    ''Start of code''
    
    'msgbox("Starting Script")
    
    For Each file In Files
       If InStr(file, ".pdf") Then
           ' If name contains "DN" '
           If InStr(file, "DN") Then
               PrintFile (currentPath + "\" + file)
           End If
           ' if name contains "INV" '
           If InStr(file, "INV") Then
               For i = 1 To 6
                   PrintFile (currentPath + "\" + file)
               Next i
           End If
           ' if name contains "PO" '
           If InStr(file, "PO") Then
                   PrintFile (currentPath + "\" + file)
                   PrintFile (currentPath + "\" + file)
           End If
       End If
    Next
    MsgBox ("completed")
    
    End Sub
    

    所以,在纠正一个错误之后,它是 VBS 而不是 VBA,我建议使用以下代码:

    Set shApp = CreateObject("shell.application")
    
    Set shFolder = shApp.Namespace(currentPath)
    
    '' To set the items in the current folder as "files"
    
    Set Files = shFolder.Items()
    
    ''Start of code''
    
    For Each file In Files
    If InStr(file, ".pdf") Then
       ' If name contains "DN" '
       If InStr(file, "DN") Then
           file.InvokeVerbEx("Print")
           WScript.Sleep 1000 'wait 1 sec
       End If
       ' if name contains "INV" '
       If InStr(file, "INV") Then
           Filename = currentPath + "\" + file
           Do
               i = i+1
               file.InvokeVerbEx("Print") 
               WScript.Sleep 1000 'wait 1 sec
           Loop until i >=6
           i = 0
       End If
       ' if name contains "PO" '
       If InStr(file, "PO") Then
           Filename = currentPath + "\" + file
               file.InvokeVerbEx("Print") 
               WScript.Sleep 1000 'wait 1 sec
               file.InvokeVerbEx("Print") 
               WScript.Sleep 1000 'wait 1 sec
        End If
    End If
    Next
    MsgBox ("completed")
    

    【讨论】:

    • 您好 Kajkrow,感谢您的帮助。然而,我很抱歉。我发现了为什么你的代码不能工作。我在 vbs 上执行此操作(使用记事本 ++)而不是 VBA。对不起,业余的错误......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 2016-03-04
    • 2018-12-25
    相关资源
    最近更新 更多