想要弹出消息 "ABC" 而不是 vba 中的错误 5

Want to Pop up Msg "ABC" instead of Error 5 in vba

我有一个脚本,它只在 Outlook 中附加选定的文件,但是当文件没有保存时​​,它会给出错误号 5。

我想要一个消息弹出窗口“请保存您的文件”而不是错误消息,下面是我的脚本

Sub SendSDDesignteam()
    Dim objActivePresetation As Presentation
    Dim objSlide As Slide
    Dim n As Long
    Dim strName As String
    Dim strTempPresetation As String
    Dim objTempPresetation As Presentation
    Dim objOutlookApp As Object
    Dim objMail As Object

    Set objActivePresetation = ActivePresentation

    For Each objSlide In objActivePresetation.Slides
        objSlide.Tags.Delete ("Selected")
    Next

    'Add a tag "Selected" to the selected slides
    For n = 1 To ActiveWindow.Selection.SlideRange.Count
        ActiveWindow.Selection.SlideRange(n).Tags.Add "Selected", "YES"
    Next n

    strName = objActivePresetation.Name
    strName = Left(strName, InStrRev(strName, ".") - 1)
    strTempPresetation = Environ("TEMP") & "\" & strName & ".pptx"

    'Copy the active presentation to a temp presentation
    objActivePresetation.SaveCopyAs strTempPresetation
    Set objTempPresetation = Presentations.Open(strTempPresetation)

    'Remove the untagged slides
    For n = objTempPresetation.Slides.Count To 1 Step -1
        If objTempPresetation.Slides(n).Tags("Selected") <> "YES" Then
           objTempPresetation.Slides(n).Delete
        End If
    Next n
 
    objTempPresetation.Save
    objTempPresetation.Close
 
    'Attach the temp presentation to a new email
    Set objOutlookApp = CreateObject("Outlook.Application")
    Set objMail = objOutlookApp.CreateItem(olMailItem)
 
    'Change the email details as per your needs
    With objMail
         .To = "abc@johndoe.com"
         .Subject = "Formatting/Designing Help"
         .Body = "Hi Team," & vbCr & vbCr & vbTab & "Need this by Date: DD/MM/YYYY, Time : 00:00, Client : XYZ, Comment : NA."
         .Attachments.Add strTempPresetation
         .Display
    End With
End Sub

如果你能在这方面帮助我,那将是很大的帮助。

提前致谢

此处出现错误。

    strName = Left(strName, InStrRev(strName, ".") - 1)

未保存文件时,strName不包含“.”。 InStrRev(strName, ".") - 1 等于 -1。这会产生一个错误。 所以你可以检查是否发生错误并显示这样的消息。

    On Error Resume Next 'Begin ignoring errors.
    strName = Left(strName, InStrRev(strName, ".") - 1)
    If Err Then
        MsgBox "Please save your file", vbCritical, "Error"
        Exit Sub
    End If
    On Error Goto 0 'Stop ignoring errors.

但是你最好像这样在程序开始时检查文件是否保存。

    Set objActivePresetation = ActivePresentation

    'Check if the file is saved.
    If objActivePresetation.Saved = False Then
        MsgBox "Please save your file", vbCritical, "Error"
        Exit Sub
    End If