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

Evening all, I have been quietly working away on my application for a few days now, see Win32:OLE and Word Mail Merge. I wrote the code for a particular part of my script in a seperate file, made sure it was working and then copied and pasted it when I was ready for it a bit later. (This is just the way I like to work). Anyway, I can't get it to work in my main program but it works fine by itself. The error I receive is "Can't call method 'MailMerge' on an undefined value at contacts.pl line 2278" Here is the code...
my $template = "templates\\standard letter.doc"; my $datasource = "c:\\windows\\desktop\\test.csv"; my $ret = "c:\\windows\\desktop\\test.doc"; use strict; use Win32::OLE ; use Win32::OLE::Const 'Microsoft Word'; my $Word= Win32::OLE->new('Word.Application', 'Quit'); $Word->Documents->Open($template); #line 2278 $Word->ActiveDocument->MailMerge->OpenDataSource($datasource); $Word->ActiveDocument->SaveAs($ret);
If I take out Line 2278 I receive the same error for the next line. Must be somthing to do with ActiveDocument I guess. By the way - the two scripts are in the same directory and are working in the same directory. Thus I am stumped. Is there any obvious reason as to why this should work in one place but not in the other. In both cases it is in a sub routine all by itself. Any ideas would be greatly appreciated. Cheers, Gerard.

Replies are listed 'Best First'.
Re: Another Win32::OLE question
by cacharbe (Curate) on Oct 20, 2001 at 20:24 UTC
    What does the code around it look like when it is in the fail environment?

    Is the word object declared in the same scope? Did you change working directories before trying to open the $template such that you are having a failure opening that file?

    Set $Win32::OLE::Warn = 3; # Fail on errors in this conext:

    use strict; use Win32::OLE ; use Win32::OLE::Const 'Microsoft Word'; $Win32::OLE::Warn = 3; # Fail on errors my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit');

    and then print Win32::OLE->LastError(); before you exit to get some error checking, in case you didn't have a fatal error.

    C-.

      I am positive that I had not changed the directory at all. I checked this before posting my question. I could not think of anything, but I read through my whole script anyway and could not find anywhere. After reading your reply I decided to change to the directory that it should be just in case. It worked! Which is good, but kind of annoying that I have wasted my own and everyones time. Now I just need to see why it is changing directories without my telling it to... Cheers all for the advice.
        I've found while working with ::OLE it's best to explicitly detail your file paths and file names, especially if this is a remote application on a webserver. The Webservers working directory ISN'T necessarily the directory of the application, believe it or not.

        Be that as it may, I'm glad I could help.

        C-.

Re: Another Win32::OLE question
by traveler (Parson) on Oct 20, 2001 at 20:08 UTC
    I have not used OLE with Word, just with PPT. My suggestion is to add some error checking around the open to see if indeed it is succeeding. Also, two additional comments: 1) you can replace the double backslashes with a single forward slash, 2) you can use Data::Dumper to look at the data structures, but set the depth to one-- at least in PPT each item has a link to its parent so any depth other than 1 will cause Data::Dumper to consume all available memory...

    HTH, --traveler