Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Win32::OLE module

by perl_seeker (Scribe)
on Aug 12, 2003 at 08:41 UTC ( [id://283114]=perlquestion: print w/replies, xml ) Need Help??

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

I have in hand some code which converts an MS-Word .doc
file to a text file.How do I change this code to do the reverse
i.e.convert from text to .doc format?

sub save_doc_as_text { my ( $infile, $outfile ) = @_; print "\n$infile"; print "\n$outfile"; require Win32::OLE; my $word = Win32::OLE->new( 'Word.Application', sub {$_[0]->Quit;});er +ror( "Can't create new instance or Word Reason:$Win32::OLE::LastError +" ) unless $word; $word->{visible} = 0; my $doc = $word->{Documents}->Open($infile); error( "Can't open $infile, Reason:$Win32::OLE::LastError" ) unless $d +oc; # wdFormatDocument wdFormatText wdFormatHTML $doc->SaveAs( { FileName => $outfile, FileFormat => $wdFormatText}); + $doc->Close; undef $doc; undef $word;} $inf="C:\\test\\input3.doc"; $outf="C:\\test\\text-output.txt"; save_doc_as_text($inf,$outf);
Please help.
Thanx

Replies are listed 'Best First'.
Re: Win32::OLE module
by cacharbe (Curate) on Aug 12, 2003 at 12:06 UTC

    To find the answer to your question, I would suggest a couple things. First, I wrote a tutorial about using Excel, but the first couple of paragraphs apply to any OLE development, as they describe resources, books and tools that you might need while trying to figure out how to do certain things in the OLE environment, so you might want to give it a scan.

    Second, your note suggests that you aren't really sure what the code you posted is doing, and that might be a really good first place to start, as through research and understanding come enlightenment.

    Your line $doc->SaveAs( { FileName => $outfile, FileFormat => $wdFormatText}); is the key. After you open the text file, you'll want to save as wdFormatDocument, though I would load the constants as well, using the following code:
    use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Word'; my $textfile = '<SOME FILE PATH>'; my $outfile= '<SOME FILE PATH>'; my $Word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application', 'Quit'); my $Doc = $Word->Documents->Open($textfile); $Doc->SaveAs({FileName => $outfile, FileFormat => wdFormatDocument} +);
    YMMV, some restrictions may apply.

    C-.

    ---
    Flex the Geek

      Hi,
      Way to go! Your code works just fine.Thanks a lot.
      I am going to read your tutorial.
      :)
Re: Win32::OLE module
by bm (Hermit) on Aug 12, 2003 at 09:03 UTC
      Hi
      I don't understand.The link you mentioned
      has discussions on converting from:
      .doc file TO text file
      What I need is to convert from
      text file TO .doc file
      Thanx.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://283114]
Approved by gellyfish
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (3)
As of 2024-04-25 09:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found