2010-02-16 93 views
1

我有以下vba代碼是更大的腳本的一部分。我遇到的問題是,即使Outlook郵件已保存到系統上的某個目錄,SaveAs功能也會不斷拋出錯誤。檢查Err對象不會產生任何結果,因爲所有內容都是空白或0。爲什麼SaveAs方法拋出錯誤?

另一個奇怪的問題是,當錯誤處理代碼被註釋掉,因爲它在下面,腳本正確執行而不會引發任何錯誤。對我來說,似乎錯誤處理代碼本身導致了這個問題。 VSTO目前不是一種選擇。

  1. 下面是否有替代 方法?
  2. 你能提供一些 有用的調試技巧來幫助這個 的情況嗎?

這是我使用的代碼

For Each itm In itemsToMove 
    Dim mItem As MailItem 
    Set mItem = itm 

    ' On Error Resume Next 
    sSubject = mItem.Subject 
    sDate = Format(mItem.CreationTime, "yyyymmdd_hhnnss_") 
    FNme = DirName & sDate & StripIllegalChar(sSubject) & ".msg" 
    **mItem.SaveAs FNme, olMSG** 
    iCount = iCount + 1 

    'ErrorHandler: 
    '   MsgBox ("The email " & FNme & " failed to save.") 
    '   MsgBox Err.Description & " (" & Err.Number & ")" 
    '   Set objNameSpace = Nothing 
    '   Set objOutlook = Nothing 
    '   Set objNameSpace = Nothing 
    '   Set objInbox = Nothing 
    '   Set objInbox = Nothing 
    '   Set itemsToMove = Nothing 
    '   Set itemsToMove = Nothing 
    '   Exit Sub 
Next 

解決方案:

Sub SomeSub 
.... 
.... 
For Each itm In itemsToMove 
    Dim mItem As MailItem 
    Set mItem = itm 

    On Error GoTo ErrorHandler 
    sSubject = mItem.Subject 
    sDate = Format(mItem.CreationTime, "yyyymmdd_hhnnss_") 
    FNme = DirName & sDate & StripIllegalChar(sSubject) & ".msg" 
    mItem.SaveAs FNme, olMSG 
    iCount = iCount + 1 
Next 
End If 
Exit Sub 

ErrorHandler: 
    MsgBox ("The email " & FNme & " failed to save.") 
    MsgBox Err.Description & " (" & Err.Number & ")" 
    Set objNameSpace = Nothing 
    Set objOutlook = Nothing 
    Set objNameSpace = Nothing 
    Set objInbox = Nothing 
    Set objInbox = Nothing 
    Set itemsToMove = Nothing 
    Set itemsToMove = Nothing 
    Resume Next 
End Sub 

回答

4

廣場的退出小組/功能的ErrorHandler之前。

您的代碼正在正確執行,但您始終執行ErrorHandler。

您只希望錯誤代碼在出錯時執行,並非總是如此。如果沒有發生錯誤,您需要退出功能/子功能。

喜歡的東西

... 
iCount = iCount + 1 

NoError: 
    Exit Sub 

ErrorHandler: 
... 

Error Handling In VBA

喜歡的東西

On Error Goto ErrHandler: 
N = 1/0 ' cause an error 
' 
' more code 
' 
Exit Sub 'THIS IS WHAT YOU ARE MISSING 
ErrHandler: 
' error handling code 
Resume Next 
End Sub 
+0

是怎麼回事?那不就是每次都退出Sub嗎? – Ahmad

+0

那應該是下一個,不是嗎? – falstro

+0

正確,我將錯誤處理程序重構爲Sub的結尾,主要原因是我不想完全退出,直到處理完所有項目 – Ahmad

2

您必須確保當錯誤實際已發生的ErrorHandler你只執行。我想嘗試這樣的事情,但你必須把它適應sub的休息:

Sub ... 
    // ... 
    On Error goto errorhandler 
    For Each itm In itemsToMove 
    //... 
    mItem.SaveAs FNme, olMSG 
    iCount = iCount + 1 
    Next  

    Exit Sub 
ErrorHandler: 
    // ... 
End Sub 

另一種可能是:在我的環境

For Each itm In itemsToMove 
    On Error goto errorhandler 
    //... 
    mItem.SaveAs FNme, olMSG 
    iCount = iCount + 1 
    goto NoError 

    ErrorHandler: 
     //... 
     Exit sub 
    NoError: 
    Next  
0

工作就好了,稍微從上面修改(我刪除了StripIllegalChar例程,因爲它沒有發佈):

Sub SaveAsItems() 
Dim MAPINS As NameSpace 
Set MAPINS = Application.GetNamespace("MAPI") 
Dim inboxFolder As Folder 
Set inboxFolder = MAPINS.GetDefaultFolder(olFolderInbox) 
Dim itemsToMove As items 
Set itemsToMove = inboxFolder.items 
Dim mItem As MailItem 
DirName = "C:\Users\Me\Desktop\files\" 
For Each itm In itemsToMove 
    Set mItem = itm 
    sSubject = mItem.Subject 
    sDate = Format(mItem.CreationTime, "yyyymmdd_hhnnss_") 
    FNme = DirName & sDate & ".msg" 
    mItem.SaveAs FNme, olMSG 
Next 
End Sub 
相關問題