Ich habe dieses seltsame Problem mit diesem Code irgendwo gefunden. Ich versuche, alle E-Mails in Excel in allen Unterordnern von Outlook aufzulisten:VBA (Excel) Laufzeitfehler 13 auf für jede Schleife
Ich habe gesucht und recherchiert seit Wochen ohne Glück.
'Requires reference to Outlook library
Option Explicit
Public Sub ListOutlookFolders()
Dim olApp As Outlook.Application
Dim olNamespace As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim rngOutput As Range
Dim lngCol As Long
Dim olItem As Outlook.MailItem
Dim rng As Excel.Range
Dim strSheet As String
Dim strPath As String
Set rngOutput = ActiveSheet.Range("A1")
Set olApp = New Outlook.Application
Set olNamespace = olApp.GetNamespace("MAPI")
For Each olFolder In olNamespace.Folders
rngOutput = olFolder.Name
rngOutput.Offset(0, 1) = olFolder.Description
Set rngOutput = rngOutput.Offset(1)
For Each olItem In olFolder.Items
Set rngOutput = rngOutput.Offset(1)
With rngOutput
.Offset(0, 1) = olItem.SenderEmailAddress ' Sender
End With
Next
Set rngOutput = ListFolders(olFolder, 1, rngOutput)
Next
Set olFolder = Nothing
Set olNamespace = Nothing
Set olApp = Nothing
End Sub
Function ListFolders(MyFolder As Outlook.MAPIFolder, Level As Integer, theOutput As Range) As Range
Dim olFolder As Outlook.MAPIFolder
Dim olItem As Outlook.MailItem
Dim lngCol As Long
For Each olFolder In MyFolder.Folders
theOutput.Offset(0, lngCol) = olFolder.Name
Set theOutput = theOutput.Offset(1)
If (olFolder.DefaultItemType = olMailItem) And (Not olFolder.Name = "Slettet post") Then
For Each olItem In olFolder.Items
If olItem.Class = olMail Then
With theOutput
.Offset(0, 1) = olItem.SenderEmailAddress ' Sender
End With
Set theOutput = theOutput.Offset(1)
End If
Next olItem <--- ERROR 13 here
End If
If olFolder.Folders.Count > 0 Then
Set theOutput = ListFolders(olFolder, Level + 1, theOutput)
End If
Next olFolder
Set ListFolders = theOutput.Offset(1)
End Function
Der Code läuft für 10-20 Artikel in Ordnung und gibt mir dann einen Laufzeitfehler 13 in der Zeile oben angegeben, und wenn ich debug traf es sagt mir, dass olItem ist = Nothing!? - Wenn ich einen einzelnen Schritt drücke, läuft der Code wieder eine Weile gut.
Ich habe versucht, einen "ON ERROR" einzufügen, aber dann enthält meine Liste nicht alle E-Mails.
Ich bin ein Neuling bei der Programmierung von VBA also bitte mit mir blank.
Vielen Dank im Voraus
Vielen Dank! es funktioniert wie Charme! – Axbogen
Netter Fang @ Brettdj – Jesse