Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Inserting text into a Word document.....

Status
Not open for further replies.

goBoating

Programmer
Feb 8, 2000
1,606
US
I'm trying to create a M$ Word doc and populate it with input from a web/cgi/interface.&nbsp;&nbsp;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.&nbsp;&nbsp;The snippet below creates a vanilla Word doc with four lines (paragraphs). Anyone have any ideas? <br><br><FONT FACE=monospace><br>#!perl -w<br>use strict;<br>use Win32::OLE qw(in with);<br>my $file&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 'c:\perl\code\wordOut.doc';<br>my($word) = Win32::OLE-&gt;new('Word.Application', 'Quit')<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;¦¦ die &quot;Couldn't run Word&quot;;<br>my($doc) = $word-&gt;Documents-&gt;Add();<br>my ($par1) = $doc-&gt;Paragraphs-&gt;Add();<br><font color=red># I would like to add text to par1 here</font><br>my ($par2) = $doc-&gt;Paragraphs-&gt;Add();<br>my ($par3) = $doc-&gt;Paragraphs-&gt;Add();<br>my ($par4) = $doc-&gt;Paragraphs-&gt;Add();<br><br>$doc-&gt;SaveAs(&quot;$file&quot;);<br></font> <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
' Found an example at <A HREF=" TARGET="_new"> modified, from the example on that page.....the following opens a new Word doc, inserts lines from @lines, does a 'Save As', and quits.<br><br><FONT FACE=monospace><br>#!perl -w<br>use strict;<br>use Win32::OLE;<br>use Win32::OLE::Const 'Microsoft Word';<br><br>my(@line) = ('Here is the first line of text.',<br>&nbsp;&nbsp;&nbsp; 'Here is the second line of text.');<br>my($outputFile) = 'c:\perl\code\output\wordOut.doc';<br>unlink($outputFile);<br><br>my($word) = Win32::OLE -&gt; new('Word.Application', 'Quit');<br><br>my($doc) = $word -&gt; Documents -&gt; Add(); # Create a new document<br>my($range) = $doc -&gt; {Content};<br><br>$range -&gt; {Text} = $line[0];<br>my($i);<br>for ($i=1; $i &lt;= $#line; $i++) <br> {<br>&nbsp;&nbsp;&nbsp; $range -&gt; InsertParagraphAfter();<br>&nbsp;&nbsp;&nbsp; $range -&gt; InsertAfter($line[$i]);<br> }<br><br>my($paragraphCount) = $doc -&gt; Paragraphs -&gt; Count();<br>for ($i=1; $i &lt;= $paragraphCount; $i++) <br> {<br>&nbsp;&nbsp;&nbsp; print &quot;$i: &quot;, $doc -&gt; Paragraphs($i) -&gt; Range -&gt; {Text}, &quot;\n&quot;;<br> }<br><br>$doc -&gt; SaveAs($outputFile);<br>$doc -&gt; Close();<br>$word -&gt; Quit();<br># Success.<br>print &quot;Success \n&quot;;<br>exit(0);<br></font> <p> <br><a href=mailto: > </a><br><a href= > </a><br> keep the rudder amid ship and beware the odd typo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top