Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1210
Insert Text Box and calculate width
posted

Is there any way to insert a Text Box into an Excel sheet. I found number of examples of how to insert image and comments, but nothing related to inserting text boxes.

Another issue I'm having is when placing images on a sheet. I know the width of the images in pixels and I know the width of the cells in "1/256s of average character width" (the width property on the cell). How can I convert that to pixels?

Thanks in advance,

Trausti

Parents
  • 44743
    posted

    In the future, please post individual questions separately so the threads are easier to to follow for everyone else. Thanks.

    Text boxes are currently not fully supported, but they do have round trip support, which means if you load in a Workbook which has text boxes on it, modify it, and then save it out again, the text boxes will still be there. So if you only need a text box in a predefined location, you could have a Excel file that you use as a template which has the text boxes. Then, instead of creating a new Workbook object, load one from the existing file. Add the data to it and then save it.

    In fact, you can even modify the position and text of the text boxes in the loaded Workbook because they are accessible in the Worksheet.Shapes collection as UnknownShape instances, which have Text and position properties. So all you really need in the preexisting file is the correct number of text boxes. However, if you need to dynamically create an unknown number of text boxes, you will need to submit a feature request for this ability.

    As for positioning shapes relative to cells, I would recommend using twips instead of pixels or 1/256th of the average character width. The WorksheetCell object defines a couple GetBoundsInTwips methods which tell you the twip bounds the cell occupies on the worksheet. The WorksheetShape class defines similar methods as well as SetBoundsInTwips methods. So if you want to position a shape directly over a cell, you could to something like this:

    shape.SetBoundsInTwips(cell.Worksheet, cell.GetBoundsInTwips());

Reply Children