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

时间:2017-07-14 08:09:17

标签: excel vba excel-vba excel-formula

我打算将公司资料作为电子邮件附件连同邮件正文一起发送到带有客户详细信息的Excel工作表中的目标客户列表(名称,EmailID等。我在编译代码时遇到错误在“编译错误:”预期结束语“

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

1 个答案:

答案 0 :(得分:0)

就个人而言,我使用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