【问题标题】:Printing a PDF file and terminating Adobe Reader打印 PDF 文件并终止 Adob​​e Reader
【发布时间】:2014-03-10 09:19:36
【问题描述】:

我正在尝试将工作表 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 阅读器会终止,但由于我的文件会立即打开和关闭,因此不会打印任何内容。

所以有什么帮助吗??

【问题讨论】:

  • 我唯一的建议是首先不要在 Adob​​e Reader 中打开它。如果您注意到,您可以从上下文菜单中打印 PDF,而无需实际打开 Reader - 我建议您研究一下它是如何工作的......
  • 即使我第一次使用 shellexecute 打开文件也会发生这种情况。你能解释一下在没有实际打开阅读器的情况下从上下文菜单打印 PDF 是什么意思吗??
  • 在安装了 Reader 的 Windows 中,在资源管理器中右键单击 PDF 将在选项中显示“打印”。选择此选项将在不打开 Reader 的情况下打印 PDF。您应该能够使用类似的机制来避免首先打开 Reader。请记住,这只是一个想法,可能适用于您的情况,也可能不适用。
  • 感谢您的想法。但我认为尝试使用代码来实现这一点对我来说非常困难。

标签: excel vba


【解决方案1】:

也许这有点简单,但为什么不能只打印 Excel 文件而不是 PDF?它们应该是一样的,不是吗?

【讨论】:

    猜你喜欢
    • 2019-06-21
    • 1970-01-01
    • 2014-07-07
    • 1970-01-01
    • 1970-01-01
    • 2011-06-12
    • 2013-04-08
    • 1970-01-01
    • 2012-03-09
    相关资源
    最近更新 更多