我打算將公司簡介作爲電子郵件附件與郵件正文一起發送到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
更改'Loop'和'Next'的位置,它們的順序是錯誤的。 – UGP
如果你像上面那樣格式化你的代碼塊,你可以更好地看到to循環。我越看循環,我認爲其中一個是多餘的。我不明白爲什麼需要For Next循環。 – CLR
另外'對於X = 2到4'和'Next x'在循環中自動遞增 - 除非你真的希望X = 2(第一個循環),否則不需要'x = x + 1'那麼X = 4(第二次通過)而不是X = 3。 – ainwood