ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   C# adding textbox to excel sheet (https://www.excelbanter.com/excel-programming/395004-c-adding-textbox-excel-sheet.html)

SkyKid

C# adding textbox to excel sheet
 
Anytime I try to add a textbox to my excel sheet in the following manner an
exception is generated.

Excel.Shape textbox;
textbox =
(Excel.Shape)excelWorksheet.Shapes.AddFormControl( Excel.XlFormControl.xlEditBox, cellx, celly, cellwidth, cellheight);

What am I doing wrong?

Vergel Adriano

C# adding textbox to excel sheet
 
This worked for me, but it's in VB.. But I believe it shouldn't be too hard
to port to C#.


Dim xlApp As New Excel.Application
Dim xlWb As Excel.Workbook
Dim sht As Excel.Worksheet
Dim tb As Excel.OLEObject

xlWb = xlApp.Workbooks.Add
With xlWb.Sheets("Sheet1")
sht = xlWb.Worksheets("Sheet1")
tb = sht.OLEObjects.add(ClassType:="Forms.TextBox.1",
Link:=False, _
DisplayAsIcon:=False, Left:=120, Top:=48, Width:=132,
Height:=53.25)
End With

xlWb.SaveAs("C:\TEMP\Sample.xls")

xlWb.Close(False)
xlApp.Quit()



--
Hope that helps.

Vergel Adriano


"SkyKid" wrote:

Anytime I try to add a textbox to my excel sheet in the following manner an
exception is generated.

Excel.Shape textbox;
textbox =
(Excel.Shape)excelWorksheet.Shapes.AddFormControl( Excel.XlFormControl.xlEditBox, cellx, celly, cellwidth, cellheight);

What am I doing wrong?



All times are GMT +1. The time now is 01:26 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com