In order for the contents of the table to be displayed, I manually set row 3 of the table in c.docx by selecting “Minimum” in the “Row Height Value” dropdown and it worked. But I can’t do it with the following code
The github path for c.docx is https://github.com/angmangm/test
enter image description here
static void Main(string[] args)
{
Dictionary<int, float> rowHeights = new Dictionary<int, float>();
string filePath = @"D:c.docx";
Application wordApp = new Application();
Document doc = wordApp.Documents.Open(filePath, ReadOnly: false, Visible: true);
try
{
if (doc.Tables.Count > 0)
{
// Iterate through each table
for (int tableIndex = 1; tableIndex <= doc.Tables.Count; tableIndex++)
{
Table table = doc.Tables[tableIndex];
int rowCount = table.Rows.Count;
Range range = table.Range;
for (int i = 1; i <= range.Cells.Count; i++)
{
Cell cell = range.Cells[i];
string s = cell.Range.Text;
float cellBottom = (float)cell.Range.Information[WdInformation.wdVerticalPositionRelativeToPage] + cell.Height;
// Check each InlineShape inside the cell
foreach (InlineShape inlineShape in cell.Range.InlineShapes)
{
float shapeBottom = (float)inlineShape.Range.Information[WdInformation.wdVerticalPositionRelativeToPage] + inlineShape.Height;
if (shapeBottom > cellBottom)
{
if (!rowHeights.ContainsKey(cell.RowIndex))
{
rowHeights.Add(cell.RowIndex, shapeBottom - cellBottom);
}
}
}
}
object what = WdGoToItem.wdGoToTable;
object which = WdGoToDirection.wdGoToFirst;
object count = tableIndex;
wordApp.Selection.GoTo(ref what, ref which, ref count);
// Iterate through each row
for (int rowIndex = 1; rowIndex <= rowCount; rowIndex++)
{
// Selected row, but no need to move at the first row
if (rowIndex > 1)
{
// Move down to the next row
wordApp.Selection.MoveDown(WdUnits.wdLine, 1);
}
// Select the current row
wordApp.Selection.Rows.Select();
if (rowHeights.ContainsKey(rowIndex))
{
// Set row height to minimum
wordApp.Selection.Rows.HeightRule = WdRowHeightRule.wdRowHeightAtLeast;
wordApp.Selection.Rows.Height = wordApp.CentimetersToPoints(float.Parse("0.8"));
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
finally
{
doc.Close(SaveChanges: true);
wordApp.Quit();
}
}
Using c# to directly change the height value doesn’t work, however, manually dragging the conditional row height works. I don’t know what the reason is, or if there is any other way to show the hidden content in the table.