http://www.perlmonks.org?node_id=1032035

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

Hello all, Here is what I seek: My Perl reads an input file with about 80 rows of data. For each row I need to create a new DOCX with some of the data from that input row. All MS-WORD files go the same folder. I spent a couple of days trying to figure this out but couldn't. Please, could any of you provide me with a simple example of a very simple Perl pgm that automatically creates 2 docx files, enters in them a constant and a variable from the input file, and saves them, without the user's intervention. I can't save the docx files. I get error: "Can't call method "SaveAs" on an undefined value at DOC.pl line 18, <INPUTFILE> line 1."

use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; my $path = 'E:/MASAV/DOC/'; my $input = 'input.txt'; my $in_file = $path . $input; my $counter = 4534; my $My_Word = Win32::OLE->new('Word.Application') or die "Problem with MS-WORD"; open (INPUTFILE, "<$in_file")|| die "Can not open $in_file\n"; while(<INPUTFILE>) { chomp $_; $counter++; my $docx_name = $counter.".docx"; $My_Word->{'Visible'} = 0; $My_Word->{DisplayAlerts} = 0; my $My_Save = $My_Word->ActiveDocument->SaveAs( {FileName => $docx_name, FileFormat => wdFormatDocument}); $My_Word->{ActiveDocument}->Close; } $My_Word->Quit;

Replies are listed 'Best First'.
Re: create .docx files (MS-WORD with WIN32::OLE)
by poj (Abbot) on May 04, 2013 at 13:32 UTC
    You need to Add a document ;
    use strict; use warnings; use Win32::OLE; use Win32::OLE::Const 'Microsoft Word'; my $My_Word = Win32::OLE->new('Word.Application') or die "Problem with MS-WORD"; my $counter = 4534; my $path = 'C:/temp/'; while(<DATA>) { chomp $_; $counter++; my $docx_name = $path.$counter.".docx"; $My_Word->Documents->Add || die("Unable to create document ", Win32: +:OLE->LastError()); $My_Word->{'Visible'} = 0; $My_Word->{DisplayAlerts} = 0; $My_Word->Selection->TypeText ({ Text => $_ }); $My_Word->ActiveDocument->SaveAs( $docx_name ); $My_Word->{ActiveDocument}->Close; print "Created $docx_name\n"; } $My_Word->Quit; __DATA__ First Document Second Document Third Document
    poj

      $My_Word->Documents->Add returns the document. In my experience it's much better to work with that returned document directly instead of resorting to ->ActiveDocument.

Re: create .docx files (MS-WORD with WIN32::OLE)
by rpnoble419 (Pilgrim) on May 05, 2013 at 04:31 UTC

    Have you looked at creating open document format file instead. You use the OpenOffice-OODoc module. This will give you a solution that works with MS Word and allows you to port your solution to equipment where Office is not installed.

Re: create .docx files (MS-WORD with WIN32::OLE)
by gadi99 (Initiate) on May 04, 2013 at 18:11 UTC
    To get on well in life, all you need to know is who to turn to, and when it comes to Perl, you Monks are the ones. THANK YOU very much, poj! Your response solved my problem. Gadi
Re: create .docx files (MS-WORD with WIN32::OLE)
by gadi99 (Initiate) on May 05, 2013 at 07:21 UTC
    Thank you, rpnoble419. I work in a very small environment where Office is installed, so POJ's solution was very satisfactory. Thanks again to all you Monks. Gadi