Activate Excel

jeremyjohnolson

Board Regular
Joined
Apr 29, 2014
Messages
53
I have the following code that is in Excel VBA. It gets emails conversation topics from emails in Outlook and pastes them into Excel. Everything works fine, except right after the line "Set Folder = nms.PickFolder" the screen displays Outlook and I want it to come back to the Excel file afterwards. So after Outlook is displayed on screen, what code do I need to put in my VBA macro to make it go back to showing Excel?

Code:
Option Explicit

Sub Download_Outlook_Mail_To_Excel2()
    'Add Tools->References->"Microsoft Outlook nn.n Object Library"
    'nn.n varies as per our Outlook Installation
    Dim appOutlook As Outlook.Application
    Dim nms As Outlook.Namespace
    Dim Folder As Outlook.MAPIFolder
    Dim iRow As Integer
    Dim oRow As Integer
    Dim EndSel As Range
    


    Set appOutlook = GetObject(, "Outlook.Application")
    Set EndSel = Selection
    Set nms = appOutlook.GetNamespace("MAPI")
 
    Set Folder = nms.PickFolder


    '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"
    
    'Insert Column Headers
    Worksheets("test").Cells(1, 1) = "Conversation Topic"
    
    'Insert Mail Data
    For iRow = 1 To Folder.Items.Count
        oRow = iRow + 1
        Worksheets("test").Cells(oRow, 1).Select
        Worksheets("test").Cells(oRow, 1) = Folder.Items.Item(iRow).ConversationTopic
    Next iRow
    
    EndSel.Select
    
    MsgBox "Outlook Mails Extracted to Excel"


 
JumpExit:
    Set nms = Nothing
    Set Folder = Nothing
    Exit Sub


End Sub
 
Last edited:

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
See the AppActivate statement.
 
Upvote 0
Thanks shg. It took a bit of poking around, but I got it to work with this:

AppActivate ActiveWorkbook.Name

So this is what the code looks like now:

Code:
Option Explicit

Sub Download_Outlook_Mail_To_Excel2()
    'Add Tools->References->"Microsoft Outlook nn.n Object Library"
    'nn.n varies as per our Outlook Installation
    Dim appOutlook As Outlook.Application
    Dim nms As Outlook.Namespace
    Dim Folder As Outlook.MAPIFolder
    Dim EndSel As Range
    Dim iRow As Integer
    Dim oRow As Integer
    
    Set appOutlook = GetObject(, "Outlook.Application")
    Set nms = appOutlook.GetNamespace("MAPI")
    Set Folder = nms.PickFolder
    Set EndSel = Selection
    
    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"
    
    'Insert Column Headers
    Worksheets("test").Cells(1, 1) = "Conversation Topic"
    
    'Insert Mail Data
    For iRow = 1 To Folder.Items.Count
        oRow = iRow + 1
        Worksheets("test").Cells(oRow, 1).Select
        Worksheets("test").Cells(oRow, 1) = Folder.Items.Item(iRow).ConversationTopic
    Next iRow
    
    EndSel.Select
    
    MsgBox "Outlook Mails Extracted to Excel"


 
JumpExit:
    Set nms = Nothing
    Set Folder = Nothing
    Exit Sub


End Sub
 
Upvote 0
Good job, you're welcome.
 
Upvote 0

Forum statistics

Threads
1,216,739
Messages
6,132,439
Members
449,728
Latest member
teodora bocarski

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