Perché Metodo SaveAs buttare errore?
-
20-09-2019 - |
Domanda
Ho il seguente codice VBA è parte di uno script più grande. Il problema che sto avendo è che che la funzione Salva con nome getta continuamente un errore anche se il messaggio di Outlook è stato salvato in una directory sul sistema. Ispezione del dell'oggetto Err produce alcun risultato in quanto tutto è vuoto o 0.
Un altro problema strana è che quando il codice di gestione degli errori è commentata in quanto è al di sotto, lo script viene eseguito correttamente senza alcun errore essere gettato. A me sembra che il codice di gestione degli errori per sé causa il problema. VSTO non è un'opzione in questo momento.
- Ci sono alternative alla avvicinamento al di sotto?
- Si può fornire un certo suggerimenti di debug utili per aiutare questo situazione?
Questo è il codice che sto usando
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
Soluzione:
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
Soluzione
Inserire un Exit Sub / Function prima di ErrorHandler .
Il codice viene eseguito correttamente, ma si sta eseguendo l'ErrorHandler sempre.
Si desidera solo il codice di errore da eseguire in caso di errore, non sempre. È necessario uscire dalla funzione / Sub Se non si verificano errori.
Qualcosa di simile
...
iCount = iCount + 1
NoError:
Exit Sub
ErrorHandler:
...
Qualcosa di simile
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
Altri suggerimenti
Si dovrà assicurarsi che il proprio ErrorHandler eseguito solo quando un errore in realtà si è verificato. Mi piacerebbe provare qualcosa di simile, ma dovrete per adattarlo al resto della sub
:
Sub ...
// ...
On Error goto errorhandler
For Each itm In itemsToMove
//...
mItem.SaveAs FNme, olMSG
iCount = iCount + 1
Next
Exit Sub
ErrorHandler:
// ...
End Sub
Un'alternativa potrebbe essere:
For Each itm In itemsToMove
On Error goto errorhandler
//...
mItem.SaveAs FNme, olMSG
iCount = iCount + 1
goto NoError
ErrorHandler:
//...
Exit sub
NoError:
Next
Lavorare bene nel mio ambiente, leggermente modificata dalla tua sopra (ho rimosso la routine StripIllegalChar in quanto non è stato pubblicato):
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