如果文件再次保存,我正在尝试将文件保存在目录中,并且出现excel消息。我是vba错误处理的新手,被卡在此项目代码中
如果按“ NO”,则出现主题错误。这是我的代码:
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Dim x As String
With Destwb
On Error Resume Next
.SaveAs TempFileName & FileExtStr, FileFormat:=FileFormatNum
If Sheet1.Cells(2, 6) = "All" Then
Exit Sub
End If
'Destwb.Sheets("REC_INT").Range("A1").Select
If Not Sheet1.Cells(2, 6) = "All" Then
x = Sheet1.Range("L3")
With OutMail
.To = Sheet1.Cells(x, 3).Value
.CC = Sheet1.Cells(x, 4).Value
.BCC = ""
.Subject = "Service Record"
.Body = Sheet1.Cells(8, 7).Value
.Attachments.Add Destwb.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
'.Send 'or use
.Display
End With
On Error GoTo 0
.Close savechanges:=False
End If
End With
'Delete the file you have send
'Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
Application.Calculation = xlCalculationAutomatic
End Sub
为什么不跳过错误?
答案 0 :(得分:1)
问题:
On Error Resume Next
在保存文件的行之后这将继续执行代码而不保存文件。
解决方案:
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Dim x As String
On Error Resume Next
With Destwb
.Save True
'.SaveAs TempFileName & FileExtStr, FileFormat:=FileFormatNum
If Sheet1.Cells(2, 6) = "All" Then
Exit Sub
Else
x = Sheet1.Range("L3")
With OutMail
.To = Sheet1.Cells(x, 3).Value
.CC = Sheet1.Cells(x, 4).Value
.BCC = ""
.Subject = "Service Record"
.Body = Sheet1.Cells(8, 7).Value
.Attachments.Add Destwb.FullName
'You can add other files also like this
'.Attachments.Add ("C:\test.txt")
'.Send 'or use
.Display
End With
On Error GoTo 0
.Close savechanges:=False
End If
End With
'Delete the file you have send
'Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
With Application
.ScreenUpdating = True
.EnableEvents = True
.Calculation = xlCalculationAutomatic
End With
End Sub