初学VBA,自己参照网上写了一段代码,想要实现在特定的模板下,利用VBA可以自己循环发邮件的功能。OUTLOOK已经和EXCEL连接上,我之前写的更简单的VBA可以自己发邮件,但是不知道为什么一加上循环,就总是在收件人这一行报错?求指点。
Sub SendEmail()
Mypath = "E:\Vera\实习\VBA群发邮件" emailattachment = Mypath &"\" & "附1:声明文件.docx" '附件位置
Windows("练习VBA.xlsm").Activate
Dim OutApp As Object Dim OutMail As Object Dim MailSubject As String Set OutApp =CreateObject("outlook.Application") Set OutMail =OutApp.CreateItemFromTemplate("E:\Vera\实习\VBA群发邮件\邮件内容.oft") '邮件模板位置
MsgBox ("This will send a email toclients") With OutMail
For i = 2 To 6
.To =Worksheets("Sheet1").Cells(i, "E").Value '这一行总是报错,但是我设置的就是E列为收件人邮箱 .CC =Worksheets("Sheet1").Cells(i, "F").Value .Subject = "请提供所有人信息" .Attachments.Add emailattachment .Send
Next End With
Set OutMail = Nothing Set OutApp = Nothing End Sub
|