Late Binding

jeremyjohnolson

Board Regular
Joined
Apr 29, 2014
Messages
53
Does anyone know how to change the below code to late binding as to not have to have a user enable the Outlook object library reference?

Code:
Sub Download_Outlook_Mail_To_Excel3()
    'Add Tools->References->"Microsoft Outlook nn.n Object Library"
    'nn.n varies as per our Outlook Installation
    Dim WksName As String
    WksName = "Macro" '****name of worksheet to put data****


    Dim appOutlook As Outlook.Application
    Dim nms As Outlook.Namespace
    Dim Folder As Outlook.MAPIFolder
    Dim iRow As Integer
    Dim oRow As Integer
    Dim nEmails As Integer
    Dim nConvos As Integer
    
    Set appOutlook = GetObject(, "Outlook.Application")
    Set nms = appOutlook.GetNamespace("MAPI")
    Set Folder = nms.PickFolder
        
    AppActivate ActiveWorkbook.Name


    'Handle potential errors with Select Folder dialog box.
    If Folder Is Nothing Then
        MsgBox "There are no mail messages to export", vbOKOnly, "Error"
        GoTo JumpExit
    ElseIf Folder.DefaultItemType <> olMailItem Then
        MsgBox "These are not Mail Items", vbOKOnly, "Error"
        GoTo JumpExit
    ElseIf Folder.Items.Count = 0 Then
        MsgBox "There are no mail messages to export", vbOKOnly, "Error"
        GoTo JumpExit
    End If


    'Read Through each Mail and export the details to Excel for Email Archival
    Folder.Items.Sort "Received"
    
    'Clear old data
    Worksheets(WksName).Cells(1, 1).EntireColumn.Clear
    
    'Insert Column Headers
    Worksheets(WksName).Cells(1, 1) = "Conversation Topics:"
    
    'Insert Mail Data
    For iRow = 1 To Folder.Items.Count
        oRow = iRow + 1
        Worksheets(WksName).Cells(oRow, 1) = Folder.Items.Item(iRow).ConversationTopic
    Next iRow
    
    'put number of emails on sheet
    nEmails = Worksheets(WksName).Cells(1, 1).EntireColumn.Cells.SpecialCells(xlCellTypeConstants).Count - 1
    Worksheets(WksName).Cells(2, 3).Value = nEmails
    
    'Remove duplicates
    Worksheets(WksName).Cells(1, 1).EntireColumn.RemoveDuplicates Columns:=1, Header:=xlYes
    
    'put number of conversations on sheet
    nConvos = Worksheets(WksName).Cells(1, 1).EntireColumn.Cells.SpecialCells(xlCellTypeConstants).Count - 1
    Worksheets(WksName).Cells(2, 4).Value = nConvos
    
    'Formatting & hide tab
    Worksheets(WksName).Cells(1, 1).EntireColumn.AutoFit
    Worksheets(WksName).Cells(1, 1).Font.Underline = xlUnderlineStyleSingle
    Worksheets(WksName).Visible = True
'    Worksheets(WksName).Visible = xlSheetVeryHidden
    
    MsgBox "Outlook Mails Extracted to Excel"
 
JumpExit:
    Set nms = Nothing
    Set Folder = Nothing
    Exit Sub


End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Change these variables to be of Object type:

Rich (BB code):
    Dim appOutlook As Outlook.Application
    Dim nms As Outlook.Namespace
    Dim Folder As Outlook.MAPIFolder

Instead do this:

Rich (BB code):
    Dim appOutlook As Object    ' Outlook.Application
    Dim nms As Object           ' Outlook.Namespace
    Dim Folder As Object        ' Outlook.MAPIFolder

You will also need to hard-code the enumerations. You can get the values from the help file, or using the Immediate window Debug.Print. For example, this line:

Rich (BB code):
    ElseIf Folder.DefaultItemType <> olMailItem Then

becomes this:

Rich (BB code):
    ElseIf Folder.DefaultItemType <> 0 Then    ' OlItemType.olMailItem = 0

I strongly recommend you use comments (in green above) to keep track of what library object each variable actually represents. Makes your life a lot easier when modifying, troubleshooting, or debugging later on.
 
Last edited:
Upvote 0
Thank you so much, this is awesome! You are a tremendous help...I will test it out later and let you know if I have any questions.
 
Upvote 0
It works! Thank you. Here is the new code

Code:
Option Explicit

