Hi all,
after hours of research in the web I finally need to ask here for your help: Following problem: I try to add a reference (Outlook library) in the Private Sub Workbook_Open() event (yes, it's in ThisWorkbook) since I need early binding for getting Outlook events later in different places. Once I open my workbook I get a compile error "user-defined type not defined" in my class module here --> Private olApp As New Outlook.Application. I understand this since the Outlook reference is not yet set and this is what I exactly try to do in the Workbook_Open event. How does Excel work here? Does it first try to compile the project BEFORE it executes code in the Workbook_Open event (enableEvents is set to True)?
Once I run the Sub Workbook_open manually, the reference is set without any problems.
Thanks in advance for your help.
Trying to add reference for MS Outlook Object Library when Workbook opens:
Private Sub Workbook_Open()
Dim strGUID As String, theRef As Variant, i As Long
'Update the GUID you need below.
strGUID = "{00062FFF-0000-0000-C000-000000000046}" 'MS Outlook x.x Object Library
'Set to continue in case of error
On Error Resume Next
'Remove any missing references
For i = thisWorkbook.VBProject.References.Count To 1 Step -1
Set theRef = thisWorkbook.VBProject.References.Item(i)
If theRef.isbroken = True Then
thisWorkbook.VBProject.References.Remove theRef
End If
Next i
'Clear any errors so that error trapping for GUID additions can be evaluated
Err.Clear
'Add the reference
thisWorkbook.VBProject.References.AddFromGuid _
GUID:=strGUID, Major:=1, Minor:=0
'If an error was encountered, inform the user
Select Case Err.Number
Case Is = 32813
'Reference already in use. No action necessary
MsgBox "Reference already in use"
Case Is = vbNullString
'Reference added without issue
MsgBox "Reference added"
Case Else
'An unknown error was encountered, so alert the user
MsgBox "A problem was encountered trying to" & vbNewLine _
& "add or remove a reference in this file" & vbNewLine & "Please check the " _
& "references in your VBA project!", vbCritical + vbOKOnly, "Error!"
End Select
On Error GoTo 0
End Sub
Here the Class Module "ClsAppoint" in which the compile error occurs once I open the workbook (red line):
Option Explicit
Private olApp As New Outlook.Application
Public WithEvents Appoint As Outlook.AppointmentItem
Private invite_sent As Boolean
Private Sub Class_Initialize()
Set Appoint = olApp.CreateItem(olAppointmentItem)
invite_sent = False
End Sub
Private Sub Appoint_Close(Cancel As Boolean)
Dim CloseAndDiscard As VbMsgBoxResult
If invite_sent Then Exit Sub
Appoint.GetInspector.WindowState = olMinimized
AppActivate Application.Caption
CloseAndDiscard = MsgBox("Close and discard?", vbYesNo + vbQuestion)
If CloseAndDiscard = vbYes Then
Appoint.Close olDiscard
Set invite = Nothing
Else
Cancel = True
Appoint.Display
Exit Sub
End If
End Sub
Private Sub Appoint_Send(Cancel As Boolean)
invite_sent = True
invite.Appoint.SaveAs save_email_folder & invite_name
Set invite = Nothing
End Sub
Bookmarks