我有一个PDF文件,我需要每天邮寄出去。我在我的excel文件中设置了一个宏,更新了一个数据表,这个数据表在邮件的正文中,然后它打开这个PDF文件,并将4页保存为一个PDF文件,并将它附在我发送的邮件中。
问题是,sendkeys真的不是那么可靠,我想使用其他的东西,或者让它只是默默地打开并将这些特定的页面保存为一个新的pdf在我的临时文件夹中。任何想法将被感激
Option Explicit
Public Sub Print_All_PDF_Files_in_Folder()
On Error Resume Next
Kill "C:\temp\S4 Region.pdf"
On Error GoTo 0
Dim folder As String
Dim PDFfilename As String
folder = "location of pdf" 'CHANGE AS REQUIRED
If Right(folder, 1) <> "\" Then folder = folder & "\"
PDFfilename = Dir(folder & "S4 Reg" & "*.pdf", vbNormal)
While Len(PDFfilename) <> 0
Print_PDF folder & PDFfilename
PDFfilename = Dir() ' Get next matching file
Wend
Call ClosePDF
End Sub
Private Sub Print_PDF(sPDFfile As String)
Shell "C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe /p " & Chr(34) & sPDFfile & Chr(34)
SendKeys "p"
SendKeys "%g"
SendKeys "{tab}"
SendKeys "5,9,14,15"
SendKeys "%r"
SendKeys "{down 2}"
Application.Wait DateAdd("s", 10, Now)
SendKeys "{enter}"
Application.Wait DateAdd("s", 15, Now)
SendKeys "{tab 6}"
SendKeys "{enter}"
SendKeys "C:\temp"
SendKeys "%s"
Application.Wait DateAdd("s", 10, Now)
End Sub
Sub ClosePDF()
Dim Process As Object, intError As Integer
For Each Process In GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2").ExecQuery("select * from win32_process where name='AcroRd32.exe'")
intError = Process.Terminate 'Terminates a process and all of its threads.
If intError <> 0 Then Exit For 'Return value is 0 for success. Any other number is an error.
Next
End Sub
默默地打开并将这些特定的页面保存为一个新的PDF文件在我的临时文件夹中。
你需要使用 Workbook.ExportAsFixedFormat 方法,用于将工作簿发布为PDF或XPS格式。请注意以下参数。
From
- 开始发布的页数。如果省略此参数,则从头开始发布。
To
- 要发布的最后一页的编号。如果省略此参数,则以最后一页结束发布。
OpenAfterPublish
- 如果设置为 True
,发布后在查看器中显示文件。如果设置为 False
文件已发布,但不显示。
ActiveWorkbook.ExportAsFixedFormat Type:=xlTypePDF FileName:="sales.pdf" Quality:=xlQualityStandard From:=2 To:=4 OpenAfterPublish:=True