Snospar has asked for the wisdom of the Perl Monks concerning the following question:

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;

Replies are listed 'Best First'.
Re: Win32::OLE, Word, a Table and KeepWithNext
by choroba (Cardinal) on Jun 19, 2015 at 11:20 UTC
    Guessing based on Problem with Excel and Win32::OLE: should't you use 'True' instead of 1 for the value?
    لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      Should have put in my original post that I'd already tried different values for "True" (including wdTrue, msoTrue, 'True') and none of them work. Most often I got an exception stating "Bad parameter". In another thread it was confirmed that 1 should be synonymous with "True" in this situation.