将工作表导出为 PDF 并附加到 Outlook 电子邮件

Export worksheet as PDF and attach to Outlook e-mail

我在网上找到了这个代码并且它可以工作,但是每当它把作品sheet导出为 PDF 时,PDF 大约有 4000 页。我只需要 sheet 叫做“Grille”。

Sub SendWorksheet_AsPDFAttachment_OutlookEmail()
    Dim objFileSystem As Object
    Dim strTempFile As String
    Dim oApp As Object
    Dim oMail As Object

   
    'Specify the worksheet name
    Sheets("Grille").Activate
    ActiveSheet.UsedRange.Select
    ThisWorkbook.Sheets(Array("Grille")).Select


    Set objFileSystem = CreateObject("Scripting.FileSystemObject")
    strTempFile = objFileSystem.GetSpecialFolder(2).Path & "\" & ActiveSheet.Name & " in " & ThisWorkbook.Name & ".pdf"


    'export the specific worksheet as PDF
    Selection.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strTempFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True


    'Create a new email
    Set oApp = CreateObject("Outlook.application")
    Set oMail = oApp.CreateItem(0)


    'Attach the PDF file
    objMail.Attachments.Add strTempFile
    objMail.Display '==>display this email


    'Delete the temp PDF file
    objFileSystem.DeleteFile (strTempFile)
End Sub

而不是将 Selection 导出为 PDF 导出 sheet。

 'export the specific worksheet as PDF
    ThisWorkbook.Sheets("Grille").ExportAsFixedFormat Type:=xlTypePDF, Filename:=strTempFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False