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.
- For each Cell, set Cell.FitText to true
- Check if font scale has been changed
- Set FitText back to false
- 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();
}
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