in reply to Re^6: Mail Merge with Word 2007 and Perl
in thread Mail Merge with Word 2007 and Perl

Hello Again,

After getting some clarification on what I'm supposed to do I found that I am supposed to use PostScript::Simple only to create the letters from the text file I used previously. This is what I came up with:

use strict; use warnings; use PostScript::Simple; $|=1; my $file = 'C:\Users\Carl\Documents\AddressMergeTXT.txt'; open (INPUT, $file) or die ("Can't open file"); my $line; my $text; my $p = 1; while (my $newline = <INPUT>) { chomp $newline; my ($firstname, $lastname, $address, $city, $state, $zipcode, $blank1, + $blank2, $blank3, $blank4, $blank5, $blank6, $blank7, $barcode) = sp +lit('\t', $newline); $line = new PostScript::Simple (papersize => "Letter"); $line -> setfont("Arial,Bold", 10); $line -> text (430, 750, "Cogswell Cogs"); $line -> text(430, 740, "2234 W. Dale Ave."); $line -> text (430, 730, "San Antonio, TX. 77450"); $line -> text (430, 720, "722 - 423 - 7260"); $line -> text (40,700, $firstname); $line -> text (80, 700, $lastname); $line -> text (40, 690, $address); $line -> text (40, 680, $city.","); $line -> text (115, 680, $state."."); $line -> text (135, 680, $zipcode); $line -> setfont("USPSIMBStandard", 10); $line -> text (40, 670, $barcode); $line -> setfont("Arial,Bold", 10); $line -> text (40, 620, "Dear ". $firstname . ","); $text = &getLetter; $line -> text (40, 610, $text); $line -> output('C:\Users\Carl\Documents\testdata\file' .$p .'.eps'); $p += 1; } sub getLetter { my $txline; my $letterfile = 'C:\Users\Carl\Documents\NewLetter.txt'; open (INPUT1, $letterfile) or die ("Can't open file"); while ($txline = <INPUT1>) { return $txline; } } close (INPUT); close (INPUT1);

It works fine, even though the formatting and style probably sucks, up until I try to read in a form letter using the sub getLetter. It only reads in one line and then stops, thus only the first line of the file gets inserted into the letter. Can someone show me what I did wrong and how I can get the whole file loaded into the letter? Also, almost forgot, can someone tell me how to get the loop to skip reading in the headers on the text file? In other words, so the first letter created will not have First Name, Last Name as the recipient?

As always, any help appreciated

Carl

Replies are listed 'Best First'.
Re^8: Mail Merge with Word 2007 and Perl
by roboticus (Chancellor) on Sep 18, 2014 at 23:56 UTC

    cmiller2005:

    In your getLetter routine, you're only telling it to read the first line. Let's reproduce it here with good indentation:

    sub getLetter { my $txline; my $letterfile = 'C:\Users\Carl\Documents\NewLetter.txt'; open (INPUT1, $letterfile) or die ("Can't open file"); while ($txline = <INPUT1>) { return $txline; } }

    With the indentation cleaned up, the problem becomes obvious: You read a line into $txline, and then immediately return to the caller. You never actually read the rest of the file.

    To fix it, you probably want to collect the entire file and return it as a single text string. You can do it like this:

    sub getLetter { # Let's start the buffer with an empty string to avoid # an "uninitialized" warning message. my $txline = ""; my $letterfile = 'C:\Users\Carl\Documents\NewLetter.txt'; open (INPUT1, $letterfile) or die ("Can't open file"); while (my $line = <INPUT1>) { # Each time we read $line, we want to add it to the # end of $txline $txline = $txline . $line; } # *now* we have all the data we want in $txline, so we # can return it to the caller return $txline; }

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

      Thanks so much roboticus

      I copied your code above but on the output it still only prints the first line to the document. I wonder if it may be something in the main program that is trying to print everything onto one line and its not showing everything in the .eps file?

      I put in a print $txline just before the return statement and the output is fine, its just not getting everything to the document or the document is just putting it all on one line and not showing all of it. I'm thinking it must be the latter since it all prints fine on the console.

      I think the problem is with the following lines in the main program:

      $text = &getLetter; $line -> text (40, 610, $text);

      Maybe I need a nested while loop that would update the y position of the text so that each line will print on its own line. What do you think?

      Thanks so much for your continued help and advice

      Carl

        cmiller2005:

        If you're printing it as a single text string, then you'll need to break it up and print it in chunks. If I recall correctly, one of the PDF modules has a function that'll take a text string and wrap it around appropriately to fill in a block. You may google for PDF text wrap, or nose around on http://cpan.org to find it.

        ...roboticus

        When your only tool is a hammer, all problems look like your thumb.