【问题标题】:Administrator Limited Number of Outlook Items管理员数量有限的 Outlook 项目
【发布时间】:2020-10-23 01:17:26
【问题描述】:

如何改变循环?

我真的需要帮助。我有一个宏,可以使用以下代码从消息中下载 PDF:

Sub SaveAttachmentsFromSelectedItemsPDF2()

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

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

    savedFileCountPDF = 0
    For Each currentItem In Application.ActiveExplorer.Selection
        For Each currentAttachment In currentItem.Attachments
            If UCase(Right(currentAttachment.DisplayName, 4)) = ".PDF" Then
                currentAttachment.SaveAsFile saveToFolder & "\" & _
                    Left(currentAttachment.DisplayName, Len(currentAttachment.DisplayName) - 4) & "_" & Format(Now, "yyyy-mm-dd_hh-mm-ss") & ".pdf"
                savedFileCountPDF = savedFileCountPDF + 1
            End If
        Next currentAttachment
    Next currentItem

    MsgBox "Number of PDF files saved: " & savedFileCountPDF, vbInformation

End Sub

我有一个很大的数字,大约 4k。它只让我做一些,然后在标题中给我这个信息。有没有办法更改我的代码以分组或一个一个地处理它们,而不是一次全部处理?

【问题讨论】:

  • 也许尝试弄乱this article for that error 中提到的设置?或者,如果可能的话,修改您的脚本以在保存附件后将每条消息移动到存档 pst,因为这可能会将它们从服务器中删除并避免错误?
  • 我想我只是想要一种方法来执行每条消息而不是在一个允许它超过 256 条打开消息的大循环中

标签: vba outlook


【解决方案1】:

先试试For Next,看看对象是否自动释放。

如果不成功,请检查将对象设置为nothing是否有影响。

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\pdf" '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, 4)) = UCase(".PDF") Then
                currentAttachment.SaveAsFile saveToFolder & "\" & _
                  Left(currentAttachment.DisplayName, Len(currentAttachment.DisplayName) - 4) & "_" & Format(Now, "yyyy-mm-dd_hh-mm-ss") & ".pdf"
                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

【讨论】:

    猜你喜欢
    • 2015-05-09
    • 2022-11-10
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2017-05-24
    • 2016-05-11
    • 2011-11-20
    • 1970-01-01
    相关资源
    最近更新 更多