在VBA中单击一下,将pdf和excel文件附加到电子邮件中

时间:2014-10-13 20:33:26

标签: vba

我有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

1 个答案:

答案 0 :(得分:0)

With OutlookMail

    .Display
    .To = Email_To
    .Cc = Email_CC
    .BCC = Email_BCC
    .Subject = EmailSubject & CurrentMonth
    .Attachments.Add PDFFile
    .Attachments.Add "[Excel File Path Goes Here]" 'Add this line

    If DisplayEmail = False Then

        .Send

    End If

End With