使用VBA将Outlook邮件发送到Excel

时间:2015-05-06 16:04:01

标签: outlook automation

Excel File Link

这是来自you-tube视频的代码。以下代码给出了编译器错误:Userdefined Type未定义。

Sub SendEmail(what_address As String, Subject_line As String, mail_body As String)

'Dim olApp As Outlook.Application
Set olApp = CreateObject("outlook.Application")
'Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)

olMail.To = what_address
olMail.Subject = Subject_line
olMail.body = mial_body
olMail.send
End Sub

Sub SendMassEmail()
row_number = 1
Do
DoEvents
row_number = row_number + 1
Dim mail_body_message As String
Dim full_name As String
Dim Promoscode As String

mail_body_message = Sheet1.Range("J2")
full_name = Sheet1.Range("B" & row_number) & " " & Sheet1.Range("C" & row_number)
promo_code = Sheet1.Range("D" & row_number)
mial_body_message = Replace(mail_body_message, "replace_name_here", full_name)

Call SendEmail(Sheet1.Range("A1" & row_number), "This is a test e-mail", mail_body_message)

Loop Until row_number = 6

End Sub

我遇到了编译器错误,我已经检查了所有内容,但是...不确定是什么引起了这个问题。

1 个答案:

答案 0 :(得分:0)

使用MailItem类的Recipients属性指定收件人而不是To属性。

Recipients类提供Add方法,该方法允许创建新的收件人并将其添加到集合中。然后使用Resolve方法尝试针对通讯簿解析收件人对象。

Sub AssignTask()  
  Dim myItem As Outlook.TaskItem  
  Dim myDelegate As Outlook.Recipient 
  Set MyItem = Application.CreateItem(olTaskItem)  
  MyItem.Assign  
  Set myDelegate = MyItem.Recipients.Add("DL name")  
  myDelegate.Resolve  
  If myDelegate.Resolved Then  
    myItem.Subject = "Prepare Agenda For Meeting"  
    myItem.DueDate = Now + 30  
    myItem.Display  
    myItem.Send  
  End If  
End Sub