win32 ole in deepr details in perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting win32 ole in deepr details in perl
# 1  
Old 08-02-2012
win32 ole in deepr details in perl

Hello Gurus,
I am begginer in perl. I would like to ask several questions, some related to perl and its syntax but most will be regarding to WIN32 OLE. My main goal is to develop script that will check word document structure (return some information) and make some changes in this document (if it is possible). I am not shure if all that can be done throught perl and OLE. First I am sorry for posting things regarding microsoft OLE but mostly things that I have found was from this site, I also tried MSDN but seems very unclear and useless to me. Latest experimienting with OLE drives me crazy so I hope somebody could help.


1st
===
How to properly start win32 OLE ? I found somewhere this approach:

Code:
my $word = Win32::OLE->GetActiveObject('Word.Application')
    || Win32::OLE->new('Word.Application','Quit')
    or die Win32::OLE->LastError();

Seems clear, just few questions to enshure that I understand:
1. Can i replace or with || both stands for logical or ?
2. What is really going on in this code:
The perl tries to capture some of running instances of word application, if fails then starts it own instance and if this fails the error is printed ? What would happend if it captures some existing instance, and how to create this instance, it is just started word process ?
3. I read in OLE documentation that second argument is destructor (but seems that is not mandatory), what is really its purpose ? i know it is opposite of constructor but, there needs to be some method created with name 'Quit' or it is done automatically or what is going on ?
4. What does those "::" and "->" in code above stands for ? Is it some accessing of methods in some package or class or whatever ?
5. 'use warnings;' is the same as perl -w ?

2nd
===
Some codes that I've found on internet regarding OLE cointinues with following:

Code:
my $doc = $word->Documents->Open('C:\\Perl\\home\\001f.doc');

but then also found some weird structures, such as:
Code:
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} } );

First approach seems pretty clear, accessing method Open which is part of Document (class, package or whatever) but what does other do ?
What does mean {FileName => 'C:\\Perl\\home\\001f.doc', ReadOnly =>1} in fcunction arguments, why curly braces ?
What is the difference between '=>' and '->' ? Is closing properties (or what is correct name) in '{}' necessary ?

I have found that VBA has nearly simillar syntax 'ComputeStatistics(Statistic:=wdStatisticWords)' I am assuming this thing relates each other because in fact with OLE I am using microsoft technologies from perl. Also found solution which works with same functions/parameters as VBA (but without this strange assigment) here it is: Comment on

3rd
===
On this site Regarding the "Comment" object in Perl's Win32::OLE is mentioned that M$ word does not have information about number of pages contained in documet. So everytime when OLE is used the document is opened and page count is recalculated according to styles font size etc. Seems according M$ this is no problem WD98: Sample VBA Macro to Count Number of Pages in Document
WD97: VBA Macro to Return (Count) Total Number of Pages, or the page count is also recalculated during file openning ? Also found two solutions for perl which are working using wdPropertyPages - winapi - Why are the number of pages in a Word document different in Perl and Word VBA? - Stack Overflow and using wdStatisticPages - Comment on So where is the truth is there infomration about page count or not.



4th
===
As I mentioned it seems that perl, VBA and PowerShell codes that I've found have several in common (I am not exper neither of those languages but they are acessing simillar variables). Following page describes how to obtain number of words and number of pages from document. As one of user suggested it can be obtained with '$selection->Words->{Count};' and '$selection->pagenumbers->{Count};' construction. However if I search word 'selection' in Object Browser of M$ Visual basic (from word document hit alt+F11 and F2) I found following:

Code:
Class 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


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 altered

Code:
#!/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, ReadOnly =>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




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 ?



5th
===
Is possible to process word document character by character ? Or even better is possible to query data from word like if it is SQL? Simply say select * from document where Font=Italic ?

I think that reading by words I have done here ()there are some little mistakes:

Code:
#!/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;


Works but throws some error at the end and did not proceed headers and footers






6th
===
I searched found following in Object Browser:
Const wdNumberOfPagesInDocument = 4
Member of Word.WdInformation

Const wdStatisticPages = 2
Member of Word.WdStatistic

