As I said earlier in the thread, I don't really know Word. However, there are some issues I know about in Excel that may transfer to Word. When opening a second instance of Excel manually, it usually asks me about read only copies of files like personal.xls, which contains macros I want always available. However, it doesn't do this when I open it from Perl. You may find setting the DisplayAlerts (if there is an analogue in Word) property to False may help, or it may just be the way I have my computer configured. If so, it's a function of something I don't know about.
The code below works for me, without caring about the Word file being open. It also closes Word when the Perl exits, but not the new instance of Excel. Clearly, you can't save the Excel file if a file of that name is currently open. Also, there's some strange trailing character that gets copied across. I haven't tried chomping it - that's not in the question in the exam paper I read :-). use strict;
use warnings;
use diagnostics;
use Win32::OLE;
my $xl = Win32::OLE->new('Excel.Application');
$xl->{Visible} = 1;
my $wb = $xl->Workbooks->Add;
my $word = Win32::OLE->new('Word.Application');
my $doc = $word->Documents->Open({FileName => "C:\\Perl\\eg\\842813\\W
+ord.doc", ReadOnly => 1});
my $row=0;
my $col=1;
for (1..$doc->Paragraphs->Count) {
$row++;
$wb->Sheets(1)->Cells($row, $col)->{Value} = $doc->Paragraphs($_)-
+>Range->Text;
}
Regards,
John Davies
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|