如何使用Excel宏打印pdf文件

时间:2018-08-08 08:32:58

标签: excel vba excel-vba

您好,我需要在打印文件后从一个文件夹中打印pdf文件,我需要将该文件移至另一个文件夹中。我的系统是x86位,所以有人可以在这个问题上帮助我

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

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

Sub Auto_print()

Dim lstFile As String
Dim lstDir As String
Dim i As Long

lstDir = "C:" & VBA.Environ("homepath") & "\Desktop\auto\"
lstFile = VBA.Dir(lstDir & "*.pdf")

Do While VBA.Len(lstFile) > 0
    i = FindWindow("XLMAIN", Application.Caption)
   ShellExecute i, "print", lstFile, vbNullString, vbNullString, 10
    lstFile = VBA.Dir
Loop
End Sub

1 个答案:

答案 0 :(得分:0)

尝试下面的代码移动文件,并在打印后在代码中调用它。

i = FindWindow("XLMAIN", Application.Caption)
ShellExecute i, "print", lstFile, vbNullString, vbNullString, 10
MoveFile lstFile
lstFile = VBA.Dir

用需要移动文件的实际路径替换Destination Folder\

Private Function MoveFile(strSource As String)
    Dim fso As Object
    Set fso = CreateObject("Scripting.FileSystemObject")
    fso.MoveFile source:=strSource, Destination:="Destination Folder\" & Right(strSource, Len(strSource) - InStrRev(strSource, "\"))
End Function