What does mean thoe numbers ? I am shure they do not coresponds with actual number of word document pages (I was playing with code from which works Comment on)






7th
===
Finally last question, I read somewhere that full path is necessary in OLE to open word document. I would like to pass document to procesing as an argumen to script but without needing specify full path (whole path should be appended to it after it will be passed to script) found somewhere that 'abs_path($0)' is using to doing someting similar but I had no luck. Also on Windows the slashes must be escaped and so on.




I am sorry for longer post but I am stuck at points that I've described, hope somebody knows answer. Thanks a lot for any idea
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl syntax and html ole parsing

Hi gurus I am trying to understand some advanced (for me) perl constructions (syntax) following this tutorial I am trying to parse html: Using Mojo::DOM | Joel Berger say "div days:"; say $_->text for $dom->find('div.days')->each; say "\nspan hours:"; say $_->text for... (1 Reply)
Discussion started by: wakatana
1 Replies

2. Programming

Perl OLE Selection property iteration in cycle

Hi gurus, i am trying to write simple perl script using win32 ole which will iterate over all M$ word paragraphs (any text that ends with a hard return) and print only those paragraphs that matches the specified condition. The problem is that I need to access font size property. It seems to me that... (0 Replies)
Discussion started by: wakatana
0 Replies

3. Shell Programming and Scripting

Details about WIN32::OLE

Hi all, Is win32::OLE module is applicable in linux system??? from my understanding it is not possible..because we have to use some tools for that..for more info refer this website http://oclug.on.ca/archives/oclug/2001-July/008100.html (1 Reply)
Discussion started by: kavi.mogu
1 Replies

4. Shell Programming and Scripting

PERL Win32::OLE Inserting Picture in Excel

I am trying to insert a picture into a worksheet in Excel using Perl the following is the code use Win32::OLE; use Win32::OLE::Const "Microsoft Excel"; use Win32::OLE qw(in with); # Initiate Excel application $Excel = Win32::OLE->new('Excel.Application', 'Quit'); $Excel->{Visible} =1; #... (1 Reply)
Discussion started by: cold_Que
1 Replies

5. Shell Programming and Scripting

MS Outlook + Win32::OLE

Hey guys, I'm trying to go through my emails in Outlook until I find an email with a certain subject line. I am able to send emails with no problem, but reading emails and their properties (From, Subject, etc.) is my main problem. Basically, I don't know where to start and using search engines... (2 Replies)
Discussion started by: kooshi
2 Replies

6. Shell Programming and Scripting

OLE ERROR in perl

Hello All, I have executed one script where i am getting this error,what may be the reason..... please help me out. OLE exception from Microsoft Excel Win32::OLE(0.1403) error 0x800a03ec in METHOD/PROPERTYGET "open" (1 Reply)
Discussion started by: suvenduperl
1 Replies

7. Shell Programming and Scripting

Spell Check in MS Word using PERL OLE

Hi, I am trying automate couting number of spell and typo errors in MS Word document using perl script. In perl script, i am using Win32::OLE module of perl to read MS word document. Can anybody tell me are there any modules available in perl which can be imported into my script to... (0 Replies)
Discussion started by: 123an
0 Replies

8. Windows & DOS: Issues & Discussions

Perl in Win32

Dear, Did anybody knnow how to run the Perl script or whatever mean for testing the script in Windows? (1 Reply)
Discussion started by: Paris Heng
1 Replies

9. Shell Programming and Scripting

Win32::OLE open excel file as read only

I am using Win32::OLE to write a perl script which opens an excel file. That excel file is password protected and everytime i run that script dialog box pops up and I have to click on Read-Only then my script executes. Is there any way I can specify the readonly attribute in my code so i dont have... (0 Replies)
Discussion started by: dguy
0 Replies

10. Shell Programming and Scripting

perl win32::ieautomation

Hi friends, I am using win32::ieautomation to automate portal Please help me with below line of code: my $target_cell = $table_object->tableCells(2, 5); what is $table_object in above line I am using $browser->getTable('id:', "table_id")->tableCells(2,5); The error I get is can't... (0 Replies)
Discussion started by: gurukottur
0 Replies
Login or Register to Ask a Question