Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Using OLE to view given Paragraph in MS Word Document

by ricDeez (Scribe)
on Nov 21, 2011 at 21:37 UTC ( #939328=note: print w/ replies, xml ) Need Help??


in reply to Using OLE to view given Paragraph in MS Word Document

I managed to get this to work with the changes made as per below:

#!/usr/bin/perl -w # Simple case to open MS Word Document and view Nth paragraph use strict; use warnings; use 5.012; use Win32::OLE; use Win32::OLE::Enum; use Cwd qw(getcwd abs_path); my $ParaNo = 10; # Default target paragraph # my $InFile = shift if @ARGV > 0; # Required file name ##################################################################### # For the purposes of testing, I hard-coded the file name and path ##################################################################### my $InFile = "C:/Users/Ric/Desktop/Report-WirelessSurvey.doc"; ##################################################################### # The following makes the code less portable, requiring $app_name to # be modified accordingly! ##################################################################### # my $app_name = "Word.Application.8"; # Word's application nam +e # my $app; ##################################################################### # This approach will use the active instance or will open word if # required ##################################################################### my $doc = Win32::OLE->GetObject ( $InFile ) or die "Could not load $InFile. \n"; my $app = $doc->{Application}; $app->{Visible} = 1; ##################################################################### # This is a good idea ##################################################################### $app->{DisplayAlerts} = 0; # eval {$app = Win32::OLE->GetActiveObject($app_name)}; # Use insta +nce if already running # die "Word ($app_name) is not installed" if $@; # if (!defined($app)) { # $app = Win32::OLE->new($app_name, sub {$_[0]->Quit;}) # || die "Could not connect to $app_name $!"; # } # $app->{'Visible'} = 1; # my $abspath = abs_path($InFile); # Word appears to need absolute +path # my $doc = $app->Documents()->Open({ # FileName => $abspath, # ReadOnly => 0, # }); # die "Can't open doc $abspath: $!" if !defined($doc); ##################################################################### # Why are you using Win32::OLE::Enum? ##################################################################### my $paragraphs = $doc->Paragraphs(); # my $enumerate = new Win32::OLE::Enum($paragraphs); # if (!defined($enumerate)) { # die "Can't get enumerate for $InFile"; # } my $paragraph; # for (my $i = 0; $i<$ParaNo; $i++) { for my $i ( 1 .. $paragraphs->Count()){ last if $i > $ParaNo; #Forgot that you wanted to stop here! $paragraph = $paragraphs->Item( $i ); ################################################################## # This bit needs to be in the loop! ################################################################## my $style = $paragraph->{Style}->{NameLocal}; my $text = $paragraph->{Range}->{Text}; print "style=$style text=$text\n"; # print "Why doesn't the view show this location?\n"; # print "ENTER to quit\n"; # my $ans = <>; # $paragraph = $enumerate->Next(); }

Try these changes and let me know how you go. This may still trip up on unicode characters!


Comment on Re: Using OLE to view given Paragraph in MS Word Document
Download Code
Re^2: Using OLE to view given Paragraph in MS Word Document
by Ray Smith (Beadle) on Nov 22, 2011 at 00:40 UTC
    Thanks for the example.

    I tried it, first:
    1. using my own test file.
    2. Changing to use 5.10, because that's what I have.
    3. use abs_path(input) file because Word appears to require absolute path.

    Things operate with out error, but my Windows display still leaves the cursor at the beginning of the file.

    Am I missing something here?

      I don't really understand what you want to do!

      If you need to view the paragraphs being selected you could add the following:

      for my $i ( 1 .. $paragraphs->Count()){ last if $i > $ParaNo; $paragraph = $paragraphs->Item( $i ); $paragraph->{Range}->Select(); # <<<<<Added sleep(1); # <<<<<Added my $style = $paragraph->{Style}->{NameLocal}; my $text = $paragraph->{Range}->{Text}; print "style=$style text=$text\n"; }

      I have used placed the sleep in the loop so that you can see the paragraphs being selected in turn, otherwise it would just happen too quickly - especially since you are only interested in the first 10 paragraphs!

        Your advice was spot on! This is what I was missing, to view the specified paragraph:
        $paragraph = $paragraphs->Item( $i ); $paragraph->{Range}->Select(); # <<<<<Added
        I appologize for not being clear. I have a simple application that uses Microsoft Word for user generated input source. My goal, which I can now approach with your help, is, when an error in this user input file is discovered, to bring up the source file with the view positioned to facilitate the user making required modifications to correct the error(s).

        Thanks again for the help.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://939328]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2014-07-31 23:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (255 votes), past polls