Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Issues in Setting Word margins using Win32::OLE

by shantanu_bhadoria (Novice)
on May 27, 2008 at 07:45 UTC ( #688615=perlquestion: print w/ replies, xml ) Need Help??
shantanu_bhadoria has asked for the wisdom of the Perl Monks concerning the following question:

Hi I had this unique issue with setting page margins for a word file using Win32::OLE

The ides is to set right and left margins for any word document. here is the snippet of code i am having trouble with :
$margins_hash{LeftMargin} = $left_margin; $margins_hash{RightMargin} = $right_margin); with( $ExcelObject->{ActiveDocument}->{PageSetup}, %margins_hash );

The issue with this is that if the papersize is such that width is 8 and the initial right margin for the document before editing was 7.

the Win32::OLE::with function sets the margins in sequence and not together. so if i want to set margins to 2 and 2 in the file, I won't be able to do that since when OLE tries to set the left margin to 2 the word app croaks, since the right margin is still 7 and paper size is 8 so can't have left and right margins of 7 and 2 for a paper width of 8.

the with in VBA allows this adjustment when we pass multiple params to the VBA with() , so I looked into the Win32/OLE/ and saw that the with() function is simply a perl implementation of the VBA function which traverses the hash the perl way without any adjustments as required above. Does anyone know a way I can resolve the issue. An obvious hack around this issue is to set both margins to 0 first and then to the desired value but i wish there was a cleaner way to do this. Maybe by calling the VBA with() directly in some way. Just blurbing in the dark here, would appreciate some pointers.
Shantanu Bhadoria

Comment on Issues in Setting Word margins using Win32::OLE
Download Code
Re: Issues in Setting Word margins using Win32::OLE
by nbartusi (Scribe) on May 28, 2008 at 12:25 UTC
    A slightly cleaner method would be to change the larger existing margin to the new value first. While I haven't seen the module code adding a sort on the existing margins during the hash traversal sounds possible.
      Here is the funny twist with that solution. I am also setting papersize before i set margins and word allows the paper size to be set to invalid values. e.g. at initial margins of 7,2 initial paper size being 10 by 10 if i set the papersize to say 8 by 8 the word app would allow it. and then we are totally stuck cuz the app wouldn't hear of allowing either right or left margin to be set alone. the funny thing is it allows the VBA to set the margins but only if it is within a with and if both the margin value together make a valid pair.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://688615]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (12)
As of 2014-10-21 16:57 GMT
Find Nodes?
    Voting Booth?

    For retirement, I am banking on:

    Results (106 votes), past polls