我对VBA和这个论坛都是新手。首先,让我说,我花了相当多的时间,试图通过做和搜索来找到我的答案。
我需要创建一个脚本,我可以添加到我的规则中删除某些电子邮件的附件。我找到了这段代码,并认为它会有所帮助。
Public Sub deleteAttach(itm As Outlook.MailItem) 
Dim objAtt As Outlook.Attachment 
Set objAtt = itm.Attachments
objAtt.itm(1).Delete
End Sub现在我似乎不能让这段代码工作了。读完之后,我意识到itm需要是一个对象,但不知何故,如果我使用Public Sub deleteAttach(ByVal Item As Object),规则就找不到我的脚本了。
我还尝试将代码更改为objATT.Delete
任何帮助都将不胜感激。谢谢。
发布于 2014-03-05 02:53:43
你确定它缺失子例程吗?当数组从索引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发布于 2014-03-05 04:41:57
您的代码永远不会读取该项目的已删除附件。你把附件拔掉了,但又忘了重新“设置”。
此外,您需要在代码顶部使用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如果你不养成在以编程方式修改电子邮件后进行“保存”的习惯,你会遇到与此类似的晦涩难懂的Outlook问题。
或者,如果您想要删除所有附件,您可以向后迭代列表:
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发布于 2015-01-28 06:45:22
打开您认为规则应该处理的项目并运行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 Subhttps://stackoverflow.com/questions/22180099
复制相似问题