2017-07-14 70 views
0

我打算將公司簡介作爲電子郵件附件與郵件正文一起發送到Excel工作表中攜帶客戶詳細信息(名稱,電子郵件ID等)的預期客戶列表。我得到當我編譯下面「編譯錯誤:」代碼中的錯誤從Excel無法使用VBA宏代碼在電子郵件中發送附件

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 = "[email protected]" 
      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 
+0

更改'Loop'和'Next'的位置,它們的順序是錯誤的。 – UGP

+0

如果你像上面那樣格式化你的代碼塊,你可以更好地看到to循環。我越看循環,我認爲其中一個是多餘的。我不明白爲什麼需要For Next循環。 – CLR

+0

另外'對於X = 2到4'和'Next x'在循環中自動遞增 - 除非你真的希望X = 2(第一個循環),否則不需要'x = x + 1'那麼X = 4(第二次通過)而不是X = 3。 – ainwood

回答

0

個人陳述的預計結束」

EMAIL_ID,我使用的是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 = "[email protected]" 
     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 
+0

輸入了上面給出的代碼,但它沒有從Excel工作表中挑選電子郵件ID,並且主題行和身體信息也未從代碼中完全取下 – prashant3s

+0

我編輯了代碼,因爲我注意到您有錯字(我已經複製)在'.Body'行中。 – CLR

+0

至於*主題* - 你沒有'OutlookmailItem.Subject'行 - 所以你沒有得到。 – CLR

相關問題