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