vba - 想要在 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
如果你能在这方面帮助我,那将是很大的帮助。
提前致谢
解决方案
这里发生错误。
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
推荐阅读
- flutter - Flutter中根据Tab Bar名称显示Tab Bar查看产品
- sql - 如何从 SQL 中具有重复字符的列值(字符串)中获取值?
- python - YML 文件中的 Python 脚本输出状态(如果失败或成功运行)
- odoo - Odoo - 设置域/主机名时的会话错误
- ajax - Wordpress 博客“加载更多”博客文章索引创建博客重复
- javascript - React onFocus 和 onFocusCapture 的区别
- django - 如果 search_field (由相关的 FK)乘以 Django 管理注释,
- python - 无法在来自 blobstorage 的先前数据帧中附加新数据帧
- reactjs - firebase__WEBPACK_IMPORTED_MODULE_3___default.a.auth.onAuthStateChanged 不是 Firebase 反应的函数
- forms - 将控制台参数解析到浏览器的批处理脚本