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

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"; }

Replies are listed 'Best First'.
Re^2: "Type mismatch" Error While Using Win32:OLE for MS Outlook
by dushyant (Acolyte) on Jul 03, 2015 at 11:30 UTC

    Hi Marto,

    Your code is not working. its giving below error.

    D:\Perl_Project\OutLook>test2.pl No type library matching "Microsoft Outlook" found at D:\Perl_Project\ +OutLook\test2.pl line 6. Win32::OLE(0.1712): GetOleTypeLibObject() Not a Win32::OLE::TypeLib ob +ject at D:/Straberry/perl/vendor/lib/Win32/OLE/Const.pm line 49. Bareword "olFolderInbox" not allowed while "strict subs" in use at D:\ +Perl_Project\OutLook\test2.pl line 10. Execution of D:\Perl_Project\OutLook\test2.pl 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?

        Yes OutLook is installed, its "Microsoft Office Professional Plus 2013". Please check output of below code it returned outlook object.

        #! D:\Straberry\perl\bin\perl -w use strict; use Win32::OLE qw/in/; use Win32::OLE::Const 'Microsoft Outlook'; #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("olFolder"); #print ref($Folder->{Items}) . "\n"; #foreach my $subfolder ( in $Folder->Folders) { # printf "%s\n", $subfolder->{Name}; #} print "OLE object's properties:\n"; foreach my $Key (sort keys %$OL) { my $Value; eval {$Value = $OL->{$Key} }; $Value = "***Exception***" if $@; $Value = "<undef>" unless defined $Value; $Value = '['.Win32::OLE->QueryObjectType($Value).']' if UNIVERSAL::isa($Value,'Win32::OLE'); $Value = '('.join(',',@$Value).')' if ref $Value eq 'ARRAY'; printf "%s %s %s\n", $Key, '.' x (40-length($Key)), $Value; }

        Output of Above code:

        D:\Perl_Project\OutLook>test1.pl
        No type library matching "Microsoft Outlook" found at D:\Perl_Project\OutLook\test1.pl line 4.
        Win32::OLE(0.1712): GetOleTypeLibObject() Not a Win32::OLE::TypeLib object at D:/Straberry/perl/vendor/lib/Win32/OLE/Const.pm line 49.
        Win32::OLE(0.1712) error 0x80020005: "Type mismatch" in METHOD/PROPERTYGET "GetDefaultFolder" argument 1 at D:\Perl_Project\OutLook\test1.pl line 9.
        OLE object's properties:
        Application ............................. _Application
        Assistance .............................. IAssistance
        COMAddIns ............................... COMAddIns
        Class ................................... 0
        DefaultProfileName ...................... Outlook
        Explorers ............................... _Explorers
        Inspectors .............................. _Inspectors
        IsTrusted ............................... 0
        LanguageSettings ........................ LanguageSettings
        Name .................................... Outlook
        Parent .................................. <undef>
        PickerDialog ............................ PickerDialog
        ProductCode ............................. {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
        Reminders ............................... _Reminders
        Session ................................. _NameSpace
        TimeZones ............................... _TimeZones
        Version ................................. 15.0.0.4569
        D:\Perl_Project\OutLook>

      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?