我正在处理使用以下代码从代码生成的MS Word文档。该文档有一个表,其中包含一堆我打算填充的行和列。
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中的代码处理将文本换行更改为周围的任何代码示例。所以任何帮助都会很棒。
谢谢
答案 0 :(得分:1)
以下代码执行您使用D7和Word2007提出的问题。
您没有说明您的单位是否已使用其中一个Delphi导入单位作为MS Word类型库。你需要使用一个,因为这是定义像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及其对答案的评论:
Unspecified error when calling Word CentimetersToPoints via OLE