0

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
5
  • There are static arrays, where you know the size at compile time. There are dynamic arrays where you allocate the size before assigning values and there are collections which dynamically adjust the size. What do you want? Commented Sep 22, 2015 at 16:01
  • well, considering I don't know the total of messages, I think the most appropriate would be dynamic. Not sure how to work with them in this case. Commented Sep 22, 2015 at 16:05
  • It kind of depends on what you want to do with the arrays afterwards. Also do you want multiple arrays for each information, or one array of MessageItem which contains all the information you need for each email? Commented Sep 22, 2015 at 16:08
  • I think one array containing all the info would be the best option. Could you provide some exemples over my code? I appreciate. Commented Sep 22, 2015 at 16:29
  • The answer below is sufficient. Commented Sep 22, 2015 at 18:16

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.