用VBA打印PDF文件
我是新的编码与VBA。这是我未完成的代码,用于打印文件夹中的文档,其中包含具有3个不同标题“DN”“INV”和“PO”的文档。我一直在寻找打印PDF文档的代码/方法。我尝试使用invokeverb“&print”函数,但似乎不起作用。有人能教我怎么打印出来吗?非常感谢:) p.S.“DN”需要打印一次,“INV”需要打印6次,“PO”需要打印2次用VBA打印PDF文件,vba,vbscript,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.a
'' 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")
哟,
我发现:
但这只允许您在默认打印机上打印
我测试过了。它的工作原理是:
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
因此,在更正错误后,我建议使用以下代码:
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的VBA代码运行良好。我需要打印到特定的打印机,因此,如果有人看到了这一点,我找到了一个适合我的解决方案,简单地使用“printto”而不是“print”作为ShellExecute的动词,并在文件名后面的第四个参数中提供特定打印机名的名称
Call apiShellExecute(Application.hwnd, "printto", strPathAndFilename, "my printer name", vbNullString, 0)
嗨,卡杰克罗,谢谢你的帮助。然而,我很抱歉。我找到了你的代码无法工作的原因。我在vbs(使用记事本++)上做这件事,而不是在VBA上。对不起,业余爱好者的错误…我知道这是从一段时间以前,但我得到了一个错误,当使用这个说打印机信息未找到。我试过多次输入打印机名称,想知道打印机名称应该是什么样子。是IP吗?如果有任何变化,则此打印机位于具有多个计算机连接的网络上。您应该查看windows控制面板上的打印机名称。这是每台打印机的名称,与您在这里必须使用的相同,至少对我来说是这样。
Call apiShellExecute(Application.hwnd, "printto", strPathAndFilename, "my printer name", vbNullString, 0)