0

説明:

フォルダー内の選択した電子メールをループし、いくつかの情報を .csv ファイルに書き留める Outlook マクロがあります。失敗する前に250まで完全に機能します。コードの一部を次に示します。

Open strSaveAsFilename For Append As #1

CountVar = 0
For Each objItem In Application.ActiveExplorer.Selection
    DoEvents
    If objItem.VotingResponse <> "" Then
        CountVar = CountVar + 1
        Debug.Print "   " & CountVar & ". " & objItem.SenderName
        Print #1,  & objItem.SenderName & "," &  objItem.VotingResponse
    Else
        CountVar = CountVar + 1
        Debug.Print "   " & CountVar & ". " & "Moving email from: " & Chr(34) & objItem.SenderName & Chr(34) & " to: Special Cases sub-folder"
        objItem.Move CurrentFolderVar.Folders("Special Cases")
    End If
Next
Close #1

問題

このコードが 250 通の電子メールを処理した後、次のスクリーンショットが表示されます。

http://i.stack.imgur.com/yt9P8.jpg

サーバーを休ませるために「待機」機能を追加して、サーバーにすばやくクエリを実行しないようにしましたが、同じ時点で同じエラーが発生します。

4

2 に答える 2

2

@76mel に感謝します。私が大いに参照した別の質問に対する彼の回答に対して。250 を超えるアイテムを開くことはできず、Outlook は何があってもマクロが終了するまですべてのアイテムをメモリに保持するというOutlook ( source )の組み込みの制限であることがわかりました。選択した各アイテムをループする代わりに、回避策:

For Each objItem In Application.ActiveExplorer.Selection

親フォルダーをループできます。私はこのようなことができると思いました:

For Each objItem In oFolder.Items

しかし、メールを削除または移動すると、リストが 1 つ上に移動するため、メールがスキップされることがわかりました。別の回答で見つけたフォルダーを反復処理する最良の方法は、これを行うことです。

For i = oFolder.Items.Count To 1 Step -1 'Iterates from the end backwards
Set objItem = oFolder.Items(i)

これは、解析するフォルダーを選択するように求めるプロンプトを表示し、そのフォルダーに「不在」返信用のサブディレクトリと、「RE:」で始まるすべての電子メールを配置する「特殊なケース」を作成するコード全体です。

Sub SaveItemsToExcel()
    Debug.Print "Begin SaveItemsToExcel"

    Dim oNameSpace As Outlook.NameSpace
    Set oNameSpace = Application.GetNamespace("MAPI")
    Dim oFolder As Outlook.MAPIFolder
    Set oFolder = oNameSpace.PickFolder
    Dim IsFolderSpecialCase As Boolean
    Dim IsFolderOutofOffice As Boolean
    IsFolderSpecialCase = False
    IsFolderOutofOffice = False

    'If they don't check a folder, exit.
    If oFolder Is Nothing Then
        GoTo ErrorHandlerExit
    ElseIf oFolder.DefaultItemType <> olMailItem Then 'Make sure folder is not empty
        MsgBox "Folder does not contain mail messages"
        GoTo ErrorHandlerExit
    End If

    'Checks to see if Special Cases Folder and Out of Office folders exists. If not, create them
    For i = 1 To oFolder.Folders.Count
        If oFolder.Folders.Item(i).name = "Special Cases" Then IsFolderSpecialCase = True
        If oFolder.Folders.Item(i).name = "Out of Office" Then IsFolderOutofOffice = True
    Next
    If Not IsFolderSpecialCase Then oFolder.Folders.Add ("Special Cases")
    If Not IsFolderOutofOffice Then oFolder.Folders.Add ("Out of Office")

    'Asks user for name and location to save the export
    objOutputFile = CreateObject("Excel.application").GetSaveAsFilename(InitialFileName:="TestExport" & Format(Now, "_yyyymmdd"), fileFilter:="Outlook Message (*.csv), *.csv", Title:="Export data to:")
    If objOutputFile = False Then Exit Sub
    Debug.Print "   Will save to: " & objOutputFile & Chr(10)

    'Overwrite outputfile, with new headers.
    Open objOutputFile For Output As #1
    Print #1, "User ID,Last Name,First Name,Company Name,Subject,Vote Response,Recived"

    ProcessFolderItems oFolder, objOutputFile

    Close #1

    Set oFolder = Nothing
    Set oNameSpace = Nothing
    Set objOutputFile = Nothing
    Set objFS = Nothing

    MsgBox "All complete! Emails requiring attention are in the " & Chr(34) & "Special Cases" & Chr(34) & " subdirectory."
    Debug.Print "End SaveItemsToExcel."
    Exit Sub
ErrorHandlerExit:
    Debug.Print "Error in code."
End Sub

Sub ProcessFolderItems(oParentFolder, ByRef objOutputFile)
    Dim oCount As Integer
    Dim oFolder As Outlook.MAPIFolder
    Dim MessageVar As String
    oCount = oParentFolder.Items.Count
    Dim CountVar As Integer
    Dim objItem As Outlook.MailItem

    CountVar = 0

    For i = oParentFolder.Items.Count To 1 Step -1 'Iterates from the end backwards
    Set objItem = oParentFolder.Items(i)
        DoEvents
        If objItem.Class = olMail Then
            If objItem.VotingResponse <> "" Then
                CountVar = CountVar + 1
                Debug.Print "   " & CountVar & ". " & GetUsername(objItem.SenderName, objItem.SenderEmailAddress) & "," & objItem.SenderName & "," & GetCompany(objItem.SenderName) & "," & Replace(objItem.Subject, ",", "") & "," & objItem.VotingResponse & "," & objItem.ReceivedTime
                Print #1, GetUsername(objItem.SenderName, objItem.SenderEmailAddress) & "," & objItem.SenderName & "," & GetCompany(objItem.SenderName) & "," & Replace(objItem.Subject, ",", "") & "," & objItem.VotingResponse & "," & objItem.ReceivedTime
            ElseIf objItem.Subject Like "*Out of Office*" Then
                CountVar = CountVar + 1
                Debug.Print "   " & CountVar & ". " & "Moving email from: " & Chr(34) & objItem.SenderName & Chr(34) & " to the, " & Chr(34) & "Out of Office" & Chr(34) & " sub-folder"
                objItem.Move oParentFolder.Folders("Out of Office")
            Else
                CountVar = CountVar + 1
                Debug.Print "   " & CountVar & ". " & "Moving email from: " & Chr(34) & objItem.SenderName & Chr(34) & " to the, " & Chr(34) & "Special Cases" & Chr(34) & " sub-folder"
                objItem.Move oParentFolder.Folders("Special Cases")
            End If
        End If
    Next i
    Set objItem = Nothing
End Sub

Function GetUsername(SenderNameVar As String, SenderEmailVar As String) As String
    On Error Resume Next
    GetUsername = ""
    GetUsername = CreateObject("Outlook.Application").CreateItem(olMailItem).Recipients.Add(SenderNameVar).AddressEntry.GetExchangeUser.Alias
    If GetUsername = "" Then GetUsername = Mid(SenderEmailVar, InStrRev(SenderEmailVar, "=", -1) + 1)
End Function

Function GetCompany(SenderNameVar)
    On Error Resume Next
    GetCompany = ""
    GetCompany = CreateObject("Outlook.Application").CreateItem(olMailItem).Recipients.Add(SenderNameVar).AddressEntry.GetExchangeUser.CompanyName
End Function
于 2012-11-13T15:51:21.240 に答える