list opened outlook mail items VBA

RAYLWARD102

Well-known Member
Joined
May 27, 2010
Messages
529
Was wondering if it's possible to generate a list of emails that are opened in outlook. With that list; I want to later close those emails and re-open the emails after the computer restarts. 20-30 emails

macro A gets the list and closes the email.
macro B re-opens the closed emails
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
For macro A try:

Code:
Sub Test()
    Dim Sh As Worksheet
    Dim olApp As Object
    Dim r As Long
    Dim olInspector As Object
    Dim olItem As Object
    Set Sh = Worksheets("Sheet1")
    Set olApp = GetObject(, "Outlook.Application")
    r = 1
    For Each olInspector In olApp.inspectors
        Set olItem = olInspector.CurrentItem
        Sh.Cells(r, 1).Value = olItem.SenderName
        Sh.Cells(r, 2).Value = olItem.Subject
        Sh.Cells(r, 3).Value = olItem.Body
        olInspector.Close 0
        r = r + 1
    Next olInspector
    Sh.Cells.EntireColumn.AutoFit
End Sub
 
Upvote 0
Ok....So I played around a bit with this. The way it's setup now; I get the name of the message (title at top of window). I think this is what I'm looking for. It seems to only process 2-3 emails at a time though? I am using outlook 2010 but will be using this code in office 2007. So now I'm wondering if you had an example of how to reopen these emails from the stored information?

Code:
Sub Test()
    Dim Sh As Worksheet
    Dim olApp As Object
    Dim r As Long
    Dim olInspector As Object
    Dim olItem As Object
    Set Sh = Worksheets("Sheet1")
    Set olApp = GetObject(, "Outlook.Application")
    r = 1
    For Each olInspector In olApp.inspectors
        Set olItem = olInspector.CurrentItem
        Sh.Cells(r, 1).Value = olInspector
        'Sh.Cells(r, 1).Value = olItem.SenderName
        'Sh.Cells(r, 2).Value = olItem.subject
        'Sh.Cells(r, 3).Value = olItem.Body
        olInspector.Close 0
        r = r + 1
    Next olInspector
    Sh.Cells.EntireColumn.AutoFit
End Sub
 
Upvote 0
Try this:

Code:
Sub MacroA()
    Dim Sh As Worksheet
    Dim olApp As Object
    Dim r As Long
    Dim i As Long
    Dim olItem As Object
    Set Sh = Worksheets("Sheet1")
    Sh.Cells.Clear
    Set olApp = GetObject(, "Outlook.Application")
    r = 1
    With olApp
        For i = .Inspectors.Count To 1 Step -1
            Set olItem = .Inspectors.Item(i).CurrentItem
            Sh.Cells(r, 1).Value = olItem.Subject
            Sh.Cells(r, 2).Value = olItem.EntryID
            Sh.Cells(r, 3).Value = olItem.Parent.StoreID
            .Inspectors.Item(i).Close 0
            r = r + 1
        Next i
    End With
    Sh.Cells.EntireColumn.AutoFit
End Sub
 
Sub MacroB()
    Dim Sh As Worksheet
    Dim Rng As Range
    Dim olApp As Object
    Dim olNameSpace As Object
    Dim Cell As Range
    Set Sh = Worksheets("Sheet1")
    Set Rng = Sh.Range("A1").CurrentRegion.Columns(1)
    Set olApp = GetObject(, "Outlook.Application")
    Set olNameSpace = olApp.GetNameSpace("MAPI")
    For Each Cell In Rng.Cells
        olNameSpace.GetItemFromID(Cell.Offset(, 1).Value, Cell.Offset(, 2).Value).display
    Next Cell
End Sub
 
Upvote 0
That works awesome. Thanks Andrew!
I figured we had to do this with some kind of genuine ID as an email can appear with the exact subject line many times. I really appreciate it!
 
Upvote 0
In Short; How does one go about discovering usable objects?
Andrew made use of the "entryid" and "storeid" in the provided example.
I've always discovered these objects via other peoples code/projects; but am assuming there's a way to look at available objects from application to application.
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,555
Members
452,928
Latest member
101blockchains

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