首页 > 解决方案 > 想要在 vba 中弹出消息“ABC”而不是错误 5

问题描述

我有一个脚本,它只在 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

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

提前致谢

标签: vbapowerpoint-2013

解决方案


这里发生错误。

    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

推荐阅读