Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Inserting text into a Word document.....

Inserting text into a Word document.....

Inserting text into a Word document.....

I'm trying to create a M$ Word doc and populate it with input from a web/cgi/interface.  I can open Word, create paragraphs, and save the file to the desired name, but I can't figure out how to insert text into the paragraph objects.  The snippet below creates a vanilla Word doc with four lines (paragraphs). Anyone have any ideas?

#!perl -w
use strict;
use Win32::OLE qw(in with);
my $file       = 'c:\perl\code\wordOut.doc';
my($word) = Win32::OLE->new('Word.Application', 'Quit')
        ¦¦ die "Couldn't run Word";
my($doc) = $word->Documents->Add();
my ($par1) = $doc->Paragraphs->Add();
# I would like to add text to par1 here
my ($par2) = $doc->Paragraphs->Add();
my ($par3) = $doc->Paragraphs->Add();
my ($par4) = $doc->Paragraphs->Add();


keep the rudder amid ship and beware the odd typo

RE: Inserting text into a Word document.....

' Found an example at http://savage.net.au/Perl-tutorials.html. ; Slightly modified, from the example on that page.....the following opens a new Word doc, inserts lines from @lines, does a 'Save As', and quits.

#!perl -w
use strict;
use Win32::OLE;
use Win32::OLE::Const 'Microsoft Word';

my(@line) = ('Here is the first line of text.',
    'Here is the second line of text.');
my($outputFile) = 'c:\perl\code\output\wordOut.doc';

my($word) = Win32::OLE -> new('Word.Application', 'Quit');

my($doc) = $word -> Documents -> Add(); # Create a new document
my($range) = $doc -> {Content};

$range -> {Text} = $line[0];
for ($i=1; $i <= $#line; $i++)
    $range -> InsertParagraphAfter();
    $range -> InsertAfter($line[$i]);

my($paragraphCount) = $doc -> Paragraphs -> Count();
for ($i=1; $i <= $paragraphCount; $i++)
    print "$i: ", $doc -> Paragraphs($i) -> Range -> {Text}, "\n";

$doc -> SaveAs($outputFile);
$doc -> Close();
$word -> Quit();
# Success.
print "Success \n";

keep the rudder amid ship and beware the odd typo

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close