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

Dear Monks, Here is an interesting one for you. I'm sure I am missing something obvious. I am working with a cgi form document. One of the form questions the client fills out, allows multiple selections. For instance,
Test1,
Test2,
Test3,
Test4,
The CGI program then takes those multiple selections, uses the split function @rename_file = split(/,/, $in{'clients'}); splits the data on the comma and then stores each selection into the array rename_file. From there, the program runs a foreach loop, opens up a Microsoft Word file called CENTRAL NJ.doc, replaces text in that word file with the name of the choice the client selected, saves the file according to the file name the client chose, closes the document and then repeats this whole process until all of the clients selections are used.
See code below:
foreach $file (@rename_file)
{
my $word = Win32::OLE->new('Word.Application', 'Quit');
$word->Documents->Open("S:/Documents/CENTRAL NJ.doc") || die("Unable to open document ", Win32::OLE->LastError());
$word->Selection->Find->{'Text'}='CENTRAL NJ';
$word->Selection->Find->Replacement->{'Text'}="$file";
$word->Selection->Find->Execute({Replace=>wdReplaceAll});
$word->ActiveDocument->SaveAs({FileName=>"S:/Documents/$file.doc"});
$word->ActiveDocument->Close;
$word->Quit;
}
If the client just selects one choice, the program works fine. The problem is if a client selects more than one option on the cgi form, the program fails to work. For example, if the client selects Test1 and Test2, the program works fine with Test1 (opens up CENTRAL NJ.doc, replaces the text inside the document "CENTRAL NJ" with "Test1", saves the file as Test1, closes the document) but then when it gets to Test2, the original word file "CENTRAL NJ.doc" loses the text inside the document "CENTRAL NJ" and then the program stops working. Any answers as to why this happens?

Replies are listed 'Best First'.
Re: Question On Forms and MS Word
by shmem (Chancellor) on Sep 25, 2007 at 21:19 UTC
    The CGI program then takes those multiple selections, uses the split function @rename_file = split(/,/, $in{'clients'}); splits the data on the comma and then stores each selection into the array rename_file.

    Are you sure this is what is happening? prove it with some code. You might have an anonymous array as value to that hash key, but then I don't know how you get $in{'clients'} in the first place.

    Any answers as to why this happens?

    It could be a parameter processing error as outlined above; it could be something else. Without code it's hard to tell.

    I have no experience with MS Word encantations via perl, but it strikes me that you create a full application for each replacement. Why don't you just chain your replacements using a single Word instance?

    my $word = Win32::OLE->new('Word.Application', 'Quit'); $word->Documents->Open("S:/Documents/CENTRAL NJ.doc") || die("Unable t +o open document ", Win32::OLE->LastError()); my $searchword = 'CENTRAL NJ'; foreach $file (@rename_file) { $word->Selection->Find->{'Text'} = $searchword; $word->Selection->Find->Replacement->{'Text'} = "$file"; $word->Selection->Find->Execute({Replace=>wdReplaceAll}); $word->ActiveDocument->SaveAs({FileName=>"S:/Documents/$file.doc"} +); $searchword = $file; # next pass, search this text. } $word->ActiveDocument->Close; $word->Quit;

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Question On Forms and MS Word
by mr_mischief (Monsignor) on Sep 25, 2007 at 22:41 UTC
    Please consider using CGI.pm (or CGI::Simple) for your CGI processing needs. For one reason, cgi-lib.pl, which you seem to be using or emulating, has been obsolete and deprecated for some time.

    There should be no reason to need help with parsing form entries from the POST or GET results manually. If you're rolling your own solution for anything more than fun or education, you should already know how to do it as well as the common, documented, and well-supported module. If you don't know how to do it as well as CGI.pm does it, then please use CGI.pm or CGI::Simple and we as a community can focus on supporting that.

      Thanks for your help in regards to using CGI.pm The Perl Module use CGI qw(:cgi-lib); was the problem.
        Okay, so you're using CGI.pm but in ts compatibility mode with cgi-lib? I'd suggest switching over to the native CGI.pm way of doing things. This:
        use CGI; my $cgi = CGI->new; my @array_of_options = $cgi->param( 'options' );
        is just dead simple compared to what you're trying to do.