我正在尝试将工作表Sheet1
导出为PDF文件并打印,然后使用代码关闭adobe reader。
这是我正在使用的代码:
Private Declare Function ShellExecute 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 Function PrintThisDoc(Formname As Long, FileName As String)
On Error Resume Next
Dim X As Long
X = ShellExecute(Formname, "Print", FileName, 0&, 0&, 1)
End Function
Private Sub CommandButton1_Click()
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, FileName:="D:\Print.pdf"
PrintThis = PrintThisDoc(0, "D:\Print.pdf")
Application.Wait (Now + TimeSerial(0, 0, 10))
TerminateProcess
End Sub
我使用.wait
方法暂停代码10秒钟,直到打印订单被发送到打印机,然后终止adobe reader。
但是,当我添加此行时,TerminateProcess
子受到影响,我得到Not found error
这一行(intError = objProcess.Terminate
)
Sub TerminateProcess()
Dim strTerminateThis As String
Dim objWMIcimv2 As Object
Dim objProcess As Object
Dim objList As Object
Dim intError As Integer
'Process to terminate
strTerminateThis = "AcroRd32.exe"
'Connect to CIMV2 Namespace
Set objWMIcimv2 = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
'Find the process to terminate
Set objList = objWMIcimv2.ExecQuery("select * from win32_process where name='" & strTerminateThis & "'")
'Terminates a process and all of its threads.
For Each objProcess In objList
intError = objProcess.Terminate
Next
Set objWMIcimv2 = Nothing
Set objList = Nothing
Set objProcess = Nothing
End Sub
当我删除.wait
行时,Adobe阅读器会终止,但不会打印任何内容,因为我的文件很快就会打开和关闭。
所以请帮忙吗?
答案 0 :(得分:0)
也许这有点过分了,但为什么你不能只打印Excel文件而不是PDF?他们应该是一样的不应该吗?