Excel vba to extract outlook email --run error 438

bnfkru4567

New Member
Joined
Aug 20, 2017
Messages
10
Hi everyone

I need your expert help !!

Please refer to the code below. The code will be in Excel VBA (I am using Excel and outlook 2013 version) and then run it in order to extract a large amount of email in outlook


I have 3 questions and need your help


  1. Let's say my "Inbox" has around 400+ emails and while I was running the following code, the error 438 came out when the code was searching up to 23 email. How can I solve it out?
  2. Please refer to the code below:

    <code>Dim olMail As Variant <-----------------</code>Should I use "object" or "variant"? Which is better and why?
  3. In the near future, my Outlook will have more than 1 email account. Let's say
    the first email account is <code>abc@def.com</code> and
    the second email account is <code>abcd1@abc.com</code>.
    In future, I will sometimes need to run the code for the first email account and sometimes for second email account.

    How can I switch them?
    --------------------------------------------------------------

    <code>Sub GetFromInbox()
    Dim olApp As Outlook.Application
    Dim olNs As Namespace
    Dim Fldr As MAPIFolder
    Dim olMail As Variant
    Dim i, ij As Integer
    Dim tt As Date
    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI") <questions 2---how="" can="" i="" change="" it="" in="" here="" to="" add="" more="" 1="" email="" address?????=""></questions></code>
    <code></code>
    <code></code> <code>

    Set Fldr = olNs.GetDefaultFolder(olFolderInbox)

    i = 1
    ij = 0</code>
    <code>x = Date
    For Each olMail In Fldr.Items
    ij = ij + 1

    'If IsNumeric((Format(olMail.ReceivedTime, "dd/mm/yy"))) Then

    Sheets("test").Range("a1").Select
    Sheets("test").Range("I1").Clear
    Sheets("test").Range("I2") = ij

    Sheets("test").Range("I1").Value = (Format(olMail.ReceivedTime, "dd/mm/yy")) <---<code>Question 1 : while ij was counting up to 23, the error 438 stopped in here?? I have no idea why !! ---></code>


    Sheets("test").Range("I1").NumberFormat = "dd/mm/yy"

    tt = Sheets("test").Range("I1")

    ' MsgBox ("Y-tt=" & tt & " receivedtime=" & olMail.ReceivedTime)


    'Else
    'tt = 0
    'MsgBox ("N-tt=" & tt & " receivedtime=" & olMail.ReceivedTime)
    'End If

    ' tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))

    If tt >= Range("H1") Then <------------H1 is a date and let say 15/01/17

    'If InStr(olMail.Subject, "others") > 0 And tt >= Range("h1") Then
    If InStr(olMail.Subject, "others") > 0 Then

    ActiveSheet.Range("h2") = "y"
    ActiveSheet.Cells(i, 1).Value = olMail.Subject
    ActiveSheet.Cells(i, 2).Value = olMail.ReceivedTime
    ActiveSheet.Cells(i, 3).Value = olMail.SenderName
    tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
    ActiveSheet.Cells(i, 4).Value = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
    ' tt = ActiveSheet.Cells(i, 4).Value
    ActiveSheet.Cells(i, 5).Value = (Format(olMail.ReceivedTime, "hh:mm"))

    MsgBox ("tt=" & tt)

    i = i + 1
    End If

    Else
    Sheets("test").Range("h2") = "N"

    End If
    Next olMail

    Set Fldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing
    'tt = ""
    End Sub</code>
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
- A folder may contain other objects such as delivery receipts, see code below.
- Do you still need help with the account issue?


Code:
Sub GetFromInbox()
Dim olApp As Outlook.Application, olNs As Namespace, Fldr As MAPIFolder, olMail As Object, _
x, i, ij%, tt As Date, ts As Worksheet
Set ts = Sheets("test")
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
i = 1: ij = 0
x = Date
ts.[i1].NumberFormat = "dd/mm/yy"
For Each olMail In Fldr.Items
    If TypeOf olMail Is MailItem Then                           ' only mail items
      ij = ij + 1
      ts.[a1].Activate
      ts.Range("I1").Clear
      ts.Range("I2") = ij
      ts.Range("I1") = (Format(olMail.ReceivedTime, "dd/mm/yy"))
      tt = Sheets("test").Range("I1")
      If InStr(olMail.Subject, "others") > 0 Then
          ActiveSheet.Range("h2") = "y"
          ActiveSheet.Cells(i, 1) = olMail.Subject
          ActiveSheet.Cells(i, 2) = olMail.ReceivedTime
          ActiveSheet.Cells(i, 3) = olMail.SenderName
          tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
          ActiveSheet.Cells(i, 4) = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
          ActiveSheet.Cells(i, 5) = (Format(olMail.ReceivedTime, "hh:mm"))
          MsgBox ("tt=" & tt)
          i = i + 1
      End If
    End If
Next
Set Fldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub
 
Upvote 0
Worf :Thanks for your help ! you are right as in my inbox , there are a few "delivery receipts" !!! That;s why

Question 1 : The above For Each olmail next code is starting to read each email from the oldest to newest !!! So how can I change the code in order to read from the newest to oldest ?

Question 2:
In the near future, my Outlook will have more than 1 email account. Let's say
the first email account is <code>abc@def.com</code> and
the second email account is <code>abcd1@abc.com</code>.
In future, I will sometimes need to run the code for the first email account and sometimes for second email account.

How can I switch them?
 
