VBA for Outlook中的运行时错误

时间:2014-03-13 19:38:11

标签: vba outlook outlook-vba

所以我试图在Outlook中编写一个模块,用于解析我在邮件正文中发送的电子邮件中的电子邮件。我得到了一个'运行时424错误"在这行代码

    For Each outlookMessage In outlookFolder.Items

它说需要一个对象,我很确定错误是参考For Each outlookMessage In outlookFolder.Items

有人可以帮助我,我对VBA不是很好,我需要一些帮助。

以下是我所谈论的代码的一部分:

    ' Get all top level folders and find our target email folder....
 For iCtr = 1 To OutlookNameSpace.Folders.Item(1).Folders.Count
    ' handle case sensitivity as I can't type worth a crap
     If LCase(OutlookNameSpace.Folders.Item(1).Folders(iCtr).Name) = LCase(strTargetFolder) Then
     'found our target :)
        Set outlookFolder = OutlookNameSpace.Folders.Item(1).Folders(iCtr)
     Exit For  ' found it so lets move on
    End If
 Next
 'set up a header for the data dump, this is for CSV
 strEmailContents = "User,Remote,Forwarder,Encoding,timestamp" & vbCrLf

 'likely should have some error handling here, in case we have found no target folder
 'Set myFolderItem = outlookFolder.Items
 ' I have commenteted out some items to illustrate the call to Sue'strEmailContents Function
    For Each outlookMessage In outlookFolder.Items
          strMsgBody = outlookMessage.Body  ' assign message body to a Var
          ' then use Sue Moshers code to look for stuff in the body
          ' all of the following stuff in the quotes "" is specific to your needs

          strEmailContents = strEmailContents & ParseTextLinePair(strMsgBody, "E-mail:")
        '  strEmailContents = strEmailContents & "," & ParseTextLinePair(strMsgBody, "REMOTE_ADDR=")
         ' strEmailContents = strEmailContents & "," & ParseTextLinePair(strMsgBody, "HTTP_USER_AGENT=")
         ' strEmailContents = strEmailContents & "," & ParseTextLinePair(strMsgBody, "HTTP_VIA=")
       '   strEmailContents = strEmailContents & "," & ParseTextLinePair(strMsgBody, "HTTP_X_FORWARDED_FOR=")
        '  strEmailContents = strEmailContents & "," & ParseTextLinePair(strMsgBody, "ENCODING=")
          'add the email message time stamp, just cause i want it
          strEmailContents = strEmailContents & "," & outlookMessage.ReceivedTime & vbCrLf
          'debug message comment it out for production
           'wscript.echo strEmailContents
     Next

先谢谢你了!

2 个答案:

答案 0 :(得分:0)

首先,您的代码永远不会检查outlookFolder是否为null。如果上面的循环找不到它怎么办?

其次,您是如何声明outlookMes​​sage变量的?您是将其声明为MailItem(不能这样做)还是作为通用对象?

答案 1 :(得分:0)

根据目前为止的讨论,以下代码应该适合您。也许你正在参加会议邀请或其他非邮件项目?

strTargetFolder = "inbox"
Dim outlookFolder As Object
Set OutlookNamespace = Application.GetNamespace("MAPI")
For iCtr = 1 To OutlookNamespace.Folders.Item(1).Folders.Count
    ' handle case sensitivity as I can't type worth a crap
     If LCase(OutlookNamespace.Folders.Item(1).Folders(iCtr).Name) = LCase(strTargetFolder) Then
     'found our target :)
        Set outlookFolder = OutlookNamespace.Folders.Item(1).Folders(iCtr)
     Exit For  ' found it so lets move on
    End If
 Next

strEmailContents = "User,Remote,Forwarder,Encoding,timestamp" & vbCrLf
if Not outlookFolder is Nothing then    
    For Each outlookMessage In outlookFolder.Items
        If TypeOf outlookMessage Is MailItem Then
             strMsgBody = outlookMessage.Body  ' assign message body to a Var
              strEmailContents = strEmailContents & ParseTextLinePair(strMsgBody, "E-mail:")
              strEmailContents = strEmailContents & "," & outlookMessage.ReceivedTime & vbCrLf
        End If
    Next
end if