0
votes

I received a number of Word documents generated automatically from a third party database. Most (if not all) content is in tables. The problem is that in many cells the text is not entirely visible due to overflow. The column width and row height is set with absolute values. The column widths cannot be changed. If there is overflow, the row height should be changed to allow the text to run over more lines.

My question is: is it in any way possible to find the cells where this is the case and only for these rows set the Height to wdRowHeightAuto.

I tried just changing all rows to auto height, but this really messes up the layout.

I am now considering the following approach, but I really wonder if there is no easier way.

  1. For each Cell, set Cell.FitText to true
  2. Check if font scale has been changed
  3. Set FitText back to false
  4. If font scale was changed by FitText, set row height to Auto

Any help VBA or c# would be appreciated!

This is where I'm at, but I get runtime errors about accessing the individual column not being accessible due to different column widths. Indiviual rows are also not accessible as there are merged cells.

app = new Word.Application();

            foreach (var file in inputFiles)
            {
                var doc = app.Documents.Open(file);

                foreach (var table in doc.Tables)
                {
                    foreach (var column in table.Columns)
                    {
                        try
                        {
                            foreach (var cell in column.Cells)
                            {
                                cell.FitText = true;

                                bool textIsScaled = false;

                                if (cell.Range.Font.Scaling != 100)
                                    textIsScaled = true;

                                cell.FitText = false;

                                if (textIsScaled)
                                    cell.HeightRule = WdRowHeightRule.wdRowHeightAuto;
                            }
                        }
                        catch (Exception)
                        {
                            continue;
                        }
                    }
                }

                doc.Save();

                doc.Close();

            }
1
Have you tried using SomeTable.Rows.HeightRule = wdRowHeightAtLeast, this is keep the row height to a minimum of whatever you set it as and if there is additional text the row height will adjust as required.Jean-Pierre Oosthuizen
Thanks for the suggestion! The result looked promising. The row heights as they are now should only be changed when there is overflow. I tried setting the Table.Rows.HeightRule, but this changed too many of the row heights that have no overflow. Wanting to pass the current cell height as minimum I tried using the SetHeight(cell.Height, wdRowHeightAtLeast), but here I again have the problem that I cannot access individual rows or cells because of merged cells.lennart

1 Answers

1
votes

I found a solution thanks to Jean-Pierre Oosthuizen's suggestion to use wdRowHeightAtLeast. You can access the individual cells even if the individuals rows are not accessible (due to vertically merged cells) by using Range.Cells.

I pass the current row height as minimum for wdRowHeightAtLeast to make sure the layout is not changed if the text fits in the cells.

foreach (var table in doc.Tables)
                {
                    table.Rows.AllowBreakAcrossPages = 0;

                    var cells = table.Range.Cells;

                    foreach (var cell in cells)
                    {
                        try
                        {
                            cell.SetHeight(cell.Height, WdRowHeightRule.wdRowHeightAtLeast);
                        }
                        catch (Exception ex)
                        {
                            Logger.LogError(ex);
                            continue;
                        }
                    }
                }