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

I would like to perform a mail merge with Word 2007 and a tab delimited text file using Perl. I have tried the code below but all it does is create a copy of the Word template without any merge taking place. I think I'm missing something somewhere as I am not well versed in Perl. When I do the merge manually it works fine. Can someone help me out?

use strict; use warnings; use Win32::OLE ; use Win32::OLE::Const 'Microsoft Word'; my $sourcefile='C:\Users\Carl\Documents\AddressMergeTXT.txt'; my $mergefile='C:\Users\Carl\Documents\Merge.docx'; my $template = 'C:\Users\Carl\Documents\MergeLetter.docx'; my $Word= Win32::OLE->new('Word.Application', 'Quit'); $Word->Documents->Open($template); $Word->ActiveDocument->MailMerge->OpenDataSource($sourcefile); $Word->ActiveDocument->SaveAs($mergefile);
Thanks in advance for any and all help, Carl

Replies are listed 'Best First'.
Re: Mail Merge with Word 2007 and Perl
by roboticus (Chancellor) on Aug 17, 2014 at 00:10 UTC

    cmiller2005:

    I've never tried doing a mail merge, but checking example code from microsoft seems to imply that it's a little more complex that what you've tried. It seems that you need to specify what the fields are that need to be merged, and how to interpret the data source to get the fields. Later in the linked code, it seems that you need to actually "execute" the mailmerge after configuring everything.

    With the code you've shown, I'd expect to get exactly the results you say you're getting: another saved copy of the template.

    I'd suggest starting with the VBScript in the linked document, and translating it to perl, then tweaking it to get the results you want. Or (as is often suggested here), using the macro recorder to turn a manual mail-merge session into a VBScript macro, and then convert that to perl.

    ...roboticus

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

Re: Mail Merge with Word 2007 and Perl
by cmiller2005 (Initiate) on Aug 19, 2014 at 15:35 UTC

    I finally got the program to work. This is what I came up with

    use strict; use warnings; use Win32::OLE ; use Win32::OLE::Const 'Microsoft Word'; my $sourcefile='C:\Users\Carl\Documents\AddressMergeTXT.txt'; my $mergefile='C:\Users\Carl\Documents\Merge.docx'; my $template = 'C:\Users\Carl\Documents\MergeLetter.docx'; my $Word= Win32::OLE->new('Word.Application', 'Quit'); $Word->Documents->Open($template); $Word->ActiveDocument->MailMerge->OpenDataSource($sourcefile); $Word->ActiveDocument->MailMerge->{Destination} = wdSendToNewDocument; $Word->ActiveDocument->MailMerge->Execute; $Word->ActiveDocument->SaveAs($mergefile); $Word->Documents->Close(wdDoNotSaveChanges); undef $Word;

    Now, can someone show me how to convert the above merged docx file into an eps file using Postscript::Simple? I would like to do it all in the same program.

    Thanks again for any suggestions

    Carl

      cmiller2005:

      The file is still in word format, so I can't imagine a way to use Postscript::Simple to create an EPS file from the mailmerge. I'd suggest instead telling Word to print to a file using a postscript filter (I think that's included by default in later versions of windows). Then you could use the resulting PDF or extract the postscript and further manipulate it.

      ...roboticus

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

        Hi roboticus,

        Is it possible to merge a text file with a .eps file using Postscript::Simple? If so, could you give me some pointers. I appreciate all of your previous responses.

        Carl