首页 > 解决方案 > 设置电子邮件类别

问题描述

随着循环的进行,我正在尝试为每封电子邮件添加一个类别:

Option Explicit

Sub SaveAttachmentsFromSelectedItemsPDF2_ForNext()

    Dim currentItem As Object
    Dim currentAttachment As Attachment
    Dim saveToFolder As String
    Dim savedFileCountPDF As Long
    
    Dim i As Long
    Dim j As Long

    saveToFolder = "c:\dev\outlookexport" 'change the path accordingly

    savedFileCountPDF = 0
    
    For i = 1 To ActiveExplorer.Selection.Count
        
        Set currentItem = ActiveExplorer.Selection(i)
    
        For j = 1 To currentItem.Attachments.Count
                    
            Set currentAttachment = currentItem.Attachments(j)
            
            If UCase(Right(currentAttachment.DisplayName, 5)) = UCase(".xlsx") Then
                currentAttachment.SaveAsFile saveToFolder & "\" & _
                Left(currentAttachment.DisplayName, Len(currentAttachment.DisplayName) - 5) & ".xlsx"
                savedFileCountPDF = savedFileCountPDF + 1
            End If
            
            ' If For Next does not release memory automatically then
            '  uncomment to see if this has an impact
            'Set currentAttachment = Nothing
            
        Next
        
        ' If For Next does not release memory automatically then
        '  uncomment to see if this has an impact
        'Set currentItem = Nothing
        
    Next
    
    MsgBox "Number of PDF files saved: " & savedFileCountPDF, vbInformation

End Sub

设置类别的代码似乎是:

.Categories = "MyCategory"

当我将它添加到我的代码(在 下For i = 1...)时,它给了我一个错误:

“不合格的参考”

当循环沿着选定的电子邮件列表向下移动时,如何向电子邮件添加类别?

标签: vbaoutlook

解决方案


您需要在代码中指定对象:

For i = 1 To ActiveExplorer.Selection.Count

    Set currentItem = ActiveExplorer.Selection(i)

    currentItem.Categories = "MyCategory"
Next

推荐阅读