运行代码时,获取用户定义的类型未定义错误

时间:2015-06-02 14:21:47

标签: vba outlook outlook-vba

有谁知道我为什么会得到一个未定义的"用户定义类型"使用此代码在底部的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

2 个答案:

答案 0 :(得分:12)

How to automate Outlook from another program文章介绍了自动执行Outlook所需的所有步骤。它声明:

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

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

答案 1 :(得分:1)

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

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