?
Solved

reading my inbox with vba using outlook 2003

Posted on 2013-06-28
18
Medium Priority
?
345 Views
Last Modified: 2014-10-06
just trying to read the contents of whats in my inbox hAVE A mail in there but it comes back with null any ideas


Sub disectData()
    Dim olApp As Outlook.Application
    Dim olNs As Outlook.NameSpace
    Dim olFldr As Outlook.MAPIFolder
    Dim olItms As Outlook.Items
    Dim olMail As Variant
    Dim i As Long
    Set olApp = New Outlook.Application
    Set olNs = olApp.GetNamespace("MAPI")
    Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
    Set olItms = olFldr.Items
    olItms.Sort “Subject”
    i = 1
    For Each olMail In olItms

            i = i + 1

   
    MsgBox Msg.Subject
    MsgBox Msg.Body
    Next olMail
     
    Set olFldr = Nothing
    Set olNs = Nothing
    Set olApp = Nothing
End Sub
0
Comment
Question by:sydneyguy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
  • 2
18 Comments
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 1000 total points
ID: 39284039
Does it work if you change

Set olFldr = olNs.GetDefaultFolder(olFolderInbox)

to

Set olFldr = olNs.PickFolder
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 1000 total points
ID: 39284693
To avoid the error you made, set
  Option Explicit
at the very beginning of your script.

The error is that you used the var Msg, but it is not DIMmed nor set to anything. You'll either have to use Msg as FOR var, or replace it with olMail.
0
 

Author Comment

by:sydneyguy
ID: 39297238
have jumped over to this code as it does return the email to
but when i try the email from and subject it does not like it
any help please


Function findemails2()
   
    Dim OlApp As Outlook.Application
    Dim Inbox As Outlook.MAPIFolder
    Dim InboxItems As Outlook.Items
    Dim Mailobject As Object
    Dim Email As String
    Dim Emailfrom As String
    Dim Emailsubject As String
    Set OlApp = CreateObject("Outlook.Application")
    Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
    Set InboxItems = Inbox.Items

    ' Create Text File
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("c:\email addresses.txt", True)
    ' loop to read email address from mail items.
   
    For Each Mailobject In InboxItems
       Email = Mailobject.To
       Emailfrom = Mailobject.From
       Emailsubject = Mailobject.Subject
       
       a.WriteLine (Email)
    Next
    Set OlApp = Nothing
    Set Inbox = Nothing
    Set InboxItems = Nothing
    Set Mailobject = Nothing
    a.Close

End Function
0
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
LVL 70

Expert Comment

by:Qlemo
ID: 39297310
Function findemails2()
    
    Dim OlApp As Outlook.Application
    Dim Inbox As Outlook.MAPIFolder
    Dim Mailobject As Object
    Dim EmailTo As String
    Dim EmailFrom As String
    Dim EmailSubject As String
    Set OlApp = CreateObject("Outlook.Application")
    Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)

    Dim fs, a
    ' Create Text File
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set a = fs.CreateTextFile("c:\email addresses.txt", True)
    ' loop to read email address from mail items.
    
    For Each Mailobject In Inbox.Items
       EmailTo = Mailobject.To
       EmailFrom = Mailobject.SenderEmailAddress
       EmailSubject = Mailobject.Subject
       
       a.WriteLine ("From: " & EmailFrom & " To: " & EmailTo & " Subject: " & EmailSubject)
    Next
    Set OlApp = Nothing
    Set Inbox = Nothing
    Set Mailobject = Nothing
    a.Close

End Function

Open in new window

0
 

Author Comment

by:sydneyguy
ID: 39297517
thanks is there a list around on what you can acceess and what they are
is how would i have guessed that its SenderEmailAddress instead of from as in to

 EmailTo = Mailobject.To
       EmailFrom = Mailobject.SenderEmailAddress
       EmailSubject = Mailobject.Subject
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 39297595
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39297733
Or go into the debugger, execute until you have a valid object (Mailobject in this case), and put it at the "Watch List". You can then drill down and see the properties and values.
0
 

Author Comment

by:sydneyguy
ID: 39301632
obkect
here is the object dir listing for Inbox.Items i was gusessing that the info that i am looking for is some were under here but i cannot see the
EmailTo = Mailobject.To
       EmailFrom = Mailobject.SenderEmailAddress
       EmailSubject = Mailobject.Subject