Sub EmailConvos(control As IRibbonControl)
   
   Application.ScreenUpdating = False
    
    'Add Tools->References->"Microsoft Outlook nn.n Object Library"
    'nn.n varies as per our Outlook Installation
    Dim WksName As String
    WksName = "Macro" '****name of worksheet to put data****


    Dim DestCell As Range
    Dim appOutlook As Object 'Outlook.Application
    Dim nms As Object 'Outlook.Namespace
    Dim Folder As Object 'Outlook.MAPIFolder
    Dim EndDate As Date
    Dim BegDate As Date
    Dim iTims As Object 'Outlook.Items
    Dim iRow As Integer
    Dim oRow As Integer
    Dim nEmails As Integer
    Dim nConvos As Integer
    
    On Error Resume Next
    Set DestCell = Application.InputBox(Prompt:="Please use mouse to select destination cell.", _
        Title:="Destination Cell", Default:=ActiveCell.Address, Type:=8)
    On Error GoTo 0
    If DestCell Is Nothing Then
        Exit Sub
    Else
        Set appOutlook = GetObject(, "Outlook.Application")
        Set nms = appOutlook.GetNamespace("MAPI")
        Set Folder = nms.PickFolder
        EndDate = ActiveSheet.Range("EndDate").Value + 1
        BegDate = EndDate - 6
        Set iTims = Folder.Items.Restrict("[SentOn] > '" & BegDate & "' And [SentOn]<'" & EndDate & "'")
            
        'Make screen go back to showing Excel after picking Outlook folder
        AppActivate ActiveWorkbook.Name
    
        'Handle potential errors with Select Folder dialog box.
        If Folder Is Nothing Then
            MsgBox "There are no mail messages to export", vbOKOnly, "Error"
            GoTo JumpExit
        ElseIf Folder.DefaultItemType <> 0 Then
            MsgBox "These are not Mail Items", vbOKOnly, "Error"
            GoTo JumpExit
        ElseIf Folder.Items.Count = 0 Then
            MsgBox "There are no mail messages to export", vbOKOnly, "Error"
            GoTo JumpExit
        End If
    
        'Read Through each Mail and export the details to Excel for Email Archival
        Folder.Items.Sort "Received"
        
        'Clear old data
        Worksheets(WksName).Cells(1, 1).EntireColumn.Clear
        Worksheets(WksName).Cells(1, 2).EntireColumn.Clear
        
        'Insert Column Headers
        Worksheets(WksName).Cells(1, 1) = "Conversation Topics:"
        Worksheets(WksName).Cells(1, 2) = "Sent Date:"
        
        'Insert Mail Data
        For iRow = 1 To iTims.Count
            oRow = iRow + 1
            Worksheets(WksName).Cells(oRow, 2) = iTims.Item(iRow).SentOn
            Worksheets(WksName).Cells(oRow, 1) = iTims.Item(iRow).ConversationTopic
        Next iRow
        
        'Put EndDate and BegDate on sheet
        Worksheets(WksName).Cells(5, 4).Value = BegDate
        Worksheets(WksName).Cells(5, 5).Value = EndDate
        
        'put number of emails on sheet
        nEmails = Worksheets(WksName).Cells(1, 1).EntireColumn.Cells.SpecialCells(xlCellTypeConstants).Count - 1
        Worksheets(WksName).Cells(2, 4).Value = nEmails
        
        'Remove duplicates
        Worksheets(WksName).Range("A:B").RemoveDuplicates Columns:=1, Header:=xlYes
        
        'put number of conversations on sheet
        nConvos = Worksheets(WksName).Cells(1, 1).EntireColumn.Cells.SpecialCells(xlCellTypeConstants).Count - 1
        Worksheets(WksName).Cells(2, 5).Value = nConvos
        DestCell.Value = nConvos
        
        'Formatting & hide tab
        Worksheets(WksName).Cells(1, 1).Font.Underline = xlUnderlineStyleSingle
        Worksheets(WksName).Cells(1, 2).Font.Underline = xlUnderlineStyleSingle
        Worksheets(WksName).Range("A:E").EntireColumn.AutoFit
'        Worksheets(WksName).Visible = True
        Worksheets(WksName).Visible = xlSheetVeryHidden
        Application.ScreenUpdating = True
    End If
 
JumpExit:
    Set nms = Nothing
    Set Folder = Nothing
    Application.ScreenUpdating = True
    Exit Sub


End Sub
 
Upvote 0
So this is a little odd, I finished the macro which is saved in the workbook that is saved on my company's network drive...when I go to open the workbook on another computer I see the iribbon button but it will not execute the macro. It even shows up in VBE but won't execute???
 
Upvote 0
I recently had a very similar issue. Try going to Excel Options -> Add-ins. At the bottom of the window, in the Manage drop-down, choose Disabled Items and press Go. See if your workbook is on that list.
 
Upvote 0
I think I need to add my certificate to the trusted certs of my buddies computer for it to work on his. I will try this tomorrow when he is back in.
 
Upvote 0

Forum statistics

Threads
1,215,036
Messages
6,122,794
Members
449,095
Latest member
m_smith_solihull

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top