Question

Is there a method to find out the justification of a paragraph in a MS Word document. Can any one help me out?

Était-ce utile?

La solution

Using OLE, it looks like you can get the justification (or alignment) via the ParagraphFormat2 object, which has an Alignment property. Here's an example from the OLE docs:

ActivePresentation.Slides(1).Shapes(2).TextFrame2.TextRange2.ParagraphFormat2.Alignment

You can read more about this object here.

To provide a Perl example of this, take a look at this example:

use strict;
use warnings;
use Win32::OLE qw(in with);
use Win32::OLE::Const 'Microsoft Word';
use Win32::OLE::Variant;

my $word = Win32::OLE->GetActiveObject('Word.Application')
  || Win32::OLE->new( 'Word.Application', 'Quit' );
$word->{Visible} = 1;
my $doc = $word->{Documents}->Open('<full path to file>');
print $doc->Paragraphs(1)->{Alignment} . "\n";
$doc->Close();

You'll need to install the Win32::OLE library on a machine with Microsoft Word installed on it, at minimum. When writing Perl applications to use OLE, anything that is an OLE object is a method call and anything that is a OLE member is a hash reference.

When you go to open the file, you'll need to provide the full path to the file, i.e. 'C:\\folder\\doc.docx'. Change the number passed to Paragraphs for whichever paragraph you want (in OLE arrays start at 1.)

The Alignment key will return an int, which correspondes to a WdParagraphAlignment Enumeration. I was able to test this out; 0 => Left, 1 => Center, 2 => Right, 3 => Justified.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top