0

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

user3510818
  • 103
  • 1
  • 12

1 Answers1

1

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

Community
  • 1
  • 1
MartynA
  • 30,454
  • 4
  • 32
  • 73
  • When you right click on a word table, and click on table properties. There is a button called positioning Click on it. Then the new window has at the top. Horizontal group box, and beneath it a vertical group box. Both have 2 drop downs in. that is where the position values must be inserted. – user3510818 Jul 04 '14 at 08:37
  • TableRows.RelativeHorizontalPosition := wdRelativeHorizontalPositionMargin; TableRows.RelativeVerticalPosition := wdRelativeVerticalPositionParagraph; Here you have defined the right drop downs in the group box. Now i just need to know hoe to define the left(position:) drop downs. I do not need to define the "Distance from surrounding text" values. – user3510818 Jul 04 '14 at 08:40
  • I've finally managed to solve the problems I was having and hope my answer now fully answers your question. I've deleted my earlier comments because they no longer apply. I have a few more grey hairs than when I started out writing it, so I'd appreciate it if you'd accept my answer if it works for you. – MartynA Jul 04 '14 at 18:55
  • I see you have just posted a new q about hiding a table column. I might look at that later, but you haven't accepted my answer to this q yet, from several days ago, which makes me wonder if you could not get it to work? – MartynA Jul 09 '14 at 12:19