Hoping someone can help me with a specific problem using Win32::OLE and Word with a Table. I've followed all the advice from previous questions on this topic (many times) and have successfully used Perl to create and manipulate documents, and Tables, but one thing eludes me. Setting "KeepWithNext" on a Table Cell.

In Word I can select a Cell (or even a whole column), go into Paragraph options and tick "Keep with next"

Word Macro code for this is:
Selection.ParagraphFormat.KeepWithNext = True

Similar macro code (for text alignment) is like this:
Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

And I can get the alignment working in Perl easily with:
$word->Selection->ParagraphFormat->{Alignment} = wdAlignParagraphCenter;

But...
$word->Selection->ParagraphFormat->{KeepWithNext} = 1;

Fails with an error "The parameter is incorrect.". I've tried various different syntax (after web searching) such as:

$word->Selection->{ParagraphFormat}->{KeepWithNext} = 1; $word->Selection->ParagraphFormat({ KeepWithNext => 1 }); $word->Selection->ParagraphFormat->KeepWithNext = 1;

All give an error - sometimes the same as above, normally even more obtuse.
$word->Selection->ParagraphFormat->KeepWithNext(1); Didn't give an error but didn't do anything either.

Has anybody got KeepWithNext to work, specifically on a Word Table?

I understand the rules for this option are slightly different because this is a table and the option we're setting is for a paragraph but it seems illogical that the Alignment code works fine where the other doesn't.

UPDATE: I've managed to solve this myself but it might be useful for future reference. Whilst testing I noticed that some values for "True" didn't flag an error but none of them actually set the KeepWithNext checkbox. However, when I tried -1 it worked!
So the solution, using my simple examples is just:

$word->Selection->ParagraphFormat->{KeepWithNext} = -1;


In reply to [SOLVED] Win32::OLE, Word, a Table and KeepWithNext by Snospar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.