Link to home
Start Free TrialLog in
Avatar of Dale James
Dale James

asked on

Excel VBA: Convert from Early Binding to Late Binding

Hello Team,

I have inherited the follow code which is used to record the details contained within each email received into the Plan Setup mailbox.

The code is currently coded for early binding but can you please advise where and what equivalent code should be used to convert to late binding?

Sub PlanSetup()


Dim OutApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Dim OutApp As Outlook.Application
Dim FlagCol As String
Dim fldSpamDigest As Outlook.MAPIFolder
   
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI"


Set Folder = OutApp.GetNamespace("MAPI").Folders("Plansetup").Folders("Inbox") '.Folders("1 Plan Setup Enquiries")




Sheets(1).Select


i = 1


For Each OutlookMail In Folder.Items
   
Range("A1").Offset(i, 0).Value = OutlookMail.Subject
       
       
       
        If InStr(1, OutlookMail.Subject, "Recall") > 0 Or InStr(1,    OutlookMail.Subject, "Undeliver") > 0 Or InStr(1,             OutlookMail.Subject, "delay") > 0 Then
       


       
        Range("B1").Offset(i, 0).Value = "=Today()"
        Range("C1").Offset(i, 0).Value = "Recall"
        Range("D1").Offset(i, 0).Value = OutlookMail.UnRead
        Range("E1").Offset(i, 0).Value = 0
 
       
    Else
       
        Range("B1").Offset(i, 0).Value = OutlookMail.ReceivedTime
        Range("C1").Offset(i, 0).Value = OutlookMail.SenderName
        Range("D1").Offset(i, 0).Value = OutlookMail.UnRead
        Range("E1").Offset(i, 0).Value = OutlookMail.FlagStatus
    
    End If
       
    i = i + 1
    Next OutlookMail


Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing




End Sub

Open in new window


Thank you in advance.

Sincerely

Dale

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
Avatar of Dale James
Dale James

ASKER

Hello Bill

Thank you very much for your response and solution.

I will get a chance to apply the code change in the morning when I have access to the network again.

Will provide a further update when completed.

Sincerely

Dale
And just as a tip, I often include the code for both early and late binding in the VBA macro.  Typically I can then deploy in late binding mode, but toggle into early binding mode for development.  The advantage there in early binding mode is the intellisense works and can save time in coding where object properties and methods are shown automatically in the editor.

Here's an example of using a compiler directive to specify the binding mode (early or late) you want to work in at that time.

' Specify if early or late binding to be used
#Const EarlyBinding = False

Sub PlanSetup()

    ' Local Variables
#If EarlyBinding Then
    Dim OutApp As Outlook.Application
    Dim OutlookNamespace As Namespace
    Dim Folder As MAPIFolder
    Dim fldSpamDigest As Outlook.MAPIFolder
#Else
    Dim OutApp As Object
    Dim OutlookNamespace As Object
    Dim Folder As Object
    Dim fldSpamDigest As Object
#End If
    Dim OutlookMail As Variant
    Dim FlagCol As String
   
#If EarlyBinding Then
    Set OutlookApp = New Outlook.Application
#Else
    Set OutlookApp = CreateObject("Outlook.Application")
#End If

    Sheets(1).Select
    
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set Folder = OutApp.GetNamespace("MAPI").Folders("Plansetup").Folders("Inbox") '.Folders("1 Plan Setup Enquiries")
    
    For Each OutlookMail In Folder.Items
        Range("A1").Offset(i, 0).Value = OutlookMail.Subject
        If InStr(1, OutlookMail.Subject, "Recall") > 0 Or InStr(1, OutlookMail.Subject, "Undeliver") > 0 Or InStr(1, OutlookMail.Subject, "delay") > 0 Then
            Range("B1").Offset(i, 0).Value = "=Today()"
            Range("C1").Offset(i, 0).Value = "Recall"
            Range("D1").Offset(i, 0).Value = OutlookMail.UnRead
            Range("E1").Offset(i, 0).Value = 0
        Else
            Range("B1").Offset(i, 0).Value = OutlookMail.ReceivedTime
            Range("C1").Offset(i, 0).Value = OutlookMail.SenderName
            Range("D1").Offset(i, 0).Value = OutlookMail.UnRead
            Range("E1").Offset(i, 0).Value = OutlookMail.FlagStatus
        End If
    Next OutlookMail
    
    Set Folder = Nothing
    Set OutlookNamespace = Nothing
    Set OutlookApp = Nothing

End Sub

Open in new window


»bp
Hello Bill

Thank you very much for the additional detail.
Using both early and late binding is an option that didn't occur to me, so appreciate that very much and for the code you have provided as an example.

Also, the initial code you provided for the late binding worked perfectly!

Thank you once again for all your assistance.

Sincerely
Dale
Welcome, glad that was useful, and thanks for the feedback.

»bp