Upvote 0
1) You can sort the list on the worksheet, see code below.
2) I am on Outlook 2007 today and it does not have the delivery store property. See below how it would look like:

Code:
Sub GetFromInbox()
Dim olApp As Outlook.Application, olNs As Namespace, Fldr As MAPIFolder, olMail As Object, _
x, i, ij%, tt As Date, ts As Worksheet
Set ts = Sheets("test")
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
i = 1: ij = 0
x = Date
ts.[i1].NumberFormat = "dd/mm/yy"
For Each olMail In Fldr.Items
    If TypeOf olMail Is MailItem Then                           ' only mail items
      ij = ij + 1
      ts.[a1].Activate
      ts.Range("I1").Clear
      ts.Range("I2") = ij
      ts.Range("I1") = (Format(olMail.ReceivedTime, "dd/mm/yy"))
      tt = Sheets("test").[i1]
      If InStr(olMail.Subject, "others") > 0 Then
          ActiveSheet.[h2] = "y"
          ActiveSheet.Cells(i, 1) = olMail.Subject
          ActiveSheet.Cells(i, 2) = olMail.ReceivedTime
          ActiveSheet.Cells(i, 3) = olMail.SenderName
          tt = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
          ActiveSheet.Cells(i, 4) = CDate(Format(olMail.ReceivedTime, "dd/mm/yy"))
          ActiveSheet.Cells(i, 5) = (Format(olMail.ReceivedTime, "hh:mm"))
          MsgBox ("tt=" & tt)
          i = i + 1
      End If
    End If
Next
SortRange ActiveSheet, ActiveSheet.[a:e], ActiveSheet.[b1]
Set Fldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub

Sub SortRange(ws As Worksheet, r As Range, k As Range)
ws.Sort.SortFields.Clear
ws.Sort.SortFields.Add k, xlSortOnValues, xlDescending, , 0
With ws.Sort
    .SetRange r
    .Header = xlNo
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = 1
    .Apply
End With
End Sub

Code:
[COLOR=#000088][FONT=inherit]For [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Each[/FONT][/COLOR][COLOR=#000000][FONT=inherit] oAccount [/FONT][/COLOR][COLOR=#000088][FONT=inherit]In[/FONT][/COLOR][COLOR=#000000][FONT=inherit] Application[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]Session[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]Accounts
  [/FONT][/COLOR][COLOR=#000088][FONT=inherit]If[/FONT][/COLOR][COLOR=#000000][FONT=inherit] oaccount [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#008800][FONT=inherit]"1@email.com"[/FONT][/COLOR][COLOR=#000088][FONT=inherit]then[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
    [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Set[/FONT][/COLOR][COLOR=#000000][FONT=inherit] store [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] oaccount[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]DeliveryStore
    [/FONT][/COLOR][COLOR=#000088][FONT=inherit]Set[/FONT][/COLOR][COLOR=#000000][FONT=inherit] folder [/FONT][/COLOR][COLOR=#666600][FONT=inherit]=[/FONT][/COLOR][COLOR=#000000][FONT=inherit] store[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]GetDefaultFolder[/FONT][/COLOR][COLOR=#666600][FONT=inherit]([/FONT][/COLOR][COLOR=#000000][FONT=inherit]olFolderInbox[/FONT][/COLOR][COLOR=#666600][FONT=inherit])  [/FONT][/COLOR][COLOR=#880000][FONT=inherit]'here it selects the inbox folder of account.[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
    [/FONT][/COLOR][COLOR=#000088][FONT=inherit]For [/FONT][/COLOR][COLOR=#000088][FONT=inherit]each[/FONT][/COLOR][COLOR=#000000][FONT=inherit] item [/FONT][/COLOR][COLOR=#000088][FONT=inherit]in[/FONT][/COLOR][COLOR=#000000][FONT=inherit] folder[/FONT][/COLOR][COLOR=#666600][FONT=inherit].[/FONT][/COLOR][COLOR=#000000][FONT=inherit]items
      [/FONT][/COLOR][COLOR=#880000][FONT=inherit]' Code goes here[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
    [/FONT][/COLOR][COLOR=#000088][FONT=inherit]next[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
  [/FONT][/COLOR][COLOR=#000088][FONT=inherit]end [/FONT][/COLOR][COLOR=#000088][FONT=inherit]if[/FONT][/COLOR][COLOR=#000000][FONT=inherit]
[/FONT][/COLOR][COLOR=#000088][FONT=inherit]next[/FONT][/COLOR]
 
Last edited:
Upvote 0
Hi Worf

If I am using Outlook and Excel 2013 version , do I have to keep "Deliverystore" --->
Set store = oaccount.DeliveryStore


In addition, let say I do this way -->
Sheets("test").Range("A1") to store first email address and allow user to change the email address at any time
so can I change the code
Set store = Sheets("test").Range("A1")
Am I right or wrong ?
 
Upvote 0
An example:


Code:
' Outlook module
Sub Test()
Dim a As Account, f As Folder
' sample output:
' 1
' [EMAIL="user@provider.com"]user@provider.com[/EMAIL]
' [URL="file://\\user@provider.com\Inbox"]\\user@provider.com\Inbox[/URL]
' John Smith
Set a = Session.Accounts(1)
Set f = a.DeliveryStore.GetDefaultFolder(olFolderInbox)
MsgBox Session.Accounts.Count & vbLf & a.DisplayName & _
vbLf & f.FolderPath & vbLf & f.Items(2).To
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,879
Messages
6,175,147
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top