收集电子邮件并转发 - 编译错误:需要常量表达式

时间:2017-05-03 13:23:07

标签: vba email outlook outlook-vba outlook-2007

我正在尝试查找从特定电子邮件地址发送的电子邮件,然后每天09:00到11:00之间,将此特定电子邮件地址中的所有电子邮件转发到第二个电子邮件地址。

我收到以下错误。

Compile error

Sub Search_Inbox()

    Dim myOlApp As New Outlook.Application
    Dim myNameSpace As Outlook.NameSpace
    Dim myInbox As Outlook.MAPIFolder
    Dim myitems As Outlook.Items
    Dim myitem As Object
    Dim Found As Boolean 'this is a boolean to check if the code found the emails or not
    Dim Str_Address As String 'this is a String for an Email Address
    Dim Mail_Value As MailItem 'this is where the email adresses will be stored
    Dim Bool_Time As Boolean 'used to hold the time values
    Dim Count As Integer 'used as the count for the array
    Dim emails_forward(Count) As Integer 'used to hold all the emails to be forwarded when it is 11:00


    Set myNameSpace = myOlApp.GetNamespace("MAPI")
    Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
    Set myitems = myInbox.Items
    Found = False

    For Each myitem In myitems
        'below is the selected time period
        If Bool_Time = (Time >= #9:00:00 AM#) And (Time <= #10:59:59 AM#) Then
            If myitem.Class = olMail Then
                If InStr(1, myitem.Mail_Value, "email address to search for") > 0 Then

                    Forward_Emails (Mail_Value)

                    Debug.Print "Found"
                    Found = True
                 End If
            End If
        End If
    Next myitem

End Sub

Function Forward_Emails(Email_to_Forward As Outlook.MailItem)

    Set Email_to_Forward = Item.Forward

    Email_to_Forward.Recipients.Add "email address to forward to after"
    Email_to_Forward.Save


    Email_to_Forward.Send
    Set Email_to_Forward = Nothing

End Function

1 个答案:

答案 0 :(得分:0)

使用ReDim声明数组,例如:ReDim emails_forward(Count) As Integer

<强> MSDN Constant expression required