Excel &引用;对象“U工作簿失败”的方法saveas;当目录中存在同名文件时

Excel &引用;对象“U工作簿失败”的方法saveas;当目录中存在同名文件时,excel,vba,save,Excel,Vba,Save,如果文件再次保存,我将尝试在目录中保存该文件excel消息出现。我是新的vba错误处理,困在这个项目的代码 如果按下“否”,则显示主题错误。这是我的密码: Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) Dim x As String With Destwb On Error Resume Next .SaveAs Te

如果文件再次保存,我将尝试在目录中保存该文件excel消息出现。我是新的vba错误处理,困在这个项目的代码

如果按下“否”,则显示主题错误。这是我的密码:

  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

为什么不跳过错误?

问题:

  • 错误恢复下一步
    在保存文件的行之后
这将继续代码而不保存文件

解决方案:

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

它应该给出错误,因为VBA无法保存它。是否要跳过错误?是以何种方式处理此错误句柄?你想跳过它吗?请您使用其他名称保存?覆盖文件?您的问题不清楚。如果按“否”,则跳过该问题并继续输入代码。将此行置于代码上方
错误时继续下一步
这样做不会附加我的文件;我不想保存它,而是通过邮件发送,我已经更新了剩余的代码…在您的代码中没有任何地方您正在附加文件!,你也没有提到你是。编辑您的问题,并清楚地解释您想要什么以及正在发生什么。刚刚编辑了代码。很抱歉,由于连接不好而导致延迟。如果您只想单击“否”,为什么要使用该行代码?我会说摆脱它。@UmarFarooq,按
F8
试着运行你的代码,也就是说,逐步地,并指出你遇到问题的地方。