Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting user-defined type not defined error when running code

Tags:

vba

outlook

Does anyone know why I'm getting a "user-defined type not defined" error in the Function GetOutlookApp() As Outlook.Application at the bottom of with this code?

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

' start Outlook app

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

' get worksheet range into an array in one go

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)

' loop through array and create tasks for each record

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
like image 432
Kim G Avatar asked Jun 02 '15 14:06

Kim G


People also ask

How do I fix variable not defined error in VBA?

This error has the following cause and solution: You used an Option Explicit statement to require the explicit declaration of variables, but you used a variable without declaring it. Explicitly declare the variable, or change the spelling of the variable to match that of the intended variable.

How do I fix type mismatch error in VBA?

Step 1: Write the subprocedure for VBA Type Mismatch. Step 2: Again assign a new variable, let's say “A” as Byte data type. Let's understand the Byte Data type here. Byte can only store the numerical value from 0 to 255.

How do you define a user defined type in VBA?

You create a user defined type using the Type statement. This combines multiple data types into a single data type. You define custom data types outside of procedures at the top of your module. Once you have created your type use the Dim statement to declare a variable of that type.


1 Answers

The How to automate Outlook from another program article describes all the required steps for automating Outlook. It states:

To use early binding, you first need to reference the available Outlook object library. To do this from Visual Basic (VB) or Visual Basic for Applications, follow these steps:

  1. In the Visual Basic Editor, on the Tools menu, click References.
  2. Click to select the Microsoft Outlook 15.0 Object Library check box, and then click OK.
like image 88
Eugene Astafiev Avatar answered Oct 13 '22 18:10

Eugene Astafiev