wakatana has asked for the wisdom of the Perl Monks concerning the following question:
Seems clear, just few questions to enshure that I understand:my $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application','Quit') or die Win32::OLE->LastError();
but then also found some weird structures, such as:my $doc = $word->Documents->Open('C:\\Perl\\home\\001f.doc');
First approach seems pretty clear, accessing method Open which is part of Document (class, package or whatever) but what does other do ?my $doc = $word->Documents->Open( { FileName => 'C:\\Perl\\home\\001f. +doc', ReadOnly =>1 }) or die Win32::OLE->LastError(); SaveAs({FileName => 'exampletext.doc', FileFormat => wdFormatDocument +,}) $doc->Close( { SaveChanges => $wdc->{wdDoNotSaveChanges} } );
As you can see both contains also variables (or properties or what is the correct name, please correct) for 'Characters' and 'Words' but seems that both 'Characters' and 'Words' are member of 'Word.Selection' how should I understand that? Also I tried to search for 'pagenumbers' as it was mentioned in above link but did not find anything except several 'wdPageNumberStyle' and 'PageNumbers' but not 'pagenumbers' (lowercase). Also I did not find in Object Browser that 'Word.Selection.Words' or 'Word.Selection.Characters' have 'Count' method (or property what is correct name) where this method (property) came from ? What does word 'as' means in above output it is some data type ? Here I am posting mentioned code which I slightly alteredClass Selection Member of Word ------------------------------------ Property Words As Words read-only Member of Word.Selection Property Characters As Characters read-only Member of Word.Selection ========================== Sub ShrinkDiscontiguousSelection() Member of Word.Selection ------------------------------------ Property Words As Words read-only Member of Word.Selection Property Characters As Characters read-only Member of Word.Selection
but this code did not works perfectly. It always returns word count 1 no matter how many word are in document. Those investigations points me to another probably most important question, how are all those OLE objects organized ? The object browser is unclear to me, I also downloaded OLE/COM Object Viewer but bad luck also. I know this is not standard question to perl but I dont know where to ask. One idea which commes to mind is to list somehow all methods (properities variables packages) which are included in OLE throught perl, and then just try several of them according name, is this possible ?#!/usr/bin/perl use Cwd 'abs_path'; use warnings; use strict; use Win32::OLE 'CP_UTF8'; $Win32::OLE::CP = CP_UTF8; binmode STDOUT, 'encoding(utf8)'; print abs_path($0) . "\n"; print "=========\n"; my $document_name = 'C:\\Perl\\home\\thisIsPerl.doc'; my $word = Win32::OLE->GetActiveObject('Word.Application') || Win32::OLE->new('Word.Application') or die Win32::OLE->LastError(); $word-> {visible} = 0; $word->Application->Selection; my $document = $word->Documents->Open( { FileName => $document_name, R +eadOnly =>1 }) or die Win32::OLE->LastError(); my $paragraphs = $document->Paragraphs (); my $n_paragraphs = $paragraphs->Count (); print "Words:", $word->Selection->Words->{Count}, "\n"; print "Characters:", $word->Selection->Characters->{Count}, "\n"; print "Paragraphs: ", $word->Selection->Paragraphs->{Count}, "\n"; $document->Close(); $word->exit; $word->Quit; Administrator@cepido /cygdrive/c/Perl/home $ ./internet04_pgcnt.pl /cygdrive/c/Perl/home/internet04_pgcnt.pl ========= Words:1 Characters:1 Paragraphs: 1
Works but throws some error at the end and did not proceed headers and footers#!/usr/bin/perl -w use strict; use warnings; use Win32::OLE::Const 'Microsoft Word'; my $file = 'C:\\Perl\\home\\thisIsPerl.doc'; my $Word = Win32::OLE->new('Word.Application', 'Quit'); $Word->{'Visible'} = 0; my $doc = $Word->Documents->Open($file); my $paragraphs = $doc->Paragraphs() ; my $n_paragraphs = $paragraphs->Count (); for my $p (1..$n_paragraphs) { my $paragraph = $paragraphs->Item ($p); my $words = Win32::OLE::Enum->new( $paragraph->{Range}->{Words} ); while ( defined ( my $word = $words->Next() ) ) { my $font = $word->{Font}; print "IN_Text:", $word->{Text}, "\n" if $word->{Text} !~ /\r/ +; #print $text; #$font->{Bold} = 1 if $word->{Text} =~ /Perl/; } print "=============\n"; } $Word->ActiveDocument->Close ; $Word->exit; $Word->Quit;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: win32 OLE in deeper details
by ig (Vicar) on Aug 03, 2012 at 05:57 UTC | |
by bulk88 (Priest) on Aug 03, 2012 at 06:41 UTC | |
|
Re: win32 OLE in deeper details
by rohit_raghu (Acolyte) on Aug 03, 2012 at 06:23 UTC | |
by Anonymous Monk on Aug 03, 2012 at 08:32 UTC | |
|
Re: win32 OLE in deeper details
by Anonymous Monk on Aug 03, 2012 at 08:34 UTC |