Link to home
Start Free TrialLog in
Avatar of Kim Howard
Kim HowardFlag for United States of America

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.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
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

In Visual Basic select Debug|Compile Project. Does it? Is 'Session' defined somewhere?
Avatar of Kim Howard

ASKER

Session is not defined anywhere, but it does compile fine.
Avatar of Bill Prew
Bill Prew

Session is an Outlook defined object, it always exists.

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


»bp
Session is not defined anywhere
Well 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.
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
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.
Where are you running this, Outlook, Excel, Work, ???



»bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here are a couple of reference for the Outlook constant values...



»bp
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!
BTW, I checked all the Outlook constants and you were spot on!
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
Thanks for your help guys! My app is flying along swimmingly and I feel greatly enlightened!