将所有Array元素转换为Outlook mailitem

时间:2014-02-10 23:05:04

标签: arrays vba outlook

我有一个包含未知数量元素的数组。

我试图找出如何将所有数组元素插入到我将发送的电子邮件正文中。

有没有办法可以引用数组的所有项目(不知道存在多少元素)?

我的代码在

下面
Dim MyArray() As String
Dim Msg As Object
Dim item As Object



Set olApp = GetObject(, "Outlook.Application")

Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = olFldr.Items

olItms.Sort "Received", False 'False = Ascending = Older to newer


i = 0
For Each Msg In olItms
    If Msg.Class = olMail Then
        If InStr(1, Msg.Subject, "1401001LS") > 0 Then

        ReDim Preserve MyArray(i)

        If i = 0 Then
           MyArray(i) = "From: " & Msg.Sender & vbNewLine & "Sent: " & Msg.SentOn & vbNewLine & "To: " & Msg.To & vbNewLine & "CC: " & Msg.CC & vbNewLine & "Subject: " & Msg.Subject & vbNewLine & vbNewLine & Msg.Body
        End If


        If i > 0 Then
            MyArray(i) = "From: " & Msg.Sender & vbNewLine & "Sent: " & Msg.SentOn & vbNewLine & "To: " & Msg.To & vbNewLine & "CC: " & Msg.CC & vbNewLine & "Subject: " & Msg.Subject & vbNewLine & vbNewLine & Split(Msg.Body, "From: ")(0)
        End If

i = i + 1


        End If
    End If

Next Msg





Unload Me

Done.Show

End Sub

1 个答案:

答案 0 :(得分:1)

您可以使用以下方法遍历数组中的元素

Dim sContentsOfArray as string
Dim iCnt As Integer
For iCnt = 0 To UBound(MyArray) Step 1
    'access the element at position iCnt and put it at the end of the string
    sContentsOfArray = sContentsOfArray + MyArray (iCnt)
Next iCnt