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.Application")
     
    ynApptSet = False
   
  **************** It is erroring on the following line of code, with the error "Object Required. ************************        
    Set objApp.ActiveExplorer.CurrentFolder = Session.GetDefaultFolder(olFolderCalendar)
    DoEvents

    Set objPane = oApp.ActiveExplorer.NavigationPane
    Set objModule = objPane.Modules.GetNavigationModule(olModuleCalendar)
     
    With objModule.NavigationGroups
        Set objGroup = .GetDefaultNavigationGroup(olMyFoldersGroup)

      '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.NavigationFolders.Item(i).Folder.FullFolderPath = strCalendar) Then
            Set objNavFolder = objGroup.NavigationFolders.Item(i)
            Set calItem = objNavFolder.Folder.Items.Add(olAppointmentItem)
            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(strAttendees)
                mtgAttendee.Type = olRequired
            End If
            calItem.Save
            If (calItem.Recipients.ResolveAll) 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
Kim HowardAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
In Visual Basic select Debug|Compile Project. Does it? Is 'Session' defined somewhere?
0
Kim HowardAuthor Commented:
Session is not defined anywhere, but it does compile fine.
0
Bill PrewCommented:
Session is an Outlook defined object, it always exists.

Application.Session is the same as Application.GetNamespace("MAPI").


»bp
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Martin LissOlder than dirtCommented:
Session is not defined anywhere
Well then that's the cause of your problem since VBA has no way to know what it is.
0
Martin LissOlder than dirtCommented:
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.
0
Kim HowardAuthor Commented:
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.Application")
    Set objNameSpace = objApp.GetNamespace("MAPI")
    ynApptSet = False
                     
    Set objApp.ActiveExplorer.CurrentFolder = objNameSpace.GetDefaultFolder(olFolderCalendar)

and this:    
    Dim objNameSpace As Object
   
    Dim i As Integer
    Dim ynApptSet As Boolean
   
    Set objApp = CreateObject("Outlook.Application")
    Set objNameSpace = objApp.GetNamespace("MAPI")
    ynApptSet = False
                     
    Set objApp.ActiveExplorer.CurrentFolder = objNameSpace.Session.GetDefaultFolder(olFolderCalendar)



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
0
Martin LissOlder than dirtCommented:
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.
0
Bill PrewCommented:
Where are you running this, Outlook, Excel, Work, ???



»bp
0
Bill PrewCommented:
Sorry, getting caught up here.  In your original code, you can change:

    Set objApp.ActiveExplorer.CurrentFolder = Session.GetDefaultFolder(olFolderCalendar)

to:

    Set objApp.ActiveExplorer.CurrentFolder = objApp.Session.GetDefaultFolder(olFolderCalendar)

to resolve your original error.


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Bill PrewCommented:
To solve the next error you will get, the type mismatch, you need to define the olFolderCalendar constant used in that same statement.  Any outlook constants like that will need to be defined by you locally to support late binding.  In early binding they are automatically included when you add the Reference.  So add:

Const olFolderCalendar = 9


»bp
0
Bill PrewCommented:
Next thing I see:

    Set objPane = oApp.ActiveExplorer.NavigationPane

should be:

    Set objPane = objApp.ActiveExplorer.NavigationPane


»bp
0
Bill PrewCommented:
It looks like you may have created some of these constants as variables, you will have to remove the DIM's and add constants for each. I think these are the right values, feel free to check me with google...

Const olFolderCalendar = 9
Const olAppointmentItem = 1
Const olModuleCalendar = 1
Const olMyFoldersGroup = 1
Const olMeeting = 1
Const olRequired = 1


»bp
0
Bill PrewCommented:
One other thing, add the following line at the beginning of the module this code lives in to force the compiler to check for undefined constants and variables, it will save you a lot of pain trying to find them debugging the code.

Option Explicit


»bp
0
Bill PrewCommented:
Here are a couple of reference for the Outlook constant values...



»bp
0
Kim HowardAuthor Commented:
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.CurrentFolder = Session.GetDefaultFolder(olFolderCalendar)

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!
0
Kim HowardAuthor Commented:
BTW, I checked all the Outlook constants and you were spot on!
0
Bill PrewCommented:
Good, glad to hear you are making some progress.

The line originally looked like this:
Set Outlook.ActiveExplorer.CurrentFolder = Session.GetDefaultFolder(olFolderCalendar)

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
0
Kim HowardAuthor Commented:
Thanks for your help guys! My app is flying along swimmingly and I feel greatly enlightened!
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VBA

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.