用 VBA 打印 PDF 文件 [英] Printing PDF files with VBA
问题描述
我是使用 VBA 编码的新手.这是我未完成的代码,用于在包含具有 3 个不同标题DN"、INV"和PO"的文档的文件夹中打印文档.我一直在寻找打印出 PDF 文档的代码/方法.我尝试使用 invokeverb "&print" 函数,但它似乎不起作用.有人可以教我如何打印吗?非常感谢:)
I am new to coding with VBA. This is my unfinished code to print documents in a folder containing documents with 3 distinct headers, "DN" "INV" and "PO". I've been searching around for the code/method to print out PDF documents. I tried using the invokeverb "&print" function but it doesn't seem to work. Can someone please teach me how to print it out? Thank you very much :)
附言DN"需要打印一次,INV"需要打印6次,PO"需要打印2次.
P.S. "DN" needs to printed out once, " INV" needs to be printed out 6 times, "PO" needs to be printed out 2 times.
'' 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")
推荐答案
哟,
我发现了这个: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
但这只能让您在默认打印机上打印.
But this only let's you print on your default printer.
我测试过了.它有效:
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,我建议使用以下代码:
So, after correcting a mistake, that it is VBS and not VBA i suggest this code:
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")
这篇关于用 VBA 打印 PDF 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!