Solved

reading my inbox with vba using outlook 2003

Posted on 2013-06-28
18
295 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
  • 9
  • 7
  • 2
18 Comments
 
LVL 83

Accepted Solution

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

Set olFldr = olNs.GetDefaultFolder(olFolderInbox)

to

Set olFldr = olNs.PickFolder
0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 250 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
 
LVL 68

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 68

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 68

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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 68

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 68

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 68

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Resolve DNS query failed errors for Exchange
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now