【问题标题】:Emailing an Excel sheet as a PDF directly直接以 PDF 格式通过电子邮件发送 Excel 工作表
【发布时间】:2016-01-06 06:53:25
【问题描述】:

我的目标是能够单击一个按钮并将我的 Excel 工作表转换为我的电子表格范围,并将其通过电子邮件发送到工作表中一个单元格中的电子邮件地址。对于初学者,我有可以将一系列单元格转换为 PDF 文件并允许我保存它的代码:

Option Explicit
Sub savePDF()
Dim wSheet As Worksheet
Dim vFile As Variant
Dim sFile As String

Set wSheet = ActiveSheet
sFile = Replace(Replace(Range("D11"), " ", ""), ".", "_") _
        & "_" _
        & Range("H11") _
        & ".pdf"
sFile = ThisWorkbook.Path & "\" & sFile

With Excel.Application.FileDialog(msoFileDialogSaveAs)

Dim i As Integer
For i = 1 To .Filters.Count
    If InStr(.Filters(i).Extensions, "pdf") <> 0 Then Exit For
Next i

.FilterIndex = i
.InitialFileName = sFile

.Show
If .SelectedItems.Count > 0 Then vFile = .SelectedItems.Item(.SelectedItems.Count)

End With

If vFile <> "False" Then
wSheet.Range("A1:BF47").ExportAsFixedFormat _
    Type:=xlTypePDF, _
    Filename:=vFile, _
    Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, _
    IgnorePrintAreas:=False, _
    OpenAfterPublish:=False

End If
End Sub

任何人都可以操纵此代码(附加到按钮),以便它通过电子邮件发送电子邮件地址,该地址位于特定单元格中,当单击按钮时,作为额外的奖励,电子邮件的主题来自单元格也在电子表格中?

【问题讨论】:

  • 也许见this?
  • 这是我想要的区域,但不同之处在于我的 excel 表需要将 PDF 的 excel 表附加到电子邮件中,我不知道如何执行此操作。 @findwindow
  • @findwindow:我同意。该解决方案应该可以正常工作。此解决方案中唯一缺少的是将 PDF 文件添加为附件:.Attachments.Add "C:\My Documents\" 'or vFile in the given solutionWith 语句中的MailItem
  • 对。这就是为什么我说也许在评论中看到这一点而不是提供答案,因为该链接不涵盖 pdf 部分。我相信有人会为此发布答案^_^就像我几天前所做的那样很容易链接^^

标签: vba excel pdf


【解决方案1】:

我有一个解决方案,如下所示。在我通过进入页面支付设置打印区域然后设置打印区域后,我成功地将 excel 表作为 PDF 文件通过电子邮件发送:

Sub savePDFandEmail()

Dim strPath As String, strFName As String
Dim OutApp As Object, OutMail As Object

strPath = Environ$("temp") & "\"  trailing "\"

strFName = ActiveWorkbook.Name
strFName = Left(strFName, InStrRev(strFName, ".") - 1) & "_" & ActiveSheet.Name & ".pdf"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
    strPath & strFName, Quality:=xlQualityStandard, _
    IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
    .to = Range("CB4")
    .CC = Range("CB6")
    .BCC = ""
    .Subject = Range("CB8")
    .Body = Range("BW11") & vbCr
    .Attachments.Add strPath & strFName
    '.Display    'Uncomment Display and comment .send to bring up an email window before sending
    .Send        'Keep this the same if you want to send the email address out on click of the button
End With

Kill strPath & strFName
On Error GoTo 0

Set OutMail = Nothing
Set OutApp = Nothing
End Sub

我还需要在我的工作表中添加一个小电子邮件工具,如下所示:

现在单击按钮将发送附有 PDF 文件的电子邮件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 2021-04-21
    • 2018-09-19
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多