Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Win32::OLE module

by perl_seeker (Scribe)
on Aug 12, 2003 at 08:41 UTC ( #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.

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.


    Flex the Geek

      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
      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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://283114]
Approved by gellyfish
[choroba]: did you copy any dependencies?
[choroba]: all XS code needs to be recompiled
[vedagiri89]: install perl5.16 in the new server and try to run appache
[hippo]: Which version of CentOS?
[vedagiri89]: how to fix: Attempt to reload aborted
[hippo]: Ah, presumably 7 then.
[vedagiri89]: CentOS Linux release 7.0.1406 (Core)
[hippo]: How did you install libapreq2?

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2018-06-19 11:30 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (113 votes). Check out past polls.