Question

import win32com.client
outlook=win32com.clent.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox=outlook.GetDefaultFolder(6)
messages=inbox.Items
message.messages.GetLast()
body_content=message.Body
print(body_content)

This will print the body of an email in my inbox. What I want to do is to reveal what is happening at each stage of this code so that I may better understand it, however when I try to print inbox, messages I get:

<COMObject <unknown>>

How do I reveal this so that I can begin to see what I'm working with.

I'm also looking for a place which has clear documentation surrounding using python to interact with MS Outlook, if anyone can share.

Was it helpful?

Solution

Try here:

In the outlook object model, most objects have a Class property, which returns an enumeration of type OlObjectType, saying what type of object it is. Other properties common to outlook (and indeed MS Office) object are Parent and Application.

If you really want to, it should be easy enough to make a function describe_outlook_object which returns a string with useful information. You will have to write it yourself of course.

Alternatively if you just want to explore the object model, you could do worse than hit Alt+F11 in Outlook and have a play with Visual Basic. (You will have to enable macros.)

OTHER TIPS

I cannot comment yet, but wanted to add to Ben's answer (which helped me immensely in a similar situation)

I wanted a way to scrape emails from multiple PST files/accounts in Outlook

import win32com.client

outlook_object = win32com.client.Dispatch("Outlook.Application")

namespace = outlook_object.GetNamespace("MAPI")

# collection of accounts
accounts = namespace.Folders

# number of outlook accounts
accounts_count = accounts.Count

# .Item(1) not .Item(0) because counting starts at 1
account1 = accounts.Item(1)

# collection of folders for account1
account_folders = account1.Folders
# number of folders under outlook account
account_folders_count = account_folders.Count

# print account1 folder names
for folder in range(account_folders_count):
    # must be +1 because .Folder(0) and .Item(0) do not work
    print str(folder+1)+":", account_folders.Item(folder+1)

There is a pattern to using Folders.Count and Folders.Item(1) to get down to the messages. Hopefully this helps someone, because it took me hours of googling to get to this point.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top