Retrieve a specific folder in outlook

6 ビュー (過去 30 日間)
Zoe Zhang
Zoe Zhang 2012 年 7 月 31 日
編集済み: Walter Roberson 2015 年 6 月 2 日
Hi,
First time using outlook COM in Matlab. No id where I did wrong in the following code. Basically I'd like to get a specific email from a sub-folder in the inbox called 'Important Infor’.
outlook.GetNamespace('mapi').GetDefaultFolder('olFolderInbox')
Folders is null so I cannot retrieve the folder 'Important Infor’.
>> outlook = actxserver('Outlook.Application');
>> MyFolder = outlook.GetNamespace('mapi').GetDefaultFolder('olFolderInbox').Folders('Important Infor')
Index exceeds matrix dimensions.
Thanks a lot in advance!

採用された回答

Eric
Eric 2012 年 7 月 31 日
You need to be aware of two things:
1. olFolderInbox should be the integer 6.
2. Indexing into VBA collections such as Folders() using a string is not supported via COM. You'll have to loop through the items and find the one with the right name. Loop through using the Items property of the collection.
Here's how I got the body for a particular email in Outlook. This gets the oldest email in my inbox. I'll show this in stages to see how the tree structure is built up.
First start with
outlook = actxserver('Outlook.Application');
Then
outlook.GetNamespace('mapi').Folders(1).Item(2) %returns my Mailbox
outlook.GetNamespace('mapi').Folders(1).Item(2).Folders(1).Item(2); %returns my Inbox
outlook.GetNamespace('mapi').Folders(1).Item(2).Folders(1).Item(2).Item(1).Item(1); %returns the oldest email in my Inbox
outlook.GetNamespace('mapi').Folders(1).Item(2).Folders(1).Item(2).Item(1).Item(1).Body; %Returns the body of this message
This is by no means straightforward and the hierarchy will depend upon how you have configured Outlook. The code here should provide a good starting point, though.
Good luck,
Eric
  2 件のコメント
Zoe Zhang
Zoe Zhang 2012 年 7 月 31 日
編集済み: Zoe Zhang 2012 年 7 月 31 日
I see. Thank you very much! Is there any other ways to know that inbox is the integer 6 instead of looping? could that may be found in outlook vba?
Eric
Eric 2012 年 7 月 31 日
編集済み: Eric 2012 年 7 月 31 日
I think you meant to say that Inbox is 2. You're right, though, you could use
olFolderInbox = 6;
outlook.GetNamespace('mapi').GetDefaultFolder(olFolderInbox).Item(1).Item(1).Body
to do the same thing. I think then we have
outlook.GetNamespace('mapi').GetDefaultFolder(olFolderInbox).Item(1)
returns the root 'Inbox' directory and
outlook.GetNamespace('mapi').GetDefaultFolder(olFolderInbox).Item(1).Item(1)
returns the oldest item in that directory. That is a bit more concise than what I had written.
Other than that, though, I don't know of a simple way of finding items in VBA collections. I run into the same problem with some Excel code I have. To write data to a particular worksheet in an Excel workbook you first need to loop through the worksheets to find the one with the name you're looking for.
-Eric

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by