Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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
[marto]: in marto's scratchpad should I see a patching ... for each file it has successfully patched? I note that I don't get that line for the ones which claim to be read only
[marto]: perlbrew installing 5.10.1 to investigate this

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2018-03-22 09:56 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (273 votes). Check out past polls.