【问题标题】:Open PDF and save specific pages as new PDF打开 PDF 并将特定页面另存为新 PDF
【发布时间】:2020-09-29 05:23:40
【问题描述】:

我有一个 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

【问题讨论】:

    标签: excel vba pdf sendkeys


    【解决方案1】:

    在我的临时文件夹中静默打开这些特定页面并将其另存为新的 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 
    

    【讨论】:

    • 感谢尤金的回复。不幸的是,我的目标是打开一个大约 25 页的 PDF 文件,然后将该 PDF 的 4 页保存到我的临时文件夹中的新 PDF 中。不需要将我当前的工作表另存为 PDF。
    猜你喜欢
    • 2015-09-26
    • 2020-01-27
    • 2015-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多