[英]How to Change “Table Positioning” in the table properties Inside a MS Word Document Using Delphi XE5
我事先表示歉意,这很难解释。 如果需要,请协助使其更清晰。
我正在使用通过以下代码从代码生成的MS Word文档。 该文档具有1个表,其中包含我打算填充的一堆行和列。
wrdDoc.Tables.Add(wrdSelection.Range,9,2);
wrdDoc.tables.Item(1).Rows.Alignment := wdAlignRowLeft;
wrdDoc.Tables.Item(1).Columns.Item(1).SetWidth(155,wdAdjustNone);
wrdDoc.Tables.Item(1).Columns.Item(2).SetWidth(299,wdAdjustNone);
wrdDoc.tables.Item(1).Borders.Item(wdBorderLeft).LineStyle := wdLineStyleSingle;
wrdDoc.tables.Item(1).Borders.Item(wdBorderRight).LineStyle := wdLineStyleSingle;
wrdDoc.tables.Item(1).Borders.Item(wdBorderVertical).LineStyle := wdLineStyleSingle;
wrdDoc.tables.Item(1).Borders.Item(wdBorderTop).LineStyle := wdLineStyleSingle;
wrdDoc.tables.Item(1).Borders.Item(wdBorderBottom).LineStyle := wdLineStyleSingle;
wrdDoc.tables.Item(1).Borders.Item(wdBorderHorizontal).LineStyle := wdLineStyleSingle;
基本上我想做的是更改以下值:
右键单击表->表属性->表选项卡
Text Wrapping = Around
- > Positioning->水平:
Position = -0.18"
Relative To = Margin
- > Positioning->垂直:
Position = -0.63"
Relative To = Paragraph
- > Positioning->选项:
Move With Text = True
Allow Overlap = True
我找不到任何代码可以帮助我。 甚至任何使用Delphi中的代码来处理将文本换行更改为周围的代码示例。 因此,任何帮助都会很棒。
谢谢
下面的代码执行您使用D7和Word2007进行的操作。
您没有说您的单位是否已经使用MS Word类型库的Delphi导入单位之一。 您将需要使用一个,因为在那里定义了wdTableLeft之类的常量。 我正在使用D7(+ Word 2007),所以我使用了D7随附的Word2000导入单元。
我的Table和TablesRows也是OleVariants,如果您尚未声明它们,则需要将它们添加到代码中。
首先,您需要在创建表的过程上方添加一些代码。 其原因在下面说明。
const
CmToPostScriptPoints : Single = 28.3464567;
InchesToCm : Single = 2.54;
function CentimetersToPoints(Centimeters : Single) : Single;
begin
Result := CmToPostScriptPoints * Centimeters;
end;
然后通过以下替换q中的代码。 请仔细阅读嵌入的注释,因为它们解释了我遇到的几个问题,这些问题花了很长时间才能弄清楚。
Table := wrdDoc.Tables.Add(wrdSelection.Range, 9, 2);
TableRows := Table.Rows;
TableRows.WrapAroundText := True;
// TableRows.MoveWithText := True;
// Note: If you un-comment the line above it will cause an exception
// Method "MoveWithText" not supported by automation object
// However, even with MoveWithText commented out, the corresponding property
// in Word's table properties will still be ticked by the time the code is finished
TableRows.AllowOverlap := True;
Table.Rows.Alignment := wdAlignRowLeft;
Table.Columns.Item(1).SetWidth(155,wdAdjustNone);
Table.Columns.Item(2).SetWidth(299,wdAdjustNone);
Table.Borders.Item(wdBorderLeft).LineStyle := wdLineStyleSingle;
Table.Borders.Item(wdBorderRight).LineStyle := wdLineStyleSingle;
Table.Borders.Item(wdBorderVertical).LineStyle := wdLineStyleSingle;
Table.Borders.Item(wdBorderTop).LineStyle := wdLineStyleSingle;
Table.Borders.Item(wdBorderBottom).LineStyle := wdLineStyleSingle;
Table.Borders.Item(wdBorderHorizontal).LineStyle := wdLineStyleSingle;
TableRows.RelativeHorizontalPosition := wdRelativeHorizontalPositionMargin;
TableRows.RelativeVerticalPosition := wdRelativeVerticalPositionParagraph;
TableRows.HorizontalPosition := CentimetersToPoints(-0.18 * InchesToCm) ;
// Note : At first, I thought the line above didn't do anything because
// after this routine finishes, the HorizontalPosition in Word
// under TableProperties | Positioning is shown as Left.
//
// However, if you put a breakpoint on the line above,
// and single-step past it, you should see the table shift leftwards
// when the line is executed. The ShowMessage should display - 0.179[...]
ShowMessage(FloatToStr(TableRows.HorizontalPosition / 72)); // 72 PostScript points to an inch
TableRows.VerticalPosition := CentimetersToPoints(-0.63 * InchesToCm);
我定义CentimetersToPoints函数而不是Word应用程序的CentimetersToPoints的原因是,尝试从Delphi代码中调用CentimetersToPoints似乎存在一个长期的问题-如果您有兴趣,请参阅此SO q及其注释。回答:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.