Indent level in Excel Addin C#

Indent level in Excel Addin C#

IndentLevel:

IndentLevel is a property which derived through Range object in Excel. The job of indent level to move text forward or backward within cell. Ideally when user wants to represents data in a hierarchy the Indent Level pays a good role to meet the expectation in Excel.

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

Let’s put some come to to play with indentation in Excel cell dynamically

Source Input:

Code example Increase Indent Level:

private void btnIncreaseIndent_Click(object sender, RibbonControlEventArgs e)
{
	//Capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	//get current indent level
	long indentLevel = oRange.IndentLevel;
	//indent
	oRange.IndentLevel++;
}

Above code needs to be put on button click event that I have created on Ribbon named as “Increase Indent”.

Output:

The code written in dynamic manner hence the number of times you click on button the indent level gets increased and cell content start shifting to the next level.

Code example Decrease Indent Level:

private void btnDecreaseIndent_Click(object sender, RibbonControlEventArgs e)
{
	//Capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	
	//check indent level
	if(oRange.IndentLevel > 0)
		oRange.IndentLevel--;

}

Output:

Please leave your valuable comments!!!

Next: Toggle wraptext property of cell in Excel VSTO C#

Leave a Reply

Your email address will not be published.