is this viewable from here
thanks for the help so far
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39301677
Set a breakpoint at my line 19, just inside the FOR EACH. At that point you should have a valid object in Mailitem, which is much easier to look at than the Items collection itself. However, we are not checking whether the item is a Mailitem, but I suppose you won't put notes or such into the inbox ;-).

If you want to see what properties and methods are available in general (without need to see values), you might be better using Object Explorer (F2) with MailItem as type.
0
 

Author Comment

by:sydneyguy
ID: 39301886
outlook mail disthave done this and here is a shot stoped in the for next loop but there is just nothing in the mailobject and i thought there should be
any ideas why
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39301957
Yes, there should be something. See which type Mailitem has - a variant without type, or a "variant/Object/MailItem"?
0
 

Author Comment

by:sydneyguy
ID: 39302045
this is how it is being defined, any help in figuring it out

Dim OlApp As Outlook.Application
    Dim Inbox As Outlook.MAPIFolder
    Dim Mailobject As Object
    Dim EmailTo As String
    Dim EmailFrom As String
    Dim EmailSubject As String
    Dim body As String
    Set OlApp = CreateObject("Outlook.Application")
    Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
    Dim olNs As Outlook.NameSpace
    Dim olFldr As Outlook.MAPIFolder
    Set OlApp = New Outlook.Application
    Set olNs = OlApp.GetNamespace("MAPI")
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39302154
Sorry, I meant Mailobject, not Mailitem, as your variable, of course.
The definition doesn't tell us what is really assigned. "Object" is generic, as is "variant". If you define Mailobject as MailItem (which is very restrictive and specific), the FOR EACH might throw an error if it doesn't get a MailItem in Inbox.Items (as said, it can be other items like calendar, notes, addresses, tasks, ...).
However, I have to admit that I have no clue why the detail vars (EMailTo ...) are set, but you do not see anything for Mailobject in your Locals subwindow.
0
 

Author Comment

by:sydneyguy
ID: 39302249
sorry that was my miss spelling it actually is mailobject maillist is jut totally wrong sorry bout that
0
 

Author Comment

by:sydneyguy
ID: 39419900
am back onto the problem at hand, got around this niggling problem by moving the funny types to another folder till i figure out how to get around them.
what i really need to do is to find the date received of the email
i have access not to
but what i need is date recieved
have tried
Emaildaterec = Mailobject.date
Emaildaterec = Mailobject.Recieved
neither of these worked.
is there a list of the object words that it uses
i have tried to drill down under dbug but still cannot find what and were they are displayed to see what its using

For Each Mailobject In Inbox.Items
       EmailTo = Mailobject.To
       EmailFrom = Mailobject.SenderEmailAddress
       EmailSubject = Mailobject.Subject
       
       a.WriteLine ("From: " & EmailFrom & " To: " & EmailTo & " Subject: " & EmailSubject)
    Next
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39453043
The correct property is ReceivedTime.

You can test for a specific type with
For Each Mailobject In Inbox.Items
       If TypeName(Mailobject) = "MailTiem" then
          EmailTo = Mailobject.To
          EmailFrom = Mailobject.SenderEmailAddress
          EmailSubject = Mailobject.Subject
          Emaildaterec = Mailobject.ReceivedTime
       
          a.WriteLine ("From: " & EmailFrom & " To: " & EmailTo & " Received: " & Remaildaterec & " Subject: " & EmailSubject)
Next

Open in new window

But you should change the name of the file you write to - "email addresses.txt" isn't really appropriate ;-).
0
 

Author Comment

by:sydneyguy
ID: 39494856
have got it runnign will revist again tomoprrow thanks for the help so far
0
 

Author Closing Comment

by:sydneyguy
ID: 40363696
thabnks for the help
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Changing a few Outlook Options can help keep you organized!
If you troubleshoot Outlook for clients, you may want to know a bit more about the OST file before doing your next job. IMAP can cause a lot of drama if removed in the accounts without backing up.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
CodeTwo Sync for iCloud (http://www.codetwo.com/sync-for-icloud?sts=6554) automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question