2015-06-15 32 views
0

我跑在每一個下面的代碼接收的電子郵件:前景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("[email protected]").Folders("Non-ingestible Items") 
Set ingFolder = ns.Folders("[email protected]").Folders("Ingestible Items") 
Set zipFolder = ns.Folders("[email protected]").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附件中調用的遞歸函數來評估附件中包含的附件​​。

+0

異常的輸出是什麼? –

+0

剛運行時錯誤'13': 類型不匹配 – user3270648

回答

0

If extension = ".msg Then給你一個類型不匹配?你確定? 您是否嘗試訪問嵌入式附件(Attachment.Type == 5)?

1.首先將附件保存爲MSG文件(Attachment.SaveAsFile),然後使用Namespace.OpenSharedItem打開MSG文件。

2.如果您使用Redemption是一個選項,它允許直接使用RDOAttachment .EmbeddedMsg屬性訪問嵌入消息附件。

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) 

這樣你就不會得到Type Mismatch錯誤。