直接将 Excel 工作表作为 PDF 通过电子邮件发送

问题描述 投票:0回答:2

我的目标是能够单击一个按钮,将我的 Excel 工作表转换为 PDF 电子表格的范围,并将其通过电子邮件发送到工作表中一个单元格中的电子邮件地址。对于初学者来说,我有可以将一系列单元格转换为 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

任何人都可以操纵此代码(附加到按钮),以便它会向特定单元格中的电子邮件地址发送电子邮件,当单击该按钮时,作为额外的奖励,可以从该单元格中的单元格中获取电子邮件的主题。还有电子表格吗?

vba excel pdf
2个回答
0
投票

我有一个解决方案如下。通过进入页面支付设置打印区域,然后设置打印区域后,我成功地将 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

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

Email Tool Inside of Spreadsheet

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


0
投票

我在编译时遇到以下语法错误。请指教。谢谢。

strPath = Environ$("temp")& "" 尾随 """

© www.soinside.com 2019 - 2024. All rights reserved.