【问题标题】:Code not executing the loop due to run type error 13 outlook由于运行类型错误 13 Outlook 导致代码未执行循环
【发布时间】:2019-06-29 21:59:30
【问题描述】:

我正在尝试列出特定文件夹中的所有电子邮件主题。只要项目不是邮寄项目,即约会等,我就会收到Run time Error 13

后续问题:

1) 如何根据主题回复所有最新电子邮件,电子邮件可能在收件箱或已发送项目中。

2) 如何循环输入文件夹中的所有电子邮件,即单击“单击此处在 Microsoft Edge 上查看更多信息”可让您访问所有旧电子邮件。

Sub AccessInbox2()

'Early binding

Dim Olook As Outlook.Application ' to access all the libraries of outlook
Dim OmailItem As Outlook.MailItem ' To access emails in the inbox
Dim ONameSpace As Outlook.Namespace ' it is class which opens the gate for you to access all outlook folders. Unlike the Folder class, it exactly tells VBA which folder to use.
Dim Fol As Outlook.Folder ' Where we have emails with attachments stored
Dim Atmt As Outlook.Attachment ' a class which will help us in dealing wiht emails which as attachements
Dim TotalEmails As Long
Dim i As Integer


Set Olook = New Outlook.Application
Set OmailItem = Olook.CreateItem(olMailItem) 'to deal with emails

'messaging application protocal interface
i = 1
For Each OmailItem In Olook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Folders("Goldy").Items

    'If TypeName(OmailItem) = "MailItem" Then
    If OmailItem.Class = 43 Then

    Sheet1.Cells(i, 7).Value = OmailItem.Subject

    End If

i = i + 1
Next

End Sub

【问题讨论】:

  • outlook VBA script run-time error 13 randomly while iterating emails in a public folder 的可能重复项。查看接受的答案如何使用Object 变量,然后测试Class 的变量。您在 For Each 循环中使用了 MailItem 变量。
  • 非常感谢您的编辑。我也进行了该查询,因此包含 ('If TypeName(OmailItem) = "MailItem" ) 但它不起作用。此外,我请求帮助解决其他问题。我已经在这些问题上花了 10 个小时来找出解决方案,但还不够聪明,无法找到解决方案。
  • 注意我的第二句话来解决你的主要问题。
  • 该代码在遇到会议或约会时确实会处理少量电子邮件,我收到此错误。此外,我不知道代码如何开始其序列,即它将首先处理哪封电子邮件。我想知道是否有办法控制这种情况。抱歉,我两周前才学会了 VBA for Outlook。
  • 我建议从链接的答案中更改一件事 - 我会使用 TypeOf ... Is 而不是 TypeName

标签: excel vba email outlook


【解决方案1】:

下面的怎么样...

Option Explicit
Public Sub AccessInbox2()
'Early binding
    Dim Olook As Outlook.Application ' to access all the libraries of outlook
    ' it is class which opens the gate for you to access
    ' all outlook folders. Unlike the Folder class,
    ' it exactly tells VBA which folder to use.

    Set Olook = New Outlook.Application

    Dim Sht As Worksheet
    Set Sht = ThisWorkbook.Sheets("Sheet1")



    Dim Items As Outlook.Items
    Set Items = Olook.GetNamespace("MAPI") _
                     .GetDefaultFolder(olFolderInbox) _
                     .Folders("Goldy").Items

   Dim i As Long
   Dim LastRow As Long
   For i = Items.Count To 1 Step -1

        If TypeOf Items(i) Is Outlook.MailItem Then
            Debug.Print Items(i).Subject ' Print on Immediate Window

            With Sht

                 LastRow = .Cells(.Rows.Count, 7).End(xlUp).Row + 1
                 Debug.Print .Cells(LastRow, 7).Address ' Print on Immediate Window
                .Cells(LastRow, 7).Value = Items(i).Subject

            End With

        End If

    Next

End Sub

【讨论】:

  • 感谢 Om3R.. 这很好用。我可以在我的另外两个后续问题上挑你的脑筋吗? 1)如何根据所选主题回复所有最新电子邮件,电子邮件可能在收件箱或已发送项目中。 2) 如何循环输入文件夹中的所有电子邮件,即单击“单击此处在 Microsoft Edge 上查看更多信息”可让您访问所有旧电子邮件。
  • 我编辑了你的代码来解决这个问题我不知道如何发布它。 Items.Sort "ReceivedTime", True ' 按日期排序 If TypeOf Items(1) Is Outlook.MailItem And Items(1).Restrict(sFilter) Then
  • 我现在不在电脑旁,但稍后会提供帮助 @DJSingh 你能发布关于你有什么和尝试过的新问题吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-13
  • 1970-01-01
  • 1970-01-01
  • 2020-08-01
相关资源
最近更新 更多