in reply to convert text to word

Why bother, Word will open text files? Why turn an elegant compact file into a bloated proprietary M$ *.doc file. If you make it a Word 2000+ file you will have turned a file that can be read by anyone into a file that can only be read by people who have Word 2000 or later installed. Wo betide those of us who have not bought a copy of Word since Word 95 which was about the last time a useful feature was added IMHO.

You can probably do it with Win32::OLE if you really want to, but why on earth would you bother? Rather like taking a Perl and turning it into a swine......

Update

For the record here is how to add text to a new word document using Win32::OLE. I will leave it as an exercise for the reader as to how to read the text file in (see perlman:open)....

use Win32::OLE; # check if Word exists and is running my $word = Win32::OLE->GetActiveObject('Word.Application'); die "Word not Installed" if $@; # start Word program instance if required or die if unable to unless (defined $word) { $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit; } ) + or die 'Cannot start Word'; } # hide/show the document $word->{Visible} = 0; # Create new document my $d = $word->Documents->Add; # define selection my $s = $word->Selection; #set lines to be written to document my @lines = ( "This is a test line\n", "This is second test Line\n", "This is the third line\n", ); # add the lines of text $s->TypeText($_) for @lines; # save our object $word->WordBasic->FileSaveAs("c:\\test.doc"); # undef our object out of existance undef $word;

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: convert text to word
by Anonymous Monk on Feb 06, 2003 at 16:21 UTC
    When I run the perl code below, there is an error said that
    Can't locate loadable object for module Win32::OLE in @INC (@INC conta +ins: /usr/lib/perl5/5.8.0/i386-linux-thread-multi /usr/lib/perl5/5.8. +0 /usr/lib/perl5/site_perl/5.8.0/i386-linux-thread-multi /usr/lib/per +l5/site_perl/5.8.0 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_per +l/5.8.0/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.0 /usr +/lib/perl5/vendor_perl .) at word2text.pl line 2
    How can I solve that problem...if anybody know please answer......
    use Win32::OLE; # check if Word exists and is running my $word = Win32::OLE->GetActiveObject('Word.Application'); die "Word not Installed" if $@; # start Word program instance if required or die if unable to unless (defined $word) { $word = Win32::OLE->new('Word.Application', sub { $_[0]->Quit; } ) + or die 'Cannot start Word'; } # hide/show the document $word->{Visible} = 0; # Create new document my $d = $word->Documents->Add; # define selection my $s = $word->Selection; #set lines to be written to document my @lines = ( "This is a test line\n", "This is second test Line\n", "This is the third line\n", ); # add the lines of text $s->TypeText($_) for @lines; # save our object $word->WordBasic->FileSaveAs("c:\\test.doc"); # undef our object out of existance undef $word;

    janitored by ybiC: Balanced <code> tags around code and error message, as per Monastery convention

      Um... Sorry, but how do you expect to run Win32::OLE on /usr/lib/perl5/5.8.0/i386-linux-thread-multi?

      Does MS Word run on that platform?

      ps. Your more likely to get responses of you take the time to read the site FAQ and learn how to format your posts so they are readable.


      Examine what is said, not who speaks.

      The 7th Rule of perl club is -- pearl clubs are easily damaged. Use a diamond club instead.