outlook vba脚本错误

时间:2015-06-15 16:18:29

标签: vba outlook outlook-vba

我在收到的每封电子邮件中都运行以下代码:

Dim globalWrongExt As Boolean
Dim globalcontainsZip As Boolean
Dim globalTotalSize As Double


Sub sortingP8(Item As Outlook.MailItem)
'Check each attachment

globalTotalSize = 0
globalcontainsZip = False
globalWrongExt = False

Set ns = Application.GetNamespace("MAPI")
Set nonIngFolder = ns.Folders("Pasdasd@xasxax.com").Folders("Non-ingestible Items")
Set ingFolder = ns.Folders("Pasdasd@xasxax.com").Folders("Ingestible Items")
Set zipFolder = ns.Folders("Pasdasd@xasxax.comk").Folders("ZIP files")

'Check extensions
extensionCheck Item

'Move email to the appropiate folder
If (globalWrongExt = True Or globalTotalSize > 10000000) Then
    Item.Move nonIngFolder
ElseIf (globalcontainsZip = True) Then
    Item.Move zipFolder
Else
    Item.Move ingFolder
End If

End Sub

Private Sub extensionCheck(Item As Outlook.MailItem)
Dim olkAtt As Outlook.Attachment
'Check each attachment

For Each olkAtt In Item.Attachments
    Dim extension As String
    extension = Right(LCase(olkAtt.FileName), 4)
    'If the attachment's file name ends with .zip
    globalTotalSize = globalTotalSize + olkAtt.Size

    If extension = ".msg" Then
        extensionCheck olkAtt
    ElseIf extension <> ".ppt" And extension <> ".doc" And extension <> ".pdf" And extension <> ".jpg" And extension <> ".zip" And extension <> ".docx" And extension <> ".pptx" And extension <> ".htm" And extension <> ".html" And extension <> ".gif" And extension <> ".tif" Then
        globalWrongExt = True
    ElseIf extension = ".zip" Then
        globalcontainsZip = True
    End If
Next

End Sub

调试时,以下行失败:

If extension = ".msg" Then
    **extensionCheck olkAtt**

是否可以将olkAtt对象转换为Outlook.MailItem。

或者也许是脚本的解决方法。我想要实现的是在每个.msg附件中调用的递归函数,以评估包含的附件​​。

2 个答案:

答案 0 :(得分:0)

If extension = ".msg Then为您提供类型不匹配的内容?你确定吗? 您是否尝试访问嵌入式附件(Attachment.Type == 5)?

1.首先将附件保存为MSG文件(Attachment.SaveAsFile),然后使用Namespace.OpenSharedItem打开MSG文件。

2.如果您使用Redemption是一个选项,它允许使用RDOAttachment。EmbeddedMsg属性直接访问嵌入式邮件附件。

答案 1 :(得分:0)

你变暗了

Dim olkAtt As Outlook.Attachment

然后调用

extensionCheck olkAtt

接收MailItem:

Private Sub extensionCheck(Item As Outlook.MailItem)

如果你是肯定的olkAtt是一个MailItem,那么你应该有另一个类型为MailItem的对象,将其设置为olkAtt,然后将其传递给extensionCheck:

Dim ToBeTested as MailItem
Set ToBeTested = olkAtt
extensionCheck(ToBeTested)

这样您就不会收到类型不匹配错误。