提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如何将工作表保存为PDF文件并通过Outlook将其作为附件通过电子邮件发送?

在某些情况下,您可能需要通过Outlook将工作表作为PDF文件发送。 通常,您必须手动将工作表保存为PDF文件,然后在Outlook中创建一封包含此PDF文件的新电子邮件作为附件并发送。 手动逐步实现它是非常耗时的。 在本文中,我们将向您展示如何快速将工作表保存为PDF文件,并通过Excel中的Outlook以附件的形式自动发送。

将工作表保存为PDF文件并将其作为附件通过电子邮件发送给VBA代码


轻松地将工作表或多个工作表保存为单独的PDF文件:

在此 分割工作簿 实用程序 Kutools for Excel 可以帮助您轻松地将工作表或多个工作表保存为单独的PDF文件。 查看屏幕截图:

Kutools for Excel 包含了比300更方便的Excel工具。 免费试用60天无限制。 立即下载免费试用版

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel解决了您的大多数问题,并使您的生产率提高了80%
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 与Office 2007-2019和365一起使用; 支持所有语言; 在您的企业或组织中轻松部署。

箭头蓝色右泡 将工作表保存为PDF文件并将其作为附件通过电子邮件发送给VBA代码


您可以运行下面的VBA代码,将活动工作表自动保存为PDF文件,然后通过Outlook将其作为附件通过电子邮件发送。 请做如下。

1。 打开您将保存为PDF并发送的工作表,然后按 其他 + F11 键同时打开 Microsoft Visual Basic for Applications 窗口。

2。 在里面 Microsoft Visual Basic for Applications 窗口中,单击 插页 > 模块。 然后复制并粘贴下面的VBA代码到 代码 窗口。 看截图:

VBA代码:将工作表另存为PDF文件并将其作为附件通过电子邮件发送

Sub Saveaspdfandsend()
Dim xSht As Worksheet
Dim xFileDlg As FileDialog
Dim xFolder As String
Dim xYesorNo As Integer
Dim xOutlookObj As Object
Dim xEmailObj As Object
Dim xUsedRng As Range

Set xSht = ActiveSheet
Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)

If xFileDlg.Show = True Then
   xFolder = xFileDlg.SelectedItems(1)
Else
   MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
   Exit Sub
End If
xFolder = xFolder + "\" + xSht.Name + ".pdf"

'Check if file already exist
If Len(Dir(xFolder)) > 0 Then
    xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
                      vbYesNo + vbQuestion, "File Exists")
    On Error Resume Next
    If xYesorNo = vbYes Then
        Kill xFolder
    Else
        MsgBox "if you don't overwrite the existing PDF, I can't continue." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
        Exit Sub
    End If
    If Err.Number <> 0 Then
        MsgBox "Unable to delete existing file.  Please make sure the file is not open or write protected." _
                    & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
        Exit Sub
    End If
End If

Set xUsedRng = xSht.UsedRange
If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
    'Save as PDF file 
    xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard
    
    'Create Outlook email 
    Set xOutlookObj = CreateObject("Outlook.Application")
    Set xEmailObj = xOutlookObj.CreateItem(0)
    With xEmailObj
        .Display
        .To = ""
        .CC = ""
        .Subject = xSht.Name + ".pdf"
        .Attachments.Add xFolder
        If DisplayEmail = False Then
            '.Send
        End If
    End With
Else
  MsgBox "The active worksheet cannot be blank"
  Exit Sub
End If
End Sub

3。 按 F5 键来运行代码。 在里面 浏览 对话框中,请选择一个文件夹来保存此PDF文件,然后单击 OK 按钮。

笔记:

1。 现在活动工作表被保存为PDF文件。 并且PDF文件以工作表名称命名。

2。 如果活动工作表是空白的,您将会看到一个对话框,如下图所示,点击后显示屏幕截图 OK 按钮。

4。 现在创建了一个新的Outlook电子邮件,您可以看到PDF文件在附加字段中作为附件列出。 看截图:

