For Each中的数组 - VBA

时间:2015-09-22 15:43:05

标签: arrays vba outlook outlook-vba

我有一个VBA代码,可以在特定的Outlook文件夹中执行循环消息。

我想用每个消息的发送者(objItem.SenderEmailAddress),日期(objItem.ReceivedTime)和主题(objItem.Subject)来提供数组。

我对数组没有多少经验,所以你想得到一些建议。

Dim objItem As Variant

Set colItems = Fldr.Items

For Each objItem In colItems

    Feed the Array here                      

Next

1 个答案:

答案 0 :(得分:1)

在您的程序之外(在模块顶部)使用:

i

然后在你的程序中使用:

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

最后,为了测试你可以在以后使用它:

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