开发者问题收集

处理属性访问器隐藏附件错误

2019-08-20
484

我有一个脚本,用于检查共享邮箱中邮件的附件。它会忽略隐藏附件(如邮件内的图像)。

它在我的计算机上运行。

当我在用户的计算机上安装它时,有时会出现此错误:

Run-time error '-2147221233 (8004010f)':
The property "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B" is unknown or cannot be found.

我搜索了 Web 并发现某些附件没有隐藏附件的属性(可能)。

我尝试制作一个错误处理程序。

该脚本的理念是仅接受带有 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("[email protected]")

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

我猜问题在于,在发生错误后,它会忽略导致错误的附件并转到下一个附件。

我还能检查错误处理程序中附件的附件类型吗?

如果隐藏附件没有隐藏附件属性怎么办?这可能吗?除非有另一种方法将隐藏附件与其他附件区分开来,否则它将使脚本变得毫无用处。

2个回答

是的,异常是设计使然,您需要处理它。这在 VBA 中当然很麻烦。

使用 on error resume next / Err.Clear / Err.Number / Err.Description - 请参阅 https://learn.microsoft.com/en-us/office/vba/language/reference/user-interface-help/err-object

Dmitry Streblechenko
2019-08-20
Option Explicit

Private Sub objItems_ItemAdd(ByVal Item As Object)

Const PR_ATTACHMENT_HIDDEN As String = "http://schemas.microsoft.com/mapi/proptag/0x7FFE000B"

Dim myAtt As Attachment
Dim allPdf As Boolean
Dim hidNum As Long

Dim bHidden As Boolean

allPdf = True
hidNum = 0

Dim pa As propertyAccessor
Dim objWatchFolder As Folder
Dim recip As Recipient

Set recip = Session.CreateRecipient("[email protected]")

Set objWatchFolder = Session.GetSharedDefaultFolder(recip, olFolderInbox)

For Each myAtt In Item.Attachments

    Debug.Print myAtt.DisplayName
    Set pa = myAtt.propertyAccessor
        
    bHidden = False
    
    On Error Resume Next
    bHidden = pa.GetProperty(PR_ATTACHMENT_HIDDEN)
    If err <> 0 Then bHidden = True ' Set True when property not found
    On Error GoTo 0 ' Consider mandatory and as soon as possible after On Error Resume Next
    
    If bHidden = False Then
        If Right(LCase(myAtt.fileName), 4) <> ".pdf" Then
            allPdf = False
            Exit For
        End If
    Else
        hidNum = hidNum + 1
    End If

Next myAtt

If allPdf = False Or Item.Attachments.count = hidNum Then
    Item.Move objWatchFolder.Parent.Folders("Error")
End If

End Sub
niton
2022-07-13