vba - Outlook:处理属性访问器隐藏附件错误
问题描述
我有一个脚本可以检查所有发送到共享邮箱的邮件。它检查附件类型并忽略检查中的隐藏附件(如邮件中的图像)。它在我的计算机上完美运行,但是当我将它安装在用户计算机上时,有时会出现此错误:
Run-time error '-2147221233 (8004010f)':
The property "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B" is unknown or cannot be found.
我在网上搜索并找到了一个原因 - 某些附件没有隐藏附件的属性(可能)。但是如何处理这个问题呢?我试图制作一个错误处理程序,但它使脚本无法正常工作。该脚本的主要思想是仅接受带有 PDF 附件的邮件,并且使用当前的错误处理程序,它有时会接受包含其他附件类型的邮件。
这是检查附件的代码部分:
Private Sub objItems_ItemAdd(ByVal Item As Object)
Const PR_ATTACHMENT_HIDDEN As String = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B"
Dim myAtt As Outlook.Attachment
Dim allPdf As Boolean
Dim hidNum As Integer
allPdf = True
hidNum = 0
Dim pa As PropertyAccessor
Dim objWatchFolder As Outlook.Folder
Set objNS = Application.GetNamespace("MAPI")
Dim Recip As Outlook.Recipient
Set Recip = objNS.CreateRecipient("test@mail.com")
Set objWatchFolder = objNS.GetSharedDefaultFolder(Recip, olFolderInbox)
For Each myAtt In Item.Attachments
Debug.Print myAtt.DisplayName
Set pa = myAtt.PropertyAccessor
On Error GoTo Handler
If Not pa.GetProperty(PR_ATTACHMENT_HIDDEN) Then
If Right(LCase(myAtt.FileName), 4) <> ".pdf" Then
allPdf = False
End If
Else
hidNum = hidNum + 1
End If
NextAtt:
Next myAtt
If allPdf = False Or Item.Attachments.Count = hidNum Then
Item.Move objWatchFolder.Parent.Folders("Error")
End If
Set Item = Nothing
Set myAtt = Nothing
Set pa = Nothing
Set objWatchFolder = Nothing
Set Recip = Nothing
Exit Sub
Handler:
Resume NextAtt
End Sub
我想问题是在出错后它只是忽略了导致错误的附件并转到下一个。我仍然可以在错误处理程序中检查附件的附件类型吗?
如果隐藏附件没有隐藏附件属性怎么办?这甚至可能吗?除非有任何其他方法可以将隐藏的附件与其他附件区分开来,否则它将使 sript 无用。
解决方案
是的,例外是设计使然,您需要处理它。这在 VBA 中当然是混乱的。
使用on error resume next
/ Err.Clear
/ Err.Number
/ Err.Description
- 请参阅https://docs.microsoft.com/en-us/office/vba/language/reference/user-interface-help/err-object
推荐阅读
- javascript - Controlling a flow of a program
- regex - Regex Match string prefix, but not multiple strings containing this prefix
- android - 资产文件夹中的 SQLite 数据库在 Android Pie 即 API 28 中不起作用
- react-native - React Native:错误未定义构建JavaScript包失败
- javascript - for循环和while循环之间的真正区别是什么?
- javascript - CSS - 未选中时单选按钮样式更改
- java - 使用 Servlet 自动登录和注销
- python - 对于python中的列表,如何实现以下功能?
- javascript - 在元素上使用 querySelector 时未定义或为 null
- r - R ggscatter p值四舍五入?