3

I'm trying to design a table that has 3 additional tables in the last cell. Like this. Nested table example

I've managed to get the first nested table into row 4, but my second nested table is going into cell(1,1) of the first table.

Incorrect nested table example

var wordApplication = new Word.Application();
wordApplication.Visible = true;
var wordDocument = wordApplication.Documents.Add();
var docRange = wordDocument.Range();
docRange.Tables.Add(docRange, 4, 1);
var mainTable = wordDocument.Tables[1];
mainTable.set_Style("Table Grid");
mainTable.Borders.Enable = 0;
mainTable.PreferredWidthType = Word.WdPreferredWidthType.wdPreferredWidthPercent;
mainTable.PreferredWidth = 100;
docRange.Collapse(Word.WdCollapseDirection.wdCollapseStart);

var phoneRange = mainTable.Cell(4, 1).Range;
phoneRange.Collapse(Word.WdCollapseDirection.wdCollapseStart);
phoneRange.Tables.Add(phoneRange, 3, 2);

var phoneTable = mainTable.Cell(4, 1).Tables[1];
phoneTable.set_Style("Table Grid");
phoneTable.Borders.Enable = 0;
phoneTable.AutoFitBehavior(Word.WdAutoFitBehavior.wdAutoFitContent);
phoneTable.Rows.RelativeHorizontalPosition = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionMargin;

phoneRange.Collapse(Word.WdCollapseDirection.wdCollapseEnd);

I've tried collapsing the range, adding in a paragraph then collapsing the range again. No luck. I found this post and many similar ones, but I must be missing something.

Thanks for your time.

8
  • It usually helps in situation like these to add a line in your code: phoneRange.Select(); and having code execution end with that. Take a look at where the Range actually is. My gut feeling is you're definitely on the right track, but may need to move the Range.End back a character or something like that: phoneRange.MoveEnd(ref oWdUnitCharacter, ref oMinusOne); FWIW your bigger problem may turn out getting the tables nicely next to one another in the same row. Commented Dec 3, 2015 at 16:07
  • Indeed, the biggest problem is to align the nested tables next to each other. You would have to use floating tables to do so (Table.Rows.WrapAroundText = true;) Commented Dec 3, 2015 at 16:41
  • @CindyMeister Adding in phoneRange.MoveEnd(Word.WdUnits.wdCharacter, -1); phoneRange.Tables.Add(phoneRange, 3, 1); results in error the object refers to the end of a table row -2 results in the table going into the row above. I've tried various combinations but haven't got much further. Thanks for your help so far. Commented Dec 4, 2015 at 9:38
  • And did you try my suggestion about selecting the range and stopping execution so you can see exactly where things are? Then try getting the result you need using the keyboard until you hit on the right combination. I DID test my suggestion and it DID work in my test environment (as far as inserting the second table in the same cell) - but without more detailed information from you we can't duplicate the test environment. So you need to do some research yourself within the Word UI... Commented Dec 4, 2015 at 15:36
  • 1
    Outside the third row? Odd... Try setting a new Range object to phoneTable.Range then collapse that to its end point. Is that any better? Commented Dec 4, 2015 at 22:13

1 Answer 1

1

It usually helps in situation like these to add a line in your code: phoneRange.Select(); and having code execution end with that. Take a look at where the Range actually is. Now you can test using the keyboard where the Range needs to be in order to insert the next table successfully.

Since you say phoneRange selects outside the third row, rather than working with phoneRange try setting a new Range object to phoneTable.Range then collapse it to its end-point.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.