我正尝试通过Outlook从Excel 2010发送电子邮件。
Outlook已打开,我已选择对Outlook 14.0的引用
我创建了一个名为SendEmail的宏:
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 = mail_body
olMail.Send
End Sub
我创建了另一个名为SendMassEmail()的宏:
Sub SendMassEmail()
row_number = 1
Do
DoEvents
row_number = row_number + 1
'MegBox (Sheet.Range("J3"))
Call SendEmail(Sheet1.Range("A" & row_number), "THis is a test email", Sheet1.Range("J2"))
Loop Until row_number = 4
'Range("A" & Rows.Count).End(xlUp).Row
End Sub
当我运行代码时,会出现以下错误:
未定义的用户定义类型
答案 0 :(得分:0)
正如您已经检查了 Microsoft Outlook 14.0对象库参考,
你可以尝试这样迟到:
Sub SendEmail(what_address As String, subject_line As String, mail_body As String)
Dim olApp As Object
Dim olMail As Object
On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number>0 Then Set olApp = CreateObject("Outlook.Application")
On Error Goto 0
Set olMail = olApp.CreateItem(0)
olMail.To = what_address
olMail.Subject = subject_line
olMail.body = mail_body
olMail.Send
End Sub
我更改了olApp
创建,因此如果您已经运行了一个Outlook,则不会创建另一个Outlook实例! ;)
答案 1 :(得分:0)
您可以使用以下代码,但需要添加对Microsoft Outlook 14.0对象库的引用(工具 - >引用...):
Sub SendEmail(what_address As String, subject_line As String, mail_body As String)
Dim olApp As Outlook.Application
Set olApp = New Outlook.Application
Dim olMail As Outlook.MailItem
Set olMail = olApp.CreateItem(olMailItem)
olMail.To = what_address
olMail.Subject = subject_line
olMail.body = mail_body
olMail.Send
End Sub
如果不导入库,还有另一种方法:
Sub SendEmail(what_address As String, subject_line As String, mail_body As String)
Dim olApp As Object
Set olApp = CreateObject("Outlook.Application")
Dim olMail As Object
Set olMail = olApp.CreateItem(olMailItem)
olMail.To = what_address
olMail.Subject = subject_line
olMail.body = mail_body
olMail.Send
End Sub
我建议先解决。因为您正在导入库它“更好”,并且每次使用Outlook对象时都可以看到对象的属性列表。