[英]Sending Outlook Email Using Excel VBA generates error: user-defined type not defined
I am trying to send email from Excel 2010 via Outlook. 我正在尝试通过Outlook从Excel 2010发送电子邮件。
Outlook is open and I have selected the reference to Outlook 14.0 Outlook已打开,我已选择对Outlook 14.0的引用
I created a macro called SendEmail: 我创建了一个名为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
I created another macro called SendMassEmail(): 我创建了另一个名为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
When I run the code it comes up with the following error: 当我运行代码时,它会出现以下错误:
user-defined type not defined
用户定义类型未定义
As you already checked the Microsoft Outlook 14.0 Object Library reference, 您已经检查了Microsoft Outlook 14.0对象库参考,
you could try late biding like this : 您可以尝试像这样延迟竞标:
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
I changed the olApp
creation, so that you don't create another instance of Outlook if you already have one running! 我更改了
olApp
创建方式,因此,如果您已经在运行另一个Outlook实例,则不要创建它! ;) ;)
You may use the following code, but you need to add reference to Microsoft Outlook 14.0 Object Library (Tools -> References...): 您可以使用以下代码,但需要添加对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
There is another way to do that without importing the library: 还有另一种方法可以不导入库:
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
I would suggest first solution. 我建议第一个解决方案。 Because then you are importing the library it works "better" and you can see a list of properties of the object every time you are working with Outlook objects.
因为这样您才能导入库,因此它会“更好”地工作,并且每次使用Outlook对象时,您都可以看到对象的属性列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.