如何使用VBA打印PDF文件?
时间:2023-09-06 22:41:26
Q:我想要在VBA中使用代码来打印指定的PDF文件,如何实现?
A:在之前的文章中,我们介绍了一个自定义函数ExePath,可以获取能够打开指定文件的EXE程序的路径。这样,我们就可以使用EXE程序来打开该文件了。因此,下面的代码先使用ExePath函数获取PDF文件的可执行程序路径,然后使用它来打开指定的PDF文件。
代码如下:
DeclareFunction FindExecutable Lib “shell32.dll” Alias “FindExecutableA” _
(ByVal lpFileAs String, ByVal lpDirectory As String, ByVal lpResult As String) As Long
Sub Test_PrintPDF()
Dim strFileName As String
strFileName = “D:\test.pdf”
PrintPDf strFileName
End Sub
Sub PrintPDf(fnAs String)
Dim pdfEXE As String
Dim q As String
pdfEXE = ExePath(fn)
If pdfEXE = “” Then
MsgBox “没有找到pdf相关的EXE程序.”,vbCritical, “Macro Ending”
Exit Sub
End If
q = “”””
Shell q & pdfEXE & q & ” /s/o /h /t ” & q & fn & q, vbHide
End Sub
Function ExePath(lpFile As String) As String
Dim lpDirectory As String
Dim strExePath As String
Dim lrc As Long
lpDirectory = “\”
strExePath = Space(255)
lrc = FindExecutable(lpFile, lpDirectory,strExePath)
strExePath = Left$(strExePath,InStr(strExePath, Chr$(0)) – 1)
ExePath = strExePath
End Function
代码中:
1.使用变量strFileName指定了所要打印的PDF文件的完整路径名。
2.对于AcroRd32.exe,传递给Shell命令的参数如下:
/n-启动一个新的Reader实例,即使该实例已经打开
/s-不显示启动界面
/o-不显示打开文件对话框
/h-以最小化窗口打开
/p
/t
3.确保使用双引号将EXE完整的路径和PDF文件完整路径名括起来。
还有一段更简单一些的代码可以实现:
Declare FunctionapiShellExecute 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 (“D:\test.pdf”)
End Sub
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Win10系统MindMaster闪退解决方法
![](https://img.aspxhome.com/file/2023/4/a312575_0s.jpg)
WinXP系统电脑打不开exe文件的解决方法
![](https://img.aspxhome.com/file/2023/4/a309253_0s.jpg)
PowerPoint中怎么保存屏幕录制的视频
![](https://img.aspxhome.com/file/2023/10/a350894_0s.jpg)
WPS excel表格简化必备小技巧
![](https://img.aspxhome.com/file/2023/7/a187477_0s.jpg)
Word中2007版文档编辑页眉和页脚的操作技巧
这些Word批量操作技巧你会吗?学会效率提高一倍
![](https://img.aspxhome.com/file/2023/0/22440_0s.gif)
word 一级标题怎么设置
![](https://img.aspxhome.com/file/2023/9/33239_0s.png)
我的世界hmcl启动器forge安装失败怎么办?forge安装失败的解决方法
![](https://img.aspxhome.com/file/2023/7/a328099_0s.jpg)
OS X中如何管理窗口大小
![](https://img.aspxhome.com/file/2023/3/a216703_0s.jpg)
win10手机预览版10536.1004已知问题汇总
![](https://img.aspxhome.com/file/2023/28/a247328_0s.png)
Windows内存效率的优化方法
![](https://img.aspxhome.com/file/2023/29/a260085_0s.jpg)
WPS文档技巧—如何输入因为所以符号
专业的苹果Mac照片后期处理工具:DxO PhotoLab 4
![](https://img.aspxhome.com/file/2023/9/a208019_0s.jpeg)
u盘系统怎么安装到电脑
![](https://img.aspxhome.com/file/2023/2/a288338_0s.png)
excel表格数据怎么转换为箱形图?
![](https://img.aspxhome.com/file/2023/7/40077_0s.jpg)
Win10系统蓝屏DMProtectEX64.sys,错误代码0xc0000428解决方法
![](https://img.aspxhome.com/file/2023/1/49091_0s.png)
找回丢失的任务栏“显示桌面”图标、快速返回桌面快捷键
Excel中表格数据分割成多列的设置技巧
Windows7开启Excel超链接显示“由于本机限制”怎么办?
![](https://img.aspxhome.com/file/2023/27/a232757_0s.jpg)
Win10开始菜单背景怎么设置透明?
![](https://img.aspxhome.com/file/2023/26/a224530_0s.jpg)