Kim Howard
asked on
Trying to switch to Late Binding - Error "Object Required"
I have some VBA code that I got off the web that has worked successfully, but now I am trying to change my code to late binding. I can't figure out what this error is and have spent a significant amount of time with web searches and I still can't make sense of it. I have got the line flagged below. I would appreciate it if someone could look over the whole thing and make sure switching to late binding is not going to cause more errors once I get past this one. The code works w/o late binding, so it is just the late binding part I need someone to look over. I would be most appreciative!!!
This is the code:
Sub MultiCalendars(ByVal strCalendar As String, strAttendees As String, strColorCategory As String, dtStartDate As Date, dtEndDate As Date, Optional strReminderText As String, Optional strLocation As String, Optional decDuration As Single)
Dim objApp As Object
Dim objPane As Object
Dim objModule As Object
Dim objGroup As Object
Dim objNavFolder As Object
Dim mtgAttendee As Object
Dim olFolderCalendar As Object
Dim calItem As Object
Dim i As Integer
Dim ynApptSet As Boolean
'Const objFolder As Long = 2
'Const olFolderCalendar As Long = 9
'Const olAppointmentItem As Long = 1
Set objApp = CreateObject("Outlook.Appl ication")
ynApptSet = False
**************** It is erroring on the following line of code, with the error "Object Required. ************************
Set objApp.ActiveExplorer.Curr entFolder = Session.GetDefaultFolder(o lFolderCal endar)
DoEvents
Set objPane = oApp.ActiveExplorer.Naviga tionPane
Set objModule = objPane.Modules.GetNavigat ionModule( olModuleCa lendar)
With objModule.NavigationGroups
Set objGroup = .GetDefaultNavigationGroup (olMyFolde rsGroup)
'To use a different calendar group
'Set objGroup = .Item("Shared Calendars")
End With
'strCalendarPath, strRecipients, strColor, dtStartDate, dtEndDate, strReminderText, strLocation, intMinutes
For i = 1 To objGroup.NavigationFolders .Count
If (objGroup.NavigationFolder s.Item(i). Folder.Ful lFolderPat h = strCalendar) Then
Set objNavFolder = objGroup.NavigationFolders .Item(i)
Set calItem = objNavFolder.Folder.Items. Add(olAppo intmentIte m)
calItem.MeetingStatus = olMeeting
calItem.Categories = strColorCategory
calItem.Subject = strReminderText
calItem.Location = strLocation
calItem.Start = dtStartDate
calItem.End = dtEndDate
'calItem.Duration = decDuration
If Len(strAttendees) > 0 Then
Set mtgAttendee = calItem.Recipients.Add(str Attendees)
mtgAttendee.Type = olRequired
End If
calItem.Save
If (calItem.Recipients.Resolv eAll) Then
calItem.Send
Else
calItem.Display
End If
ynApptSet = True
End If
Next
If ynApptSet = True Then
MsgBox "Your reminder has been added"
Else
MsgBox "***** YOUR APPOINTMENT FAILED ******"
End If
Set objApp = Nothing
'Set objPane = Nothing
'Set objModule = Nothing
'Set objGroup = Nothing
'Set objNavFolder = Nothing
'Set calItem = Nothing
'Set mtgAttendee = Nothing
End Sub
This is the code:
Sub MultiCalendars(ByVal strCalendar As String, strAttendees As String, strColorCategory As String, dtStartDate As Date, dtEndDate As Date, Optional strReminderText As String, Optional strLocation As String, Optional decDuration As Single)
Dim objApp As Object
Dim objPane As Object
Dim objModule As Object
Dim objGroup As Object
Dim objNavFolder As Object
Dim mtgAttendee As Object
Dim olFolderCalendar As Object
Dim calItem As Object
Dim i As Integer
Dim ynApptSet As Boolean
'Const objFolder As Long = 2
'Const olFolderCalendar As Long = 9
'Const olAppointmentItem As Long = 1
Set objApp = CreateObject("Outlook.Appl
ynApptSet = False
**************** It is erroring on the following line of code, with the error "Object Required. ************************
Set objApp.ActiveExplorer.Curr
DoEvents
Set objPane = oApp.ActiveExplorer.Naviga
Set objModule = objPane.Modules.GetNavigat
With objModule.NavigationGroups
Set objGroup = .GetDefaultNavigationGroup
'To use a different calendar group
'Set objGroup = .Item("Shared Calendars")
End With
'strCalendarPath, strRecipients, strColor, dtStartDate, dtEndDate, strReminderText, strLocation, intMinutes
For i = 1 To objGroup.NavigationFolders
If (objGroup.NavigationFolder
Set objNavFolder = objGroup.NavigationFolders
Set calItem = objNavFolder.Folder.Items.
calItem.MeetingStatus = olMeeting
calItem.Categories = strColorCategory
calItem.Subject = strReminderText
calItem.Location = strLocation
calItem.Start = dtStartDate
calItem.End = dtEndDate
'calItem.Duration = decDuration
If Len(strAttendees) > 0 Then
Set mtgAttendee = calItem.Recipients.Add(str
mtgAttendee.Type = olRequired
End If
calItem.Save
If (calItem.Recipients.Resolv
calItem.Send
Else
calItem.Display
End If
ynApptSet = True
End If
Next
If ynApptSet = True Then
MsgBox "Your reminder has been added"
Else
MsgBox "***** YOUR APPOINTMENT FAILED ******"
End If
Set objApp = Nothing
'Set objPane = Nothing
'Set objModule = Nothing
'Set objGroup = Nothing
'Set objNavFolder = Nothing
'Set calItem = Nothing
'Set mtgAttendee = Nothing
End Sub
In Visual Basic select Debug|Compile Project. Does it? Is 'Session' defined somewhere?
ASKER
Session is not defined anywhere, but it does compile fine.
Session is an Outlook defined object, it always exists.
Application.Session is the same as Application.GetNamespace(" MAPI").
»bp
Application.Session is the same as Application.GetNamespace("
»bp
Session is not defined anywhereWell then that's the cause of your problem since VBA has no way to know what it is.
Session is an Outlook defined object, it always exists.I'm sure that's true with early binding but I have doubts about late binding.
ASKER
Based on your comment and some more web searches, I tried this:
Dim objNameSpace As Object
Dim i As Integer
Dim ynApptSet As Boolean
Set objApp = CreateObject("Outlook.Appl ication")
Set objNameSpace = objApp.GetNamespace("MAPI" )
ynApptSet = False
Set objApp.ActiveExplorer.Curr entFolder = objNameSpace.GetDefaultFol der(olFold erCalendar )
and this:
Dim objNameSpace As Object
Dim i As Integer
Dim ynApptSet As Boolean
Set objApp = CreateObject("Outlook.Appl ication")
Set objNameSpace = objApp.GetNamespace("MAPI" )
ynApptSet = False
Set objApp.ActiveExplorer.Curr entFolder = objNameSpace.Session.GetDe faultFolde r(olFolder Calendar)
Both give me an error "Type Mismatch".
I obviously have no clue what I am doing, but hope that this process will help me learn. I appreciate your input.
Kim
Dim objNameSpace As Object
Dim i As Integer
Dim ynApptSet As Boolean
Set objApp = CreateObject("Outlook.Appl
Set objNameSpace = objApp.GetNamespace("MAPI"
ynApptSet = False
Set objApp.ActiveExplorer.Curr
and this:
Dim objNameSpace As Object
Dim i As Integer
Dim ynApptSet As Boolean
Set objApp = CreateObject("Outlook.Appl
Set objNameSpace = objApp.GetNamespace("MAPI"
ynApptSet = False
Set objApp.ActiveExplorer.Curr
Both give me an error "Type Mismatch".
I obviously have no clue what I am doing, but hope that this process will help me learn. I appreciate your input.
Kim
I know nothing about Outlook programming but try doing this after the Set statement for objApp.
Dim objSession as Object
Set objSession = objApp.Session
and then use objSession rather than Session.
Dim objSession as Object
Set objSession = objApp.Session
and then use objSession rather than Session.
Where are you running this, Outlook, Excel, Work, ???
»bp
»bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here are a couple of reference for the Outlook constant values...
»bp
»bp
ASKER
Bill,
Thanks so much for your help!!! I was led astray by several posts on the web that said you only need to change the first few lines of the routine when switching from Early Binding to Late Binding.
The line originally looked like this:
Set Outlook.ActiveExplorer.Cur rentFolder = Session.GetDefaultFolder(o lFolderCal endar)
It was obvious to change Outlook to objApp, but not to put objApp in front of Session. Can you tell me why it is needed now and not before?
I put Option Explicit at the top of my code and will check and make sure it is everywhere else.
Thanks!
Thanks so much for your help!!! I was led astray by several posts on the web that said you only need to change the first few lines of the routine when switching from Early Binding to Late Binding.
The line originally looked like this:
Set Outlook.ActiveExplorer.Cur
It was obvious to change Outlook to objApp, but not to put objApp in front of Session. Can you tell me why it is needed now and not before?
I put Option Explicit at the top of my code and will check and make sure it is everywhere else.
Thanks!
ASKER
BTW, I checked all the Outlook constants and you were spot on!
Good, glad to hear you are making some progress.
The pre-defined Application object always refers to the current application that started the VBA code, be it Access, Excel, Word, Outlook, ... And you can often omit that from statements like yours and VBA defaults to the current Application. When you use automation to control a different application, then you need to be very specific about the Application object you want to reference. Let's say you are running some VBA code in Excel, but have launched Outlook and are automating it. There are actually two Application objects active at that point, one for Excel and one for Outlook. "Application" will always refer to the Excel one since that is the native environment the VBA was executed from. "objApp" will be the Application object of the Outlook session, so when you want to refer to it you need to explicitly include it in the code.
Hope this helps a little.
»bp
The line originally looked like this:
Set Outlook.ActiveExplorer.CurrentFolder = Session.GetDefaultFolder(o lFolderCal endar)
It was obvious to change Outlook to objApp, but not to put objApp in front of Session. Can you tell me why it is needed now and not before?
The pre-defined Application object always refers to the current application that started the VBA code, be it Access, Excel, Word, Outlook, ... And you can often omit that from statements like yours and VBA defaults to the current Application. When you use automation to control a different application, then you need to be very specific about the Application object you want to reference. Let's say you are running some VBA code in Excel, but have launched Outlook and are automating it. There are actually two Application objects active at that point, one for Excel and one for Outlook. "Application" will always refer to the Excel one since that is the native environment the VBA was executed from. "objApp" will be the Application object of the Outlook session, so when you want to refer to it you need to explicitly include it in the code.
Hope this helps a little.
»bp
ASKER
Thanks for your help guys! My app is flying along swimmingly and I feel greatly enlightened!