Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

"Type mismatch" Error While Using Win32:OLE for MS Outlook

by dushyant (Acolyte)
on Jul 03, 2015 at 07:30 UTC ( #1133046=perlquestion: print w/replies, xml ) Need Help??
dushyant has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

I am tring to download email attachments from Microsoft Outlook using Win32::OLE. But I stuck at initial level I don't even able to get outlook folder list.
Please find below code I am using and Error I got.

I am using,
32 bit Straberry Perl ( perl 5, version 20, subversion 1 (v5.20.1) built for MSWin32-x86-multi-thread-64int),
OS (64 bit) = Windows 7 Enterprise Service Pack 1
Microsoft Outlook 2013

Error :

Win32::OLE(0.1712) error 0x80020005: "Type mismatch" in METHOD/PROPERTYGET "GetDefaultFolder" argument 1 at D:\Perl_Project\OutLook\ line 9.
Can't call method "Folders" on an undefined value at D:\Perl_Project\OutLook\ line 15.
D:\Perl_Project\OutLook>perl -v

#! D:\Straberry\perl\bin\perl -w use strict; use Win32::OLE qw/in/; #use Win32::OLE::Const 'Outlook.Application'; #use Win32::OLE::Variant; my $OL = Win32::OLE->GetActiveObject('Outlook.Application') || Win32:: +OLE->new('Outlook.Application') or die "$!\n";; my $NameSpace = $OL->GetNameSpace("MAPI"); my $Folder = $NameSpace->GetDefaultFolder("olFolderInbox"); #print ref($Folder->{Items}) . "\n"; foreach my $subfolder ( in $Folder->Folders) { printf "%s\n", $subfolder->{Name}; }


Replies are listed 'Best First'.
Re: "Type mismatch" Error While Using Win32:OLE for MS Outlook
by marto (Bishop) on Jul 03, 2015 at 09:28 UTC

    On this machine I don't have any sub folders within my inbox. Note the Win32::OLE::Const line and the difference in GetDefaultFolder(olFolderInbox). A short example:

    #!/usr/bin/perl use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Outlook'; my $Outlook = Win32::OLE->GetActiveObject('Outlook.Application'); my $NameSpace = $Outlook->GetNameSpace("MAPI"); my $Folder = $NameSpace->GetDefaultFolder(olFolderInbox); # print subject for each item for (my $i = 1; $i <= $Folder->Items->Count; $i++) { print $Folder->Items->Item($i)->Subject . "\n"; }

      Hi Marto,

      Your code is not working. its giving below error.

      D:\Perl_Project\OutLook> No type library matching "Microsoft Outlook" found at D:\Perl_Project\ +OutLook\ line 6. Win32::OLE(0.1712): GetOleTypeLibObject() Not a Win32::OLE::TypeLib ob +ject at D:/Straberry/perl/vendor/lib/Win32/OLE/ line 49. Bareword "olFolderInbox" not allowed while "strict subs" in use at D:\ +Perl_Project\OutLook\ line 10. Execution of D:\Perl_Project\OutLook\ aborted due to compilati +on errors. D:\Perl_Project\OutLook>
        No type library matching "Microsoft Outlook" found

        Is Outlook installed on your machine? If so, what version?

        Works for me. Windows 7 64bit, Strawberry perl 5.20, Win32::OLE v0.1712, Outlook 2007.

        Update: I suppose this could be specific to the version of Office in use.

        I have a similar problem in that the "use" can't find the MS Word object library:
        use Win32::OLE; use Win32::OLE::Const 'Microsoft.Word'; my $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new( 'Word.Application', 'Quit' ); ... if ( !$selection->GoTo( wdGoToBookmark, 0, 0, $bookmark ) ) { $LOGGER->warn("Could not find bookmark $bookmark"); return; }
        This worked previously. I now have MS Office Pro Plus 2013, and this no longer works but instead gives a similar "no type library" message. If I dump the "use" and instead grab the constants from the document itself, I can load them:
        use Win32::OLE; my $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new( 'Word.Application', 'Quit' ); my $wd_const = Win32::OLE::Const->Load($word);
        But then I have to wrap all of the constant with a more explicit call:
        if ( !$selection->GoTo( $wd_const->{wdGoToBookmark}, 0, 0, $bo +okmark ) ) { $LOGGER->warn("Could not find bookmark $bookmark"); return; }
        I'd prefer not to do that. Does anyone know how to get the use of Const to work?
Re: "Type mismatch" Error While Using Win32:OLE for MS Outlook
by VinsWorldcom (Parson) on Jul 03, 2015 at 12:47 UTC
Re: "Type mismatch" Error While Using Win32:OLE for MS Outlook
by dushyant (Acolyte) on Jul 03, 2015 at 13:22 UTC

    Thanks Corion, for RCA
    Numeric value for olFloder resolved the issue.

    my $Folder = $NameSpace->GetDefaultFolder(6);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1133046]
Approved by Corion
and all is calm...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (2)
As of 2018-03-24 08:30 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (297 votes). Check out past polls.