Richard
Richard

Reputation: 53

Array in For Each - VBA

I have a VBA code that performs a loop in a specific Outlook Folder, message by message.

I’d like to feed an array with the sender of each message(objItem.SenderEmailAddress), the date(objItem.ReceivedTime) and the subject(objItem.Subject).

I don’t have much experience with arrays, so would you like to get some suggestions.

Dim objItem As Variant

Set colItems = Fldr.Items

For Each objItem In colItems

    Feed the Array here                      

Next

Upvotes: 0

Views: 462

Answers (1)

SierraOscar
SierraOscar

Reputation: 17637

Outside of your procedure (at the top of the module) use:

Private Type EmailInfo
    Sender As String
    DateReceived As Date
    Subject As String
End Type

Then in your procedure use:

Dim emails() As EmailInfo
Dim i As Long: i = 1

Set colItems = Fldr.Items

ReDim email(1 To colItems.Count) As EmailInfo

For Each objItem In colItems
    With objItem
        email(i).Sender = .SenderEmailAddress
        email(i).DateReceived = .ReceivedTime
        email(i).Subject = .Subject
    End With

    i = i + 1
Next

Finally, for testing you can use this afterward:

For i = 1 To UBound(email)
    Debug.Print email(i).Sender
    Debug.Print email(i).DateReceived
    Debug.Print email(i).Subject
Next i

Upvotes: 1

Related Questions