【发布时间】:2014-10-13 20:33:25
【问题描述】:
我有 VBA 代码在 excel 中创建 pdf 文件并在按钮单击时附加到电子邮件。我想知道是否可以通过单击将 pdf 和 excel 文件都附加到电子邮件中。 请在下面找到我要修改的代码。粗体部分显示了两个功能。非常感谢任何建议或帮助!谢谢
Sub Button1_Click()
Dim EmailSubject As String, EmailSignature As String
Dim Email_Body As String
Dim olMailItem As Object
'Dim olFormatHTML As Form
Dim objMail As Object
Dim CurrentMonth As String, DestFolder As String, PDFFile As String
Dim Email_To As String, Email_CC As String, Email_BCC As String
Dim OpenPDFAfterCreating As Boolean, AlwaysOverwritePDF As Boolean, DisplayEmail As Boolean
Dim OverwritePDF As VbMsgBoxResult
Dim OutlookApp As Object, OutlookMail As Object
'CurrentMonth = ""
Dim fName As String, eCode1 As String, eCode2 As String, fNameLong As String
' *****************************************************
' ***** You Can Change These Variables *********
'Create excel
fName = "User Access request:"
eCode1 = Sheet1.Range("B7").Value
eCode2 = Range("B7").Value
fNameLong = fName & " " & eCode1 & " - " & eCode2
EmailSubject = "abs" 'Change this to change the subject of the email. The current month is added to end of subj line
OpenPDFAfterCreating = False 'Change this if you want to open the PDF after creating it : TRUE or FALSE
AlwaysOverwritePDF = True 'Change this if you always want to overwrite a PDF that already exists :TRUE or FALSE
DisplayEmail = True 'Change this if you don't want to display the email before sending. Note, you must have a TO email address specified for this to work
Email_To = "abc" 'Change this if you want to specify To email e.g. ActiveSheet.Range("H1") to get email from cell H1
Email_CC = "xyz.com"
Email_BCC = ""
'Create the PDF
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=PDFFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=OpenPDFAfterCreating
**
'Create an Outlook object and new mail message
Set OutlookApp = CreateObject("Outlook.application") 'Dialogs (DialogSendMail).Show ")
Set OutlookMail = OutlookApp.CreateItem(0)
'Create excel file but to another email
Application.Dialogs(xlDialogSendMail).Show "xyz", fNameLong
**
'Display email and specify To, Subject, etc
With OutlookMail
.Display
.To = Email_To
.Cc = Email_CC
.BCC = Email_BCC
.Subject = EmailSubject & CurrentMonth
.Attachments.Add PDFFile
If DisplayEmail = False Then
.Send
End If
End With
End Sub
【问题讨论】:
-
您好,有朋友可以帮我吗?下面的行已添加但它不起作用 .Attachments.Add "[Excel File Path Goes Here]" 'Add this line
标签: vba