I have an excel which contains textboxes/shapes which I am going to fill in with specific data. I used the following code in identifying each shape:
//using Excel = Microsoft.Office.Interop.Excel;
Excel.Worksheet xlWorkSheet
foreach(Excel.Shape shp in xlworksheet.Shapes)
{
//code to add text to shape goes here....
}
I also tried using:
shp.TextFrame2.TextRange.Characters.Text = "Test";
and
shp.TextFrame.Characters(Type.Missing, Type.Missing).Text = "Test";
but gives an error which states The specified value is out of range
and Member not found. (Exception from HRESULT: 0x80020003 (DISP_E_MEMBERNOTFOUND))
, respectively.
What should be the correct approach in adding texts to existing textboxes?
You have to check if the type of Shape
is msoTextBox
before setting text.
Excel.Worksheet xlWorkSheet
foreach (Excel.Shape shp in xlworksheet.Shapes)
{
if (shp.Type == Microsoft.Office.Core.MsoShapeType.msoTextBox)
{
shp.TextFrame.Characters(Type.Missing, Type.Missing).Text = "Test";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With