简体   繁体   English

使用Excel VBA发送Outlook电子邮件会生成错误:未定义用户定义的类型

[英]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.

相关问题 如何修复编译错误:在 Outlook 中使用 Excel VBA 时未定义用户定义的类型? - How to fix Compile Error: User-defined type not defined when using Excel VBA from Outlook? 在 Access VBA 中将变量声明为 Excel 应用程序生成错误:未定义用户定义的类型 - Declaring variable as Excel application in Access VBA generates error: User-defined type not defined Outlook 错误:未定义用户定义类型引用 Excel 工作簿 - Outlook Error: user-defined type not defined referring to Excel workbook 后期绑定编译错误:未定义引用Excel VBA中的Outlook mailitem的用户定义类型 - Late binding compile error: User-defined type not defined referencing Outlook mailitem in Excel VBA Excel VBA 编译抛出“未定义用户定义的类型”错误 - Excel VBA Compile throws a “User-defined type not defined” error Excel VBA On用户定义类型的错误处理 - Excel VBA On Error handling with User-Defined Type excel vba userforms:未定义的用户定义类型 - excel vba userforms: user-defined type not defined 使用 VBA 将 Outlook 用户定义字段导入 Excel - Import Outlook User-Defined Field to Excel with VBA excel-VBA 中的简单代码出现“未定义用户定义的类型”错误 - “User-defined type not defined” error for a simple code in excel-VBA 升级到Windows 10后,Excel VBA中出现“未定义用户定义类型”错误 - “User-defined type not defined” error in Excel VBA after upgrade to Windows 10
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM