Vba 直接以PDF格式通过电子邮件发送Excel工作表

Vba 直接以PDF格式通过电子邮件发送Excel工作表,vba,excel,pdf,Vba,Excel,Pdf,我的目标是能够点击一个按钮,让我的Excel工作表将我的电子表格的一系列内容转换成PDF格式,并通过电子邮件将其发送到工作表中某个单元格中的电子邮件地址。首先,我有一段代码,可以将一系列单元格转换为PDF文件,并允许我保存它: Option Explicit Sub savePDF() Dim wSheet As Worksheet Dim vFile As Variant Dim sFile As String Set wSheet = ActiveSheet sFile = Replace

我的目标是能够点击一个按钮,让我的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
选项显式
Sub-savePDF()
将工作表设置为工作表
作为变量的Dim vFile
将文件设置为字符串
设置wSheet=ActiveSheet
sFile=Replace_
& "_" _
&范围(“H11”)_
&“.pdf”
sFile=此工作簿。路径&“\”&sFile
使用Excel.Application.FileDialog(msoFileDialogSaveAs)
作为整数的Dim i
对于i=1到.Filters.Count
如果InStr(.Filters(i).Extensions,“pdf”)为0,则退出
接下来我
.FilterIndex=i
.InitialFileName=sFile
显示
如果.SelectedItems.Count>0,则vFile=.SelectedItems.Item(.SelectedItems.Count)
以
如果vFile为“False”,则
wSheet.Range(“A1:BF47”).ExportAsFixedFormat_
类型:=xlTypePDF_
文件名:=vFile_
质量:=xlQualityStandard_
IncludeDocProperties:=True_
IgnorePrintAreas:=假_
OpenAfterPublish:=False
如果结束
端接头

任何人都可以操纵此代码(附在按钮上),以便在单击按钮时发送电子邮件至特定单元格中的电子邮件地址,并且作为额外奖励,电子邮件的主题也可以从电子表格中的单元格中工作?

我有一个解决方案,如下所示。在我通过进入PagePayout设置打印区域,然后设置打印区域后,我成功地以PDF文件的形式通过电子邮件发送excel工作表:

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文件的电子邮件。

我有一个解决方案,如下所示。在我通过进入PagePayout设置打印区域,然后设置打印区域后,我成功地以PDF文件的形式通过电子邮件发送excel工作表:

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文件的电子邮件。

可能看到了吗?这是我想要的区域,但这是不同的,因为我的excel工作表需要将PDF格式的excel工作表附加到电子邮件中,我不确定如何做@findwindow@findwindow:我同意。这个解决方案应该非常有效。此解决方案中唯一缺少的是将PDF文件添加为附件:
.Attachments.add“C:\My Documents\”或给定解决方案中的vFile
,在
With
语句中添加
邮件项。这就是为什么我说也许在评论中看到,而不是提供答案,因为该链接不包括pdf部分。我相信有人会发布答案,就像我前几天做的那样,很容易链接^^^也许会看到?这是我想要的地方,但这是不同的,我的excel工作表需要将PDF格式的excel工作表附加到电子邮件中,我不知道如何做@findwindow@findwindow:我同意。这个解决方案应该非常有效。此解决方案中唯一缺少的是将PDF文件添加为附件:
.Attachments.add“C:\My Documents\”或给定解决方案中的vFile
,在
With
语句中添加
邮件项。这就是为什么我说也许在评论中看到,而不是提供答案,因为该链接不包括pdf部分。我相信有人会发布答案,就像我几天前做的那样,很容易链接^^