Toggle WrapText for column in Excel Addin C#

Toggle WrapText for column in Excel Addin C#

WrapText:

Wrapping text fits overflown text withing cell boundary. Microsoft Office WrapText is a boolean property which has set and get methods returns true, false.

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

Input source:

In above screenshot the text written in Cell “A1” is overflowing. Below code will wrap text inside cell and upon toggling it back it will unwrap the text:

Code Wrap Text:

private void btnWrapText_Click(object sender, RibbonControlEventArgs e)
{
	//Capture user selection
	excel.Range oRange = Globals.ThisAddIn.Application.Selection;
	
	//Toggle Wrap property
	if(!oRange.WrapText)
		oRange.WrapText = true;
	else
		oRange.WrapText = false;
}

Output Wrap Text:

Let’s click back and see the unwrap text will happen as shown below:

Next: Toggle merge/unmerge cell contents in Excel VSTO C#

Leave a Reply

Your email address will not be published. Required fields are marked *