Jump to content

Learning Outlook COM


VeeDub
 Share

Recommended Posts

Hi,

I have started using Outlook COM, I found a Microsoft Outlook VBA Language Reference here which has a lot of info and is useful because it identifies all the collections, methods and properties that are available. However the info is a reference rather than a tutorial so you still have to work out how to do things.

I have established how to identify the number of messages in default folders (like InBox and Sent Items) which I figure is the first step in learning how to step through the messages and perform the desired action.

Global Const $olFolderInBox = 6
Global Const $olFolderSentMail = 5

$oOutlook = ObjCreate("Outlook.Application")
$oNameSpace = $oOutlook.GetNamespace("MAPI")
$InBox_Messages = $oNameSpace.GetDefaultFolder($olFolderInBox).Items

MsgBox(0,"InBox Messages",$InBox_Messages.Count)

$SentItems_Messages = $oNameSpace.GetDefaultFolder($olFolderSentMail).Items

MsgBox(0,"Sent Items Messages",$SentItems_Messages.Count)

However one aspect that I don't get is how to identify all folders that contain messages, that is, folders other than the Default Folders. In the above example I am working with properties of the DefaultFolders, but if I create a folder (Test) that contains message items at the same level as InBox

e.g.

- InBox

- Contacts

- Sent Items

- Test

Then when I run the script, because the Test folder is not a sub-folder of InBox or Sent Items the Test folder is not included in the selection.

I am pretty sure the answer is to be found somewhere in the reference but if some guru can save me some searching that would be good.

Thanks

VW

Link to comment
Share on other sites

I've worked out one way of listing all the folders, but I think it ought to be possible to start at the Root folder, I just don't know how to do that at the moment

Global Const $olFolderInBox = 6

$oOutlook = ObjCreate("Outlook.Application")
$oNameSpace = $oOutlook.GetNamespace("MAPI")

; Start inside the InBox
$InBox_Messages = $oNameSpace.GetDefaultFolder($olFolderInBox).Items

; Go up one level
$InBox = $InBox_Messages.Parent

; Go up another level
$Folder_Root = $InBox.Parent

; List the folders
For $Count = 1 To $Folder_Root.Folders.Count
    
    MsgBox(0,"Folder",$Folder_Root.Folders.Item($Count).Name)
    
Next

This code allows you to access the entire folder hierarchy

VW

Link to comment
Share on other sites

This piece of code correctly identifies the number of attachments in a message, but when it steps through the index it does not list out the details of the individual attachment items.

For $position = 1 To $InBox.Item($InBox_Folder).Items.Count

; Record message details
$message = $InBox.Item($InBox_Folder).Items($position)
                    
If $message.Attachments.Count = 0 Then
        FileWriteLine($Event_Log,"Subject: " & $message.Subject & @TAB & "Sender: " & $message.SenderEmailAddress & @TAB & "No Attachments")
Else                    
    For $Attachment_Index = 1 To $message.Attachments.Count
        FileWriteLine($Event_Log,"Subject: " & $message.Subject & @TAB & "Sender: " & $message.SenderEmailAddress & @TAB & "Attachment: " & $Attachment_Index & " " & $message.Attachments($Attachment_Index))                                          
    Next
EndIf
Next

What I see in the output for a message with attachments is:

Attachment: 1 p

Has anyone seen this type of output before and can suggest what I need to do to display the field correctly?

Thanks

VW

Link to comment
Share on other sites

I have played around with this some more and the reason that

$message.Attachments($Attachment_Index)
doesn't display the attachment item is that the expression is not a string according to the IsString function.

Although from my reading of the Outlook Visual Basic Reference I am using the correct expression, so perhaps there is a bug in AutoIt. OTOH as I don't claim to be an Outlook COM expert, perhaps I'm just using the wrong expression for this object.

Anyway for the time being I'll have to accept that I cannot list the individual attachments to a message.

Link to comment
Share on other sites

Hi,

Some object values in AutoIt require ".value" at the end, else the object is returned only as object?

try that?

Best, Randall

Hi Randall,

Assume you meant syntax like this

$message.Attachments($Attachment_Index).Value

Unfortunately just get an error when I run the script, but thanks for the advice.

I wonder whether other people have experienced situations where certain COM objects that ought to work, don't for some reason. I think I have got the expression right, but with the expression not working, of course I am not sure.

I can't see that I can use the COM error-handling to troubleshoot because I don't receive an error when I use the expression that I believe is correct, I just don't get a meaningful result.

In any event I have to say that I am impressed that you can actually do this type of programming with AutoIt, AutoIt is extremely versatile.

Cheers,

VW

Link to comment
Share on other sites

Are you using latest beta?

There was some error in COM part corrected in one of most recent versions related to such syntax:

$message.Attachments($Attachment_Index).Value

EDIT:

3.2.1.9 (13th October, 2006) (Beta)

- Fixed: COM property write fails when using arguments.

Edited by Zedna
Link to comment
Share on other sites

You need to examine the .Filename property of the attachment object in question:

$var = $message.Attachments($Attachment_Index).Filename

That will return the filename of the attachment at index $Attachment_Index.

Hope that helps.

-S

(Yet Another) ExcelCOM UDF"A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly...[indent]...specialization is for insects." - R. A. Heinlein[/indent]
Link to comment
Share on other sites

You need to examine the .Filename property of the attachment object in question:

$var = $message.Attachments($Attachment_Index).Filename

That will return the filename of the attachment at index $Attachment_Index.

That was the solution, you're a genius ;) , and clearly I still have a lot to learn about COM.

Cheers,

VW

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...