2014-03-04 32 views
3

我是VBA和這個論壇的新手。首先讓我說,我花了相當多的時間試圖找到我的答案,通過做和搜索。通過規則刪除附件的Outlook腳本

我需要創建一個腳本,我可以添加到我的規則中以從某些電子郵件中刪除附件。我發現這個代碼,並認爲這將有所幫助。

Public Sub deleteAttach(itm As Outlook.MailItem) 
Dim objAtt As Outlook.Attachment 
Set objAtt = itm.Attachments 
objAtt.itm(1).Delete 
End Sub 

現在我似乎無法得到此代碼的工作。閱讀後,我意識到,它必須是一個對象,但不知何故,如果我使用Public Sub deleteAttach(ByVal Item As Object)該規則沒有找到我的腳本。

我也試圖更改代碼以

任何幫助將非常感激。謝謝。

+0

請注意,爲了讓Outlook來識別你的腳本作爲規則的一部分運行,那麼它必須包括類型Outlook.MailItem的參數或Outlook.MeetingItem。更多關於這[這裏](http://stackoverflow.com/a/17493565/1958691)。如果您發現以下解決方案可行,請將其標記爲幫助他人的正確答案。 – Taliesin

回答

0

你確定它缺少子程序嗎?作爲數組索引開始0

Public Sub deleteAttach(itm As Outlook.MailItem) 
'Dim objAtt As Outlook.Attachment 
'Set objAtt = itm.Attachments 'AttachmentS collection 
'objAtt.itm(0).Delete 'note the 0 starts the indexing 
'or 
'for each att in objAtt 
' att.Delete 
'next 
For j = itm.Attachments.Count To 1 Step -1 
     itm.Attachments.Remove (j) 
Next j 
itm.Save 'Save the mail item without attachments 
End Sub 
+0

當我運行上面的coed時,出現以下錯誤:運行時錯誤'13'類型不匹配。這是調試器顯示錯誤的地方:Set objAtt = itm.Attachments – user3380278

+0

啊,我看到了這個問題。這是一個集合與個人依戀。我做了一些編輯,現在就試試。 –

+0

感謝您的幫助,代碼現在可以正常工作,但不會刪除附件。我正在測試它的電子郵件中只有1個附件。任何線索? – user3380278

0

你的代碼永遠不會閱讀刪除附件的項目它設置爲第二項。您將附件關閉,但忘了再次「設置」。

此外,您需要在代碼頂部使用Option Explicit

Public Sub deleteAttach(itm As Outlook.MailItem) 
Dim objAtt As Outlook.Attachment 
Set objAtt = itm.Attachments 'this creates a "new" attachment you then delete from 
objAtt.itm(1).Delete 'This line makes no sense if you think about it 
End Sub 

嘗試以下操作:

Public Sub deleteAttach(itm As Outlook.MailItem) 
    itm.Attachments(1).Delete 
    itm.Save  
End Sub 

你會遇到類似這樣的晦澀觀的問題,如果你不編程方式修改後的電子郵件做「保存」的習慣得到。

或者,如果你想擺脫所有附件,您可以向後遍歷列表:

Public Sub deleteAttach(itm As Outlook.MailItem) 
    dim i as integer 
    for i = ubound(itm.Attachments) to lbound(itm.Attachments) 
     itm.Attachments(i).Delete 
    next i 
    itm.Save  
End Sub 
+0

嗨,謝謝你的幫助。當我嘗試運行代碼以擺脫所有附件時,這是我得到的錯誤:編譯錯誤:在UBound上輸入類型不匹配。同樣在你提供的第一個代碼上,它不刪除附件。 – user3380278

+0

還有什麼建議嗎? – user3380278

0

打開你認爲規則應該處理和運行deleteAttach_test的項目。

Private Sub deleteAttach_test() 
    Dim currItem As mailitem 
    Set currItem = ActiveInspector.currentItem 
    deleteAttach currItem 
End Sub 

Public Sub deleteAttach(itm As Outlook.mailitem) 
    ' There is only one attachment in the mail 
    itm.Attachments(1).Delete 
    itm.Save 
End Sub 

回覆:「規則沒有找到我的腳本。」

當您將「RunAScript」添加到規則時,您可以選擇腳本。如果郵件是調用規則的郵件,則代碼必須運行。


如果可以有多個附件:

Private Sub deleteAttach_V1_test() 
    Dim currItem As mailitem 
    Set currItem = ActiveInspector.currentItem 
    deleteAttach_V1 currItem 
End Sub 

Public Sub deleteAttach_V1(itm As Outlook.mailitem) 
    Dim j As Long 

    ' Delete in reverse, the highest numbered remaining item each time 
    For j = itm.Attachments.count To 1 Step -1 
     itm.Attachments(j).Delete 
    Next j 
    itm.Save 
End Sub 

Private Sub deleteAttach_V2_test() 
    Dim currItem As mailitem 
    Set currItem = ActiveInspector.currentItem 
    deleteAttach_V2 currItem 
End Sub 

Public Sub deleteAttach_V2(itm As Outlook.mailitem) 
    Dim i As Long 

    For i = 1 To itm.Attachments.count 
     ' Delete the new first item each time 
     itm.Attachments(1).Delete 
    Next i 
    itm.Save 
End Sub 
相關問題