[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

reading my inbox with vba using outlook 2003

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
sydneyguy
Asked:
sydneyguy
  • 9
  • 7
  • 2
2 Solutions
 
CodeCruiserCommented:
Does it work if you change

Set olFldr = olNs.GetDefaultFolder(olFolderInbox)

to

Set olFldr = olNs.PickFolder
0
 
QlemoC++ DeveloperCommented:
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
 
sydneyguyAuthor Commented:
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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
QlemoC++ DeveloperCommented:
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
 
sydneyguyAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
sydneyguyAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
sydneyguyAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
Yes, there should be something. See which type Mailitem has - a variant without type, or a "variant/Object/MailItem"?
0
 
sydneyguyAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
sydneyguyAuthor Commented:
sorry that was my miss spelling it actually is mailobject maillist is jut totally wrong sorry bout that
0
 
sydneyguyAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
sydneyguyAuthor Commented:
have got it runnign will revist again tomoprrow thanks for the help so far
0
 
sydneyguyAuthor Commented:
thabnks for the help
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 7
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now