简体   繁体   中英

How to Change “Table Positioning” in the table properties Inside a MS Word Document Using Delphi XE5

I apologize in advance, This is very confusing to explain. Please assist in making it clearer if need be.

I am working with a MS Word document that i generate from code using the following code. The document has 1 table with a bunch of rows and columns that i intend to populate.

 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;

Basically what i am trying to do is change the following values:

Right Click on the table->Table Properties->Table Tab

Text Wrapping = Around

->Positioning->Horizontal:

Position = -0.18"
Relative To = Margin

->Positioning->Vertical:

Position = -0.63"
Relative To = Paragraph 

->Positioning->Options:

Move With Text = True
Allow Overlap = True

I have not been able to find any code to assist me. Or even any Code samples that handle changing the text wrapping to around using code in Delphi. So any assistance would be great.

Thank You

The following code does what you asked using D7 and Word2007.

You don't say whether your unit already uses one of the Delphi import units for the MS Word type libraries. You'll need to use one, because that's where the constants like wdTableLeft are defined. I'm using D7 (+Word 2007), so I used the Word2000 import unit that came with D7.

Also my Table and TablesRows are OleVariants which you'll need to add to your code if you don't declare them already.

First thing is that you'll need to add some code above your procedure which creates the table. The reason for this is explained below.

const
  CmToPostScriptPoints : Single = 28.3464567;
  InchesToCm : Single = 2.54;

function CentimetersToPoints(Centimeters : Single) : Single;
begin
  Result := CmToPostScriptPoints * Centimeters;
end;

Then replace the code in your q by the following. Please read the embedded comments carefully because they explain a couple of problems I ran into which took a long time to figure out.

  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);

The reason I've defined a CentimetersToPoints function rather than the Word Application's CentimetersToPoints is that there seems there is a long-standing problem with trying to call CentimetersToPoints from Delphi code - if you're interested, see this SO q and its comments to the answer:

Unspecified error when calling Word CentimetersToPoints via OLE

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM