vincent90152900

I use Word Application object to operate a Microsoft Word file.

I would like to know the current the hight of a specific cell in the table.

When the HeightRule is wdRowHeighAuto and the Height propertys always be 9999999.0.

How to do get the current Height of the cell

Code Snippet

// Declaring the object variables we will need later
object varFileName = "c:\\test\\CellTest.docx";
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
// Create a reference to MS Word application
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
//set printer
varWord.ActivePrinter = "Microsoft Office Live Meeting Document Writer";
// Creates a reference to a word document
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
// Activate the document
varDoc.Activate();

string HeightRule =varDoc.Tables[1].Cell(0, 0).HeightRule.ToString();
float testHight=varDoc.Tables[1].Cell(0, 0).Height;




Re: Visual Studio Tools for Office How to know the current hight of the cell in a Microsoft document via c# program.

Cindy Meister

It's not possible to get this information directly, or accurately in anyway.

The cell's Height property returns not the current height of the cell, but the height set for the cell (actually, the row in which the cell is located). If no specific height has been set (as is often the case, and is the case by default), then you get back "undefined" - 9999999

If you do a Google Groups search on the word.vba.general and the word.tables newsgroups covering the last few years you'll turn up quite a few discussions about this, with various suggestions how to approximate the value, using a combination of properties. As these approaches are complex I will make no attempt to reproduce them here. Also note that "your mileage may vary", so you'll need to analyze and test the various suggestions.






Re: Visual Studio Tools for Office How to know the current hight of the cell in a Microsoft document via c# program.

vincent90152900

Thank you for replying.





Re: Visual Studio Tools for Office How to know the current hight of the cell in a Microsoft document via c# program.

vincent90152900

I found a solution.

Code Snippet

try
{
// Declaring the object variables we will need later
object varFileName = "c:\\test\\CellTest.docx";
object varFalseValue = false;
object varTrueValue = true;
object varMissing = Type.Missing;
// Create a reference to MS Word application
Microsoft.Office.Interop.Word.Application varWord = new Microsoft.Office.Interop.Word.Application();
//set printer
varWord.ActivePrinter = "Microsoft Office Live Meeting Document Writer";
// Creates a reference to a word document
Microsoft.Office.Interop.Word.Document varDoc = varWord.Documents.Open(ref varFileName, ref varMissing, ref varFalseValue, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);
// Activate the document
varDoc.Activate();

//select second cell
varDoc.Tables[1].Rows[2].Select();
//get the second cell relative position to page
object test=varWord.Selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdVerticalPositionRelativeToPage);
//page size
varDoc.PageSetup.PageHeight =(float) Convert.ToDouble(test.ToString())+75;

// Print the document
object PrintToFile = true;
object OutputFileName = "c:\\test\\CellTest.tif";
varDoc.PrintOut(ref varMissing, ref varFalseValue, ref varMissing, ref OutputFileName, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref PrintToFile, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing, ref varMissing);

varDoc.Close(ref varMissing, ref varMissing, ref varMissing);
varWord.Quit(ref varMissing, ref varMissing, ref varMissing);
}
catch (Exception varE)
{
MessageBox.Show("Error:\n" + varE.Message, "Error message");
}