2012-10-22 54 views
0

我有兩個VB腳本,我試圖合併中間的錯誤處理。
我有一份腳本的偉大工程:VB腳本來錯誤地複製文件和電子郵件

Dim objFSO, colFiles, objFile, strDestFolder, objNewestFile 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set colFiles = objFSO.GetFolder("C:\RD\Source") 
strDestFolder = "C:\RD\To\" 

For Each objFile In colFiles.Files 
    'If Left(objFile.Name, 4) = "apdt" Then 
     If objNewestFile = "" Then 
     Set objNewestFile = objFile 
     Else 
      If objNewestFile.DateLastModified < objFile.DateLastModified Then  
      Set objNewestFile = objFile 
      End If 
     End If 
    'End If 
Next 

If Not objNewestFile Is Nothing Then 
    objFSO.CopyFile objNewestFile.Path,strDestFolder,True 
End If 

和電子郵件的腳本,也可以工作:

strSMTPFrom = "[email protected]" 
strSMTPTo = "[email protected]" 
strSMTPRelay = "smtp relay server name or IP address" 
strTextBody = "Body of your email" 
strSubject = "Subject line" 
strAttachment = "full UNC path of file" 


Set oMessage = CreateObject("CDO.Message") 
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPRelay 
oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
oMessage.Configuration.Fields.Update 

oMessage.Subject = strSubject 
oMessage.From = strSMTPFrom 
oMessage.To = strSMTPTo 
oMessage.TextBody = strTextBody 
oMessage.AddAttachment strAttachment 


oMessage.Send 

但我想一個腳本,該腳本將複製新的文件,但如果遇到錯誤會通過電子郵件發送給我,讓我知道所以我猜我需要添加如果錯誤<> 0然後,發送電子郵件的功能,但我掙扎!任何幫助將是偉大的?

感謝

回答

0

「下面是修改後的代碼拷貝一次遇到一個錯誤,將發送電子郵件:

Dim objFSO, colFiles, objFile, strDestFolder, objNewestFile 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set colFiles = objFSO.GetFolder("C:\RD\Source") 
strDestFolder = "C:\RD\To\" 

For Each objFile In colFiles.Files 
    'If Left(objFile.Name, 4) = "apdt" Then 
    If objNewestFile = "" Then 
     Set objNewestFile = objFile 
    Else 
     If objNewestFile.DateLastModified < objFile.DateLastModified Then  
      Set objNewestFile = objFile 
     End If 
    End If 
    'End If 
Next 

If Not objNewestFile Is Nothing Then 
    On Error Resume Next 
     objFSO.CopyFile objNewestFile.Path,strDestFolder,True 
    If Err<>0 Then 

     strSMTPFrom = "[email protected]" 
     strSMTPTo = "[email protected]" 
     strSMTPRelay = "smtp relay server name or IP address" 
     strTextBody = "Error Encountered while trying to copy newest file." 
     strTextBody = strTextBody & "Error Message: " & Err.Message 
     strSubject = "Subject line" 
     strAttachment = "full UNC path of file" 


     Set oMessage = CreateObject("CDO.Message") 
     oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
     oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPRelay 
     oMessage.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
     oMessage.Configuration.Fields.Update 

     oMessage.Subject = strSubject 
     oMessage.From = strSMTPFrom 
     oMessage.To = strSMTPTo 
     oMessage.TextBody = strTextBody 
     oMessage.AddAttachment strAttachment 


     oMessage.Send 

    End If 
End If 
+0

你的先生是一個傳奇。非常感謝你! – Rob