【问题标题】:Attach both pdf and excel files to an email on single click in VBA在 VBA 中单击即可将 pdf 和 excel 文件附加到电子邮件中
【发布时间】: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


【解决方案1】:
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

【讨论】:

  • 您好,我按照上述进行了更改并提供了 excel 路径,但仍然无法正常工作.. 我只收到 PDF 附件
  • 嗨,有朋友可以帮我吗?下面的行已添加但它不工作 .Attachments.Add "[Excel File Path Goes Here]" 'Add this line
  • "[Excel File Path Goes Here]" 将替换为您的实际文件路径。在您的代码中,您可以使用 ThisWorkbook.FullName 将其作为变量提供 - 所以这行代码将显示为 .Attachments.Add ThisWorkbook.FullName
猜你喜欢
  • 1970-01-01
  • 2023-03-07
  • 2020-11-21
  • 1970-01-01
  • 1970-01-01
  • 2012-05-17
  • 2018-07-24
  • 2017-10-23
  • 1970-01-01
相关资源
最近更新 更多