Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 在几个项目之后,每个循环的运行时错误为13_Vba_Excel_Foreach_Runtime Error - Fatal编程技术网

Vba 在几个项目之后,每个循环的运行时错误为13

Vba 在几个项目之后,每个循环的运行时错误为13,vba,excel,foreach,runtime-error,Vba,Excel,Foreach,Runtime Error,我正在尝试在Outlook的所有子文件夹中列出Excel中的所有电子邮件: 我已经搜索和研究这几个星期了,没有任何运气 'Requires reference to Outlook library Option Explicit Public Sub ListOutlookFolders() Dim olApp As Outlook.Application Dim olNamespace As Outlook.Namespace Dim olFolder As Out

我正在尝试在Outlook的所有子文件夹中列出Excel中的所有电子邮件:

我已经搜索和研究这几个星期了,没有任何运气

'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
”需要引用Outlook库
选项显式
公共子ListOutlookFolders()
Dim olApp作为Outlook.Application
将命名空间设置为Outlook.Namespace
将文件夹设置为Outlook.MAPIFolder
变暗rngOutput As范围
变暗lngCol为长
将我设置为Outlook.MailItem
尺寸为Excel.Range
将标准表变暗为字符串
将strPath设置为字符串
设置rngOutput=ActiveSheet.Range(“A1”)
Set olApp=newoutlook.Application
设置olNamespace=olApp.GetNamespace(“MAPI”)
对于olNamespace.Folders中的每个olFolder
rngOutput=olFolder.Name
rngOutput.Offset(0,1)=olFolder.Description
设置rngOutput=rngOutput.Offset(1)
对于olFolder.Items中的每个olItem
设置rngOutput=rngOutput.Offset(1)
带rngOutput
.Offset(0,1)=m.SenderEmailAddress的发件人
以
下一个
设置rngOutput=ListFolders(olFolder,1,rngOutput)
下一个
设置olFolder=Nothing
设置olNamespace=Nothing
设置olApp=Nothing
端接头
函数列表文件夹(MyFolder作为Outlook.MAPIFolder,级别作为整数,输出作为范围)作为范围
将文件夹设置为Outlook.MAPIFolder
将我设置为Outlook.MailItem
变暗lngCol为长
对于MyFolder.Folders中的每个文件夹
输出偏移量(0,lngCol)=olFolder.Name
设置输出=输出偏移量(1)
如果(olFolder.DefaultItemType=olmailtItem)和(不是olFolder.Name=“slett post”)则
对于olFolder.Items中的每个olItem
如果olItem.Class=olMail,则
输出
.Offset(0,1)=m.SenderEmailAddress的发件人
以
设置输出=输出偏移量(1)
如果结束
接下来是0
设置输出=列表文件夹(olFolder,级别+1,输出)
如果结束
下一个文件夹
Set ListFolders=theOutput.Offset(1)
端函数
代码在10-20项中运行良好,然后在上面的行中给我一个运行时错误13,当我点击debug时,它告诉我olItem=Nothing!?-当我点击单步时,代码再次正常运行一段时间


我试图插入一个“ON ERROR”,但我的列表中没有包含所有电子邮件。

我向您展示我的代码:)

更改
以Outlook.MailItem的形式发送邮件


im作为对象

并非所有文件夹项目都是邮件项目,因此避免以这种方式对
olItem
变量进行尺寸标注。这一改变在我的机器上运行良好,而最初我的错误与您的相同