Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Filter(?) for Win32 OLE Find method

by banduwgs (Beadle)
on Aug 06, 2003 at 14:18 UTC ( #281408=perlquestion: print w/replies, xml ) Need Help??
banduwgs has asked for the wisdom of the Perl Monks concerning the following question:

I am dealing with Win32::OLE Outlook.Application objects. I try to apply find method over an Items collection object in following code.

$item = $items->Find( '[Subject] = Test Subject 01' );

This works fine with entire field name. (It has the power of case ignore and ignoring parts like "RE:" at the begining.)

I want to modify it to match even a portion of the given field. i.e It should return same item set (probebly more) providing the filter string '[Subject] = Test' instead of '[Subject] = Test Subject 01' in the above code

MSDN Filter Syntax Reference discuss the operator Contain, but I can't apply it in Perl. Do you have any suggesion?

Thanks in advance - SB

Replies are listed 'Best First'.
Re: Filter(?) for Win32 OLE Find method
by tcf22 (Priest) on Aug 06, 2003 at 16:17 UTC
    This is the best I could come up with. I can't find anyway except for looping through the messages.

    Also make sure to import the in method, because it isn't imported by default.
    #! /usr/bin/perl use strict; use warnings; use Win32::OLE qw( in ); #Outlook Application creation my $outlook = Win32::OLE->CreateObject('Outlook.Application'); my $ns = $outlook->getNamespace('MAPI'); my $inbox = $ns->GetDefaultFolder(6); #String to match my $match_str = 'test'; #Search the items foreach my $item (in $inbox->Items){ if($item->{Subject} =~ /^$match_str/i){ print "$item->{Subject} matches\n"; } }

      Yes this is fine, but I don't like much looping through all items :-(. "Find" method seems fairly optimize on this perspective. It suports searching portions of string in VB scripts. That's why I thought that there should be a way to activate it in Perl as well

      Thanks a lot for your attention. Do you or any others... further comments please?? - SB

Re: Filter(?) for Win32 OLE Find method
by cacharbe (Curate) on Aug 06, 2003 at 17:50 UTC

    This isn't a true DASL filter, so you end up having to use ranges, in a sense. Unfortunately, you can't use Contains, DoesNotContain, etc. from the filter object reference, as you would with the Search objects. It's a little different.

    The following will help you find a range of subjects all between 'test' and 'tz':

    use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Outlook'; $|++; $Win32::OLE::Warn = 3; my $OL = Win32::OLE->GetActiveObject('Outlook.Application') || Win32::OLE->new('Outlook.Application', 'Quit'); my $NameSpace = $OL->GetNameSpace("MAPI"); my $Folder = $NameSpace->GetDefaultFolder(olFolderInbox); my $Items = $Folder->{Items}; my $findtext = '[Subject] > "test" and [Subject] < "tz"'; my $MailItems = $Items->Find($findtext); while (1) { print $MailItems->{Subject} ."\n"; $MailItems = $Items->FindNext() || last; }
    And one should note the following (from the Outlook VB Reference):
    Comparison operators allowed within the filter expression include >,<, +<=, >=,= and <>. Logical operators allowed are And Not and Or
    Update: Slight modification to search string and Text from Find Object Reference.


    Flex the Geek

      Thank you very much for the clarification. It's really helpful - SB

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://281408]
Approved by phydeauxarff
Front-paged by tcf22
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2017-01-23 05:47 GMT
Find Nodes?
    Voting Booth?
    Do you watch meteor showers?

    Results (190 votes). Check out past polls.