RowHeight property in Excel Addin VSTO C#

RowHeight property in Excel Addin VSTO C#

RowHeight:

Returns or sets the height of the first row in the range specified, measured in points. Read/write. Since RowHeight property belongs to Range object hence entire range can be impacted with this property. In this article we will see how we can set row height property for entire range in one shot.

Prerequisites:

  • Visual Studio 2015 or above having Microsoft Office for Developer Tool installed
  • Create Excel Addin in C# code style
  • Create a ribbon designer and put button

Code example:

private void btnRowHeight_Click(object sender, RibbonControlEventArgs e)
{
	//retain active row selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//Show popup to grab input height from using
	var userValue = Globals.ThisAddIn.Application.InputBox("Enter row height:", "Adjust row height", 15.75);
	//Set rowheight property by converting user value
	double oValue;
	if (double.TryParse(userValue.ToString(), out oValue))
		oRange.RowHeight = Convert.ToDouble(userValue);            
}

In above code I have used InputBox dialog which will help capturing user input and make our example dynamic to set Height property of a row. On the dialog if user hits Cancel button the result would be boolean false hence we need to put TryParse to handle result.

Output before code execution:

Post code execution:

I input row height 40 and say OK on the dialog

Please leave your valuable comments!!!

Next : Auto adjust row height in Excel using C#

Leave a Reply

Your email address will not be published.