代码之家  ›  专栏  ›  技术社区  ›  Ahmad

为什么SaveAs方法抛出错误?

  •  1
  • Ahmad  · 技术社区  · 16 年前

    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
    
    3 回复  |  直到 6 年前
        1
  •  4
  •   Community Mohan Dere    8 年前

    放置 错误处理程序

    代码执行正确,但始终执行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 
    
        2
  •  2
  •   Anders Lindahl    16 年前

    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     
    
        3
  •  0
  •   Todd Main    16 年前

    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
    
    推荐文章