0

The following code is working, but I have a few questions.

  1. I have more than one email address in Outlook. I would like to specify the email address in Excel. Let's say in "Control" tab, cell "b1" is an email address. How can I specify it?

  2. I want to specify a non-default folder name (not Inbox, Sent, Drafts or Delete) by name in Excel. Let’s say in the "Control" tab, cell "c1" is a folder name. How can I specify it?

Sub GetFromInbox()
    
    Dim olApp As Outlook.Application
    Dim olNs As Namespace
    Dim Fldr As MAPIFolder
    Dim olMail As Variant
    Dim i, ij As Integer
    Dim tt As Date
    
    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
    i = 1
    ij = 0
    x = Date
    
    ' Now. the following "For each next " code starts to look in the oldest email!
    ' So how can I change the code if the code starts from the newest?
    For Each olMail In Fldr.Items
        ij = ij + 1
        'If IsNumeric((Format(olMail.ReceivedTime, "dd/mm/yy"))) Then
            Sheets("test").Range("a1").Select
            Sheets("test").Range("I1").Clear
            Sheets("test").Range("I2") = ij
            Sheets("test").Range("I1").Value = (Format(olMail.ReceivedTime, "dd/mm/yy"))
            Sheets("test").Range("I1").NumberFormat = "dd/mm/yy"
            tt = Sheets("test").Range("I1")
            ' MsgBox ("Y-tt=" & tt & " receivedtime=" & olMail.ReceivedTime)
        'Else
            'tt = 0
            'MsgBox ("N-tt=" & tt & " receivedtime=" & olMail.ReceivedTime)
        'End If
        ' tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
        If tt >= Range("H1") Then
            'If InStr(olMail.Subject, "others") > 0 And tt >= Range("h1") Then
            If InStr(olMail.Subject, "others") > 0 Then
                ActiveSheet.Range("h2") = "y"
                ActiveSheet.Cells(i, 1).Value = olMail.Subject
                ActiveSheet.Cells(i, 2).Value = olMail.ReceivedTime
                ActiveSheet.Cells(i, 3).Value = olMail.SenderName
                tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
                ActiveSheet.Cells(i, 4).Value = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
                ' tt = ActiveSheet.Cells(i, 4).Value
                ActiveSheet.Cells(i, 5).Value = (Format(olMail.ReceivedTime, "hh:mm"))
                MsgBox ("tt=" & tt)
                i = i + 1
            End If
        Else
            Sheets("test").Range("h2") = "N"
        End If
    Next olMail
    
    Set Fldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing
    'tt = ""
    
End Sub
karel
  • 5,489
  • 46
  • 45
  • 50
Bnf
  • 1
  • 1
  • Does this answer your question? [Get reference to additional Inbox](https://stackoverflow.com/questions/9076634/get-reference-to-additional-inbox) – niton Feb 09 '20 at 22:34
  • I checked it and it wont help a lot – Bnf Feb 16 '20 at 09:08
  • Let say this way : I have 4 email address in outlook and different name of the folders . So in "Main" tab , cell (A2) is for me to type email address. cell(b2) is for me to type folder name. Everytime if I need to extract some email (by using the above VBA code), i can be flexible by typing email address in cell (A2) and folder name in cell(b2) to extract it. So how can I change the above code to cope with that? – Bnf Feb 16 '20 at 09:10

1 Answers1

0

This describes how to walk from folder to folder.

https://stackoverflow.com/a/35657225/1571407

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration

Sub GetFromAdditionalInbox()

    ' Early binding. Set reference to Microsoft Outlook XX.X Object Library
    Dim olApp As Outlook.Application
    Dim olNs As Namespace
    Dim Fldr As Outlook.folder

    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")

    ' Where folders are in the navigation pane
    '
    ' In "Main" tab
    '  - cell (A2) is email address.
    '  - cell (B2) is "Inbox"
    '  - cell (C2) is "subfolder directly under Inbox"
    '  - cell (D2) is "subsubfolder directly under subfolder"
    '
    With ThisWorkbook.Worksheets("Main")

        ' https://stackoverflow.com/questions/9076634/get-reference-to-additional-inbox
        Set Fldr = olNs.Folders(.Range("A2").Value)
        Set Fldr = Fldr.Folders(.Range("B2").Value)
        'Set Fldr = Fldr.Folders(.Range("C2").Value)
        'Set Fldr = Fldr.Folders(.Range("D2").Value)

        ' If the tree goes much further, investigate GetFolder
        '  https://stackoverflow.com/questions/17049546/get-mapi-folder-in-outlook-from-folder-path

    End With

    Debug.Print Fldr

End Sub
niton
  • 8,771
  • 21
  • 32
  • 52
  • State what you see in the immediate pane for `Debug.Print Fldr`. Should be "Inbox". If you do not get that far, what is the error and the line with the error? – niton Feb 19 '20 at 15:09
  • I think cell(A2) is for me to put the email address. Cell(B2) is the name of the folder. The folder is not default ( i.e. inbox/sent/ deleted) but it could be any name that I created. For the email address (bc2@abc.com) , it is group email address and can read/move email under this email address but could not use this email to anyone – Bnf Feb 20 '20 at 11:39
  • it stopped working at this one -->Set Fldr = olNs.Folders(.Range("A2").Value) – Bnf Feb 20 '20 at 11:40
  • If bc2@abc.com is the text displayed in the navigation pane then there is nothing I can think of. – niton Feb 21 '20 at 00:03
  • niton: I found the way out. -- set fldr=session.folders(worksheets("main").range("A2").Value).folders(worksheets("main").range("B2").Value) <--A2 is not the email address but the name of email address. Let say this way-->the name is T123 and email address is T123.b123@abc.com – Bnf Mar 06 '20 at 17:15
  • so I will have to put into cell A2 – Bnf Mar 06 '20 at 17:16