Perl OLE Selection property iteration in cycle


 
Thread Tools Search this Thread
Top Forums Programming Perl OLE Selection property iteration in cycle
# 1  
Old 08-22-2012
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 this property is set only once in first paragraph and later is not updated. Please see following code:

Code:
#!/usr/bin/perl
use strict;
use warnings;
use Win32::OLE::Const 'Microsoft Word';
#$Win32::OLE::CP = CP_UTF8;
binmode STDOUT, 'encoding(utf8)';

# OPEN FILE SPECIFIED AS FIRST ARGUMENT
my $fname=$ARGV[0];
my $fnameFullPath = `cygpath.exe -wa $fname`;
$fnameFullPath =~ s/\\/\\\\/g;
$fnameFullPath =~ s/\s*$//;
unless (-e $fnameFullPath) { print "Error: File did not exists\n"; exit 1;}

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

$Word->{'Visible'} = 0;
my $doc = $Word->Documents->Open($fnameFullPath);
my $paragraphs = $doc->Paragraphs() ;
my $enumerate = new Win32::OLE::Enum($paragraphs);

# PROCESSING PARAGRAPHS
while(defined(my $paragraph = $enumerate->Next())) {

    my $text = $paragraph->{Range}->{Text};
    my $sel = $Word->Selection;
	my $font = $sel->Font;
	
	if ($font->{Size} == 18){
		print "Text: ", $text, "\n";
		print "Font Bold: ", $font->{Bold}, "\n";
		print "Font Italic: ", $font->{Italic}, "\n";
		print "Font Name: ", $font->{Name}, "\n";
		print "Font Size: ", $font->{Size}, "\n";
		print "=========\n";
	}
}

# CLOSING OLE
$Word->ActiveDocument->Close ;
$Word->Quit;

Here is output what I obtained:

Code:
Text: This is a doc file containing different fonts and size, document also contain header and footer (Font: TNR, Size: 18)
Font Bold: 0
Font Italic: 0
Font Name: Times New Roman
Font Size: 18
=========
Text: This is a Perl example (Font TNR, Size: 12)
Font Bold: 0
Font Italic: 0
Font Name: Times New Roman
Font Size: 18
=========
Text: This is a Python example(Font: Courier New, Size: 10)
Font Bold: 0
Font Italic: 0
Font Name: Times New Roman
Font Size: 18
=========

As you can see in output everywhere is Font Size 18 even if in original document are different sizes (Also font name is not updated). This brings me to assumption that $font is set only once in 1st paragraph which is processed. Thus the following condition

Code:
if ($font->{Size} == 18)

is only evaluated in 1st processed paragraph. This also supports fact that if I change condition to following (Match 2nd paragraph):

Code:
if ($font->{Size} == 12)

the output is nothing. Because first paragraph is 18 not 12 and thus the condition is false, $font is not updated any more so it wont never be true. What I am doing wrong ?


Many thanks
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Iteration in perl

How to iterate the contents in below file using PERL? url.txt (2 Replies)
Discussion started by: vel4ever
2 Replies

2. 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

3. Shell Programming and Scripting

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... (0 Replies)
Discussion started by: wakatana
0 Replies

4. Shell Programming and Scripting

Report a missing property and property value mis match script.

Hi All, I have 2 properties files - one is a master templete and other one is a node specific properties file, I need to comapre these 2 properties files and make sure the node Specific properties file contains all the properties in the master temple properties file else report the missing... (5 Replies)
Discussion started by: jayka
5 Replies

5. 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

6. Shell Programming and Scripting

perl use property file for variables

if I have a file that contains variables. How do I assign them in a script. file p=c e=g Thanks (3 Replies)
Discussion started by: 3junior
3 Replies

7. 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

8. Shell Programming and Scripting

GUI selection for perl scripts

Hi, I need to develop a GUI for Perl scripts which needs frequent user interactions(like getting the inputs from the user at various stages). Can anyone suggest a language to develop a GUI which is simple to use and which can be implemented within a short time frame? Thanks, Vishwa (1 Reply)
Discussion started by: vishwa787
1 Replies

9. 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

10. Shell Programming and Scripting

Iteration on line number using perl

hi freinds, I am writing a script using perl My input file will be looking like this OSILANConnectorTypeDetected RO 11713000 OSILANSignalTypeOverride RW 11610000 OSILANSignalTypeDetected RO 11620000 OSILANSignalTypeAdaptive RW 11630000 Now i need the control go to 1st line 3rd... (4 Replies)
Discussion started by: jyo123.jyothi
4 Replies
Login or Register to Ask a Question