KathyM has asked for the wisdom of the Perl Monks concerning the following question:

Hi, there. I have written a programme in Perl that is to extract the default file paths from Microsoft Word and Microsoft Excel, then write them to a file. However, the programme only works on the PC I created it on and I'm struggling to understand why. I realise there must be something different about the PC I created it on, but I don't know what. The PCs, while different models, are all running the same OS (Windows XP SP2), same version of Office (2003) and same version of Perl (ActiveState Perl 5.8.6) with all the same libraries installed.

On a PC other than the one I created it on, the programme starts, then exits with the message:

"Use of uninitialised value in print at line 54, <STDIN> line 1.

"Can't call method "Options" on unblessed reference at line 60, <STDIN> line 1."

Here is my code. The subroutine for Excel works on any machine, it's only the Word subroutine that doesn't, so I have omitted the code for the Excel routine in order to save space.

Any suggestions anyone has will be most appreciated.



#!/usr/bin/perl -w # Test file for Excel and Word # c:\perl\programs\ use strict; use Win32::OLE; use Win32::OLE::Const; $Win32::OLE::Warn = 3; # Main # Open file to pipe test results to open (FH, '>>C:\\Perl\\Programs\\test1.txt'); print "Please enter the name of the image being tested:\n"; my $image = <STDIN>; print FH "Test results for ", $image, " image.\n\n"; # Call subroutines for testing each application test_word(); test_excel(); # Subroutine to test Word sub test_word { use Win32::OLE; use Win32::OLE::Const; use constant wdDocumentsPath => 0; use constant wdUserTemplatesPath => 2; # Check Word exists on image, then check default file locations s +et to H:\xxx if (-e "C:\\Program Files\\Microsoft Office\\Office11\\Winword.ex +e") { my $Word = Win32::OLE::Const->Load("Microsoft Word"); $Word = Win32::OLE->GetActiveObject("Word.Application"); $Word->{visible} = 1; print FH "Microsoft Word testing:\n\n"; print FH "Microsoft Word opened successfully.\n\n"; my $printer = $Word->{ActivePrinter}; print FH "The default printer is set to ", $printer, ".\n\n"; # Check default documents location set to H:\My Documents\WWD my $document_location = $Word->Options->DefaultFilePath(wdDocume +ntsPath); if ($document_location =~ /H:\\My Documents\\WWD/i) { print FH "Default documents location is correctly set to ", +$document_location, ".\n\n"; } else { print FH "Default documents location is incorrectly set to " +, $document_location, ".\n"; print FH "It should be set to H:\\My Documents\\WWD.\n\n"; } # Check default templates location set to H:\MSOffice\Templates my $templates_location = $Word->Options->DefaultFilePath(wdUserT +emplatesPath); if ($templates_location =~ /H:\\MSOffice\\Templates/i) { print FH "Default user templates location is correctly set t +o ", $templates_location, ".\n\n"; } else { print FH "Default user templates location is incorrectly set + to ", $templates_location, ".\n"; print FH "It should be set to H:\\MSOffice\\Templates.\n\n"; } sleep 2; # Close Word $Word->Quit(); } else { print FH "Unable to open Word: check that Word has been install +ed correctly.\n\n"; } }

Replies are listed 'Best First'.
Re: Win32::OLE and Word
by GrandFather (Saint) on Sep 27, 2005 at 00:28 UTC

    Use $Word = Win32::OLE->new ("Word.Application"); rather than $Word = Win32::OLE->GetActiveObject("Word.Application");. I'd guess you had Word running on your test system, but not on the others.

    Update: It's not obvious to me why you my $Word = Win32::OLE::Const->Load("Microsoft Word"); as you don't use the constants. In fact you immediately clobber the variable in the next line!

    Perl is Huffman encoded by design.

      Best of both worlds:
      Adapted from Win32::ole and MSWord:

      # If Word is already running, then use the existing instance, # and leave it running when we are done. # Otherwise, start a new instance of Word, and close it when # we are done. use strict; use warnings; use Win32::OLE; my $Word; eval {$Word = Win32::OLE->GetActiveObject('Word.Application')}; die "Word not installed: $@" if $@; unless (defined $Word) { $Word = Win32::OLE->new('Word.Application', sub {$_[0]->Quit}) or die "Oops, cannot start Word"; } $Word->{'Visible'} = 1; # If you want to see what's going on

Re: Win32::OLE and Word
by runrig (Abbot) on Sep 27, 2005 at 00:28 UTC
    The error seems to indicate that the $Word object was never created, and maybe LastError() from Win32::OLE would tell you what is wrong there. Looking at the Win32::OLE::Const docs, your code doesn't look right, it should probably be:
    use Win32::OLE; use Win32::OLE::Const; my $Word = Win32::OLE->new('Word.Application'); my $wd = Win32::OLE::Const->Load($Word);
    But you never use any of the constants, it looks like you are creating them manually with "use constant ..." instead (and I don't know if those are the correct constants or if they differ on different systems), but maybe you should use the $wd hashref to get at the constants instead.
      Thanks, everyone, for your suggestions. It is now working on my other PCs as well! (Using Win32::OLE->new instead of Const->Load.

      Thanks, again, much appreciated.


Re: Win32::OLE and Word
by GrandFather (Saint) on Sep 26, 2005 at 23:25 UTC

    Have you all the required modules installed on the other machines? In particular check that Win32::OLE and Win32::OLE::Const are available.

    Perl is Huffman encoded by design.
      Thanks for your prompt response.

      I have and installed to c:\perl\site\lib\win32 and c:\perl\site\lib\win32\ole respectively on all the PCs.

      Is that what you were meaning? (Sorry, I'm new to Perl!)


        Ok, that's probably correct. You haven't provided the code for test_word or test_excel, but that seems likely to be where the problem is.

        Can you trim your code down to a smallest possible example that demonstrates the error and post that? For strong preference the test should be in a form that we monks can run and check on our systems.

        Update: actually read the code :)

        Perl is Huffman encoded by design.