简体   繁体   English

无法使用VBA宏代码在电子邮件中发送附件

[英]Unable to send attachment in emails using VBA macro code

I intend to send a Company Profile as an email attachment along with the mail body to the intended customer list from the excel sheet carrying the client details ( Name,EmailID, etc. I am getting an error while i am compiling the code below " Compile Error: " Expected end of statement" 我打算将包含客户详细信息(姓名,EmailID等)的excel表中的公司简介作为电子邮件附件与邮件正文一起发送到目标客户列表。在编译“编译”下面的代码时遇到错误错误:“语句的预期结尾”

Email_ID from Excel 来自Excel的Email_ID

Sub Send_Email_from_Excel()
    Dim Email_ID As String
    Dim FileName As String
    Dim OutlookApp As Object
    Dim myAttachments As Object
    Dim Path As String
    Dim lastrow As Integer
    Dim Attachment As String
    Dim X As Integer
    X = 2
    Do While Sheet1.Cells(X, 1) <> " "
        For X = 2 To 4
            Set OutlookApp = CreateObject("Outlook.Application")
            Set OutlookmailItem = OutlookApp.createitem(0)
            Set myAttachments = OutlookmailItem.Attachments
            Path = "C:\Users\Prashant\Desktop\HR SOL New.pdf"
            Email_ID = Sheet1.Cells(X, 1)
            Attachment = Path + FileName
            MsgBox Email_ID
            OutlookmailItem.To = Email_ID
            OutlookmailItem.CC = "enquiry@hr-solutions.net.in"
            OutlookmailitemBody = "Dear Sir," & vbCrLf& & "As per our telephonic conversation today," & vbCrLf& & " Awaiting your revrt asap"
            myAttachments.Add (Attachment)
            OutlookmailItem.display
            OutlookmailItem.send
            Email_ID = " "
            X = X + 1
        Loop
    Next X
    Set OutlookApp = Nothing
    Set OutlookmailItem = Nothing
End Sub

Personally, I use X for columns (being the X-axis) and Y for rows (being the Y-axis) but personal preferences aside, this should work: 就个人而言,我将X用于列(作为X轴),将Y用于行(作为Y轴),但是除了个人喜好,这应该可以工作:

Sub Send_Email_from_Excel()
    Dim Email_ID As String
    Dim FileName As String
    Dim OutlookApp As Object
    Dim myAttachments As Object
    Dim Path As String
    Dim lastrow As Integer
    Dim Attachment As String
    Dim X As Integer
    X = 2
    Do While Sheet1.Cells(X, 1) <> " " ' Keep going until the cell being read contains a single space?
        Set OutlookApp = CreateObject("Outlook.Application")
        Set OutlookmailItem = OutlookApp.createitem(0)
        Set myAttachments = OutlookmailItem.Attachments
        Path = "C:\Users\Prashant\Desktop\HR SOL New.pdf"
        Email_ID = Sheet1.Cells(X, 1)
        Attachment = Path + FileName
        MsgBox Email_ID
        OutlookmailItem.To = Email_ID
        OutlookmailItem.CC = "enquiry@hr-solutions.net.in"
        Outlookmailitem.Body = "Dear Sir," & vbCrLf& & "As per our telephonic conversation today," & vbCrLf& & " Awaiting your revrt asap"
        myAttachments.Add (Attachment)
        ' OutlookmailItem.display ' I'm not sure you need this if you're using .Send
        OutlookmailItem.send
        X = X + 1 ' -alternatively, if you want it to only look at ever other line as your redundant loop suggests, use X = X + 2
    Loop

    Set OutlookApp = Nothing
    Set OutlookmailItem = Nothing
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM