Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
214 views
in Technique[技术] by (71.8m points)

excel - Code not executing the loop due to run type error 13 outlook

I'm trying to list out all the subjects of emails in a particular folder. I am getting a Run time Error 13 as soon as item not mail item, i.e.appointment, etc.

Follow-up question:

1) How do reply to all to a latest email based the subject and email could be in Inbox or sent items.

2) How to loop in all emails in a folder, i.e. clicking "Click here to view more on Microsoft edge" give you access to all old emails.

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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

How about the following...

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...