使用VBA打印PDF文件

时间:2018-06-18 09:44:46

标签: vba vbscript

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

P.S。 " 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")

2 个答案:

答案 0 :(得分:1)

Kajkrow的VBA代码效果很好。我需要打印到特定的打印机,因此,如果有人在看,我找到了一个对我有用的解决方案,只需将“ printto”而不是“ print”用作SheelExecute的动词,并提供特定打印机的名称在文件名后的第四个参数中输入名称。

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

答案 1 :(得分:0)

哟,

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

  

选项明确

     

声明函数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)_      长期

     

Public Sub PrintFile(ByVal strPathAndFilename As String)

     

调用apiShellExecute(Application.hwnd," print",strPathAndFilename,vbNullString,> vbNullString,0)

     

End Sub

     

子测试()

     

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")