vba - '编译错误: User-defined type not defined'

标签 vba excel compiler-errors outlook

这个问题在这里已经有了答案:





Getting user-defined type not defined error when running code

(2 个回答)


3年前关闭。




我在运行程序以通过 Outlook 发送电子邮件时收到标题中提到的错误。有人可以帮忙吗?它突出显示了第二行 -
enter image description here昏暗 olApp 作为 Outlook.Application

代码:

Sub send_mail(address As String, subject 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 = address
olMail.subject = "Maintenance Activity at: " & subject
olMail.Body = "Equipment to be maintained: " & mail_body
olMail.CC = Sheets("Expiry").Range("M8").Value
olMail.send

End Sub

Sub mass_mail()
n = Now()
MsgBox "date: " & n
If MsgBox("Are You Sure? ", vbYesNo, " Warning! ") = vbYes Then
Dim row_number As Integer
row_number = 4

Do
DoEvents

row_number = row_number + 1
If Range("D" & row_number).Value >= n And Range("D" & row_number).Value < (n + 30) Then
Call send_mail(Sheet1.Range("L" & row_number), Sheet1.Range("C" & row_number), Sheet1.Range("D" & row_number))
MsgBox "Date: " & Range("D" & row_number).Value
End If
Loop Until row_number = 10
MsgBox ("Messages(s) Sent!")
End If
End Sub

最佳答案

早期绑定(bind)库引用:

您需要在项目引用中添加对 Outlook 库的引用。

在 Visual Basic 编辑器中,您需要转到工具 > 引用并向下滚动以找到适用于您的 Office 版本的 Microsoft Outlook 对象库

对我来说,这是 16.0 版

example

后期绑定(bind):

或者,您可以将库调用转换为后期绑定(bind),这不需要添加库引用,并且在将应用程序分发给可能具有不同 Office 版本的不同用户时更安全,其中:

Set olApp = CreateObject("Outlook.Application")

早期与晚期绑定(bind)的信息:
  • Early and Late Binding (Visual Basic)
  • 关于vba - '编译错误: User-defined type not defined',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51905656/

    相关文章:

    c++ - 对类型值的引用绑定(bind)删除限定符 MULTISET

    vba - getAttribute ("value") 不适用于驱动程序 91.0.4472.19 和 Chrome 91.0.4472.77

    macos - 无法在Mac OS X上编译QEMU;说说关于uint16的先前声明

    excel - 当我从另一个宏 "Call"时宏不起作用,但是当我单独选择它时它确实起作用

    excel - 在Excel中为几年的每个月创建事件发生的条形图

    excel - VBA getelementsbytagname问题

    Excel VBA - 从网页中提取数据

    java - 为什么我不能在toString方法中返回有值(value)的字符串类型?

    vba - 如何在单独的工作表中找到特定的单元格值?

    excel - 如何在 VBA 中定义结构? Delphi "record"命令的替代方案