【问题标题】:Getting user-defined type not defined error when running code运行代码时出现用户定义类型未定义错误
【发布时间】:2021-11-10 14:47:49
【问题描述】:

有谁知道为什么我在这段代码底部的Function GetOutlookApp() As Outlook.Application 中收到“未定义用户定义的类型”错误?

Sub CreateAppointments()

Dim cell As Excel.Range
Dim rng As Excel.Range
Dim wholeColumn As Excel.Range
Dim startingCell As Excel.Range
Dim oApp As Outlook.Application
Dim tsk As Outlook.TaskItem
Dim wkbk As Excel.Workbook
Dim wksht As Excel.Worksheet
Dim lastRow As Long
Dim arrData As Variant
Dim i As Long

' 启动 Outlook 应用程序

Set oApp = GetOutlookApp
If oApp Is Nothing Then
  MsgBox "Could not start Outlook.", vbInformation
  Exit Sub
End If

' 将工作表范围一次性放入数组中

Set wkbk = ActiveWorkbook
Set wksht = wkbk.ActiveSheet
Set wholeColumn = wksht.Range("B:B")
lastRow = wholeColumn.End(xlDown).Row - 2
Set startingCell = wksht.Range("B2")
Set rng = wksht.Range(startingCell, startingCell.Offset(lastRow, 1))
arrData = Application.Transpose(rng.Value)

' 遍历数组并为每条记录创建任务

For i = LBound(arrData, 2) To UBound(arrData, 2)
  Set tsk = oApp.CreateItem(olTaskItem)
  With tsk
    .DueDate = arrData(2, i)
    .Subject = arrData(1, i)
    .Save
  End With
Next I

End Sub

Function GetOutlookApp() As Outlook.Application
On Error Resume Next
Set GetOutlookApp = CreateObject("Outlook.Application")

End Function

【问题讨论】:

标签: vba outlook


【解决方案1】:

How to automate Outlook from another program 文章介绍了自动化 Outlook 所需的所有步骤。它指出:

要使用早期绑定,您首先需要引用可用的 Outlook 对象库。要从 Visual Basic (VB) 或 Visual Basic for Applications 执行此操作,请执行以下步骤:

  1. 在 Visual Basic 编辑器的“工具”菜单上,单击“引用”。
  2. 单击以选中 Microsoft Outlook 15.0 对象库复选框,然后单击确定。

【讨论】:

【解决方案2】:

我在脚本 VBA Excel 中使用 Outlook 时遇到了同样的问题,我选择了:

工具 > 参考 > 勾选“Microsoft Outlook 15.0 对象库”前面的复选框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多