5。 请撰写此电子邮件,然后发送。

注意:此代码仅在您将Outlook用作邮件程序时可用。


箭头蓝色右泡相关文章:


Kutools for Excel解决了您的大多数问题,并使您的生产率提高了80%

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能30天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Odd-Inge · 27 days ago
    Hello,

    Is it possible to find the name for pdf from a cell? Ex. Cell H4


    And in Cell H4 i want it to collect from three different cells. Is this possible?
  • To post as a guest, your comment is unpublished.
    Jason · 4 months ago
    How can I make it delete the saved pdf after it emails it?
    • To post as a guest, your comment is unpublished.
      crystal · 3 months ago
      Hi Jason,
      Sorry can't help you with that yet. You need to manually delete it after emailing it.
  • To post as a guest, your comment is unpublished.
    ranga · 4 months ago
    Thanks it works.
  • To post as a guest, your comment is unpublished.
    james · 1 years ago
    Hi, how can i save & send the pdf wit the workbook name with the current VBA code? what do i use instead of xSht.Name
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi James,
      Do you want to send the active worksheet as pdf and name it as the workbook name?
  • To post as a guest, your comment is unpublished.
    Tom H · 1 years ago
    How would I edit the VBA script above so that the file name is saved as a specific cell selected within the current sheet, for example cell A1?
  • To post as a guest, your comment is unpublished.
    Armin · 1 years ago
    How can I add for example sheet 2 from the workbook as an pdf?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Hi Armin,
      You need to open the Sheet 2 in your workbook firstly and then run the VBA code with above steps to get it down.
  • To post as a guest, your comment is unpublished.
    Michael Charry · 1 years ago
    How would I edit the VBA script above so that it adds a date and time stamp to the file name that way it doesn't keep overwriting what is already saved?
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Michael,
      Please run the below VBA code to solve the problem.

      Sub Saveaspdfandsend()
      Dim xSht As Worksheet
      Dim xFileDlg As FileDialog
      Dim xFolder As String
      Dim xYesorNo As Integer
      Dim xOutlookObj As Object
      Dim xEmailObj As Object
      Dim xUsedRng As Range
      Dim xStr As String

      Set xSht = ActiveSheet
      Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)

      If xFileDlg.Show = True Then
      xFolder = xFileDlg.SelectedItems(1)
      Else
      MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
      Exit Sub
      End If
      xStr = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
      xFolder = xFolder + "\" + xSht.Name + "-" + xStr + ".pdf"

      'Check if file already exist
      If Len(Dir(xFolder)) > 0 Then
      xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
      vbYesNo + vbQuestion, "File Exists")
      On Error Resume Next
      If xYesorNo = vbYes Then
      Kill xFolder
      Else
      MsgBox "if you don't overwrite the existing PDF, I can't continue." _
      & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
      Exit Sub
      End If
      If Err.Number <> 0 Then
      MsgBox "Unable to delete existing file. Please make sure the file is not open or write protected." _
      & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
      Exit Sub
      End If
      End If

      Set xUsedRng = xSht.UsedRange
      If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
      'Save as PDF file
      xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard

      'Create Outlook email
      Set xOutlookObj = CreateObject("Outlook.Application")
      Set xEmailObj = xOutlookObj.CreateItem(0)
      With xEmailObj
      .Display
      .To = ""
      .CC = ""
      .Subject = xSht.Name + "-" + xStr + ".pdf"
      .Attachments.Add xFolder
      If DisplayEmail = False Then
      '.Send
      End If
      End With
      Else
      MsgBox "The active worksheet cannot be blank"
      Exit Sub
      End If
      End Sub
      • To post as a guest, your comment is unpublished.
        Parag · 4 months ago
        Hi Crystal,

        It's really great and working perfectly for me. Need more help to add:

        1. in "To" I want to give link to particular cell of Active sheet like wise in CC and in BCC i would like to add active sheet link
        2. in e-mail body i need to specify some standard text.

        I will be great full to you for your help.

        Thanks
        Parag
      • To post as a guest, your comment is unpublished.
        Parag Somani · 4 months ago
        Hi Crystal,

        It's really great and working perfectly for me. Need more help to add:

        1. in "To" I want to give link to particular cell of Active sheet like wise in CC and in BCC i would like to add active sheet link
        2. in e-mail body i need to specify some standard text.

        I will be great full to you for your help.

        Thanks
        Parag
      • To post as a guest, your comment is unpublished.
        Parag Somani · 4 months ago
        Hi Crystal,

        It's really great and working perfectly for me. Need more help to add:

        1. in "To" I want to give link to particular cell of Active sheet like wise in CC and in BCC i would like to add active sheet link
        2. in e-mail body i need to specify some standard text.

        I will be great full to you for your help.

        Thanks
        Parag
        • To post as a guest, your comment is unpublished.
          crystal · 4 months ago
          Hi Parag Somani,
          The below VBA code can help you. Please change the .To, .CC, .BCC and .Body fields based on your needs.

          Sub Saveaspdfandsend()
          Dim xSht As Worksheet
          Dim xFileDlg As FileDialog
          Dim xFolder As String
          Dim xYesorNo As Integer
          Dim xOutlookObj As Object
          Dim xEmailObj As Object
          Dim xUsedRng As Range
          Dim xStr As String

          Set xSht = ActiveSheet
          Set xFileDlg = Application.FileDialog(msoFileDialogFolderPicker)

          If xFileDlg.Show = True Then
          xFolder = xFileDlg.SelectedItems(1)
          Else
          MsgBox "You must specify a folder to save the PDF into." & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Must Specify Destination Folder"
          Exit Sub
          End If
          xStr = Format(Now(), "yyyy-mm-dd-hh-mm-ss")
          xFolder = xFolder + "\" + xSht.Name + "-" + xStr + ".pdf"

          'Check if file already exist
          If Len(Dir(xFolder)) > 0 Then
          xYesorNo = MsgBox(xFolder & " already exists." & vbCrLf & vbCrLf & "Do you want to overwrite it?", _
          vbYesNo + vbQuestion, "File Exists")
          On Error Resume Next
          If xYesorNo = vbYes Then
          Kill xFolder
          Else
          MsgBox "if you don't overwrite the existing PDF, I can't continue." _
          & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Exiting Macro"
          Exit Sub
          End If
          If Err.Number <> 0 Then
          MsgBox "Unable to delete existing file. Please make sure the file is not open or write protected." _
          & vbCrLf & vbCrLf & "Press OK to exit this macro.", vbCritical, "Unable to Delete File"
          Exit Sub
          End If
          End If

          Set xUsedRng = xSht.UsedRange
          If Application.WorksheetFunction.CountA(xUsedRng.Cells) <> 0 Then
          'Save as PDF file
          xSht.ExportAsFixedFormat Type:=xlTypePDF, Filename:=xFolder, Quality:=xlQualityStandard

          'Create Outlook email
          Set xOutlookObj = CreateObject("Outlook.Application")
          Set xEmailObj = xOutlookObj.CreateItem(0)
          With xEmailObj
          .Display
          .To = Range("A8")
          .CC = Range("A9")
          .BCC = Range("A10")
          .Subject = xSht.Name + "-" + xStr + ".pdf"
          .Body = "Dear " _
          & vbNewLine & vbNewLine & _
          "This is a test email " & _
          "sending in Excel"
          .Attachments.Add xFolder
          If DisplayEmail = False Then
          '.Send
          End If
          End With
          Else
          MsgBox "The active worksheet cannot be blank"
          Exit Sub
          End If
          End Sub
  • To post as a guest, your comment is unpublished.
    Darren · 1 years ago
    I have tried pasting this into a new module and i get Compile error: Sub or Function not defined. Please help.
  • To post as a guest, your comment is unpublished.
    Michael · 2 years ago
    This is working great for me but is there a way to select a folder location automatically rather than select manually? I am hoping to do this for 40 sheets at once.