2014-03-13 71 views
1

所以我想在Outlook中編寫一個模塊,它將解析郵件正文中發送的電子郵件。我得到一個「運行時錯誤424」在這行代碼VBA中的Outlook運行時錯誤

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 

謝謝你在先進

+0

對於每個**項目**在outlookFolder.Items的作品? – Alex

+0

作爲後續從你的'For iCtr = 1..'循環你正在尋找具有特定名稱的文件夾,當這個文件夾沒有找到,你的'outlookFolder'是Nothing。這是錯誤的原因。要處理一個錯誤,你可以添加「if」語句:'如果沒有outlookFolder是Nothing Then'就在'For Each outlookMes​​sage in outlookFolder.Items'和'End If'緊接'Next'後面 –

+0

@simco我應該把什麼放在如果陳述? –

回答

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 
+1

非常感謝你的工作!...我現在有我的代碼的其他部分的問題;但你解決了我的問題!謝謝! –

0

首先,您的代碼從不檢查outlookFolder是否爲空。如果上面的循環找不到它,該怎麼辦?

其次,你是如何聲明outlookMes​​sage變量的?你是否聲明它爲MailItem(不能這樣做)或作爲一個通用的對象?

+0

林不知道我的錯誤檢查方法應該是什麼。我接受了@simco的建議,但我不知道我應該在我的if語句中加入什麼。此外,我宣佈OutlookMes​​sage作爲一個對象 –

+0

「如果不是outlookFolder沒有那麼」會做到這一點。 –