Setting Excel cell to Textbox.text from Visual Studio, Reference Error C# -
public excel.application xlapp = new excel.application(); public excel.range range; public excel.workbook workbook; public excel.worksheet worksheet; public string file; private void button1_click(object sender, eventargs e) { file = path.combine(application.startuppath, "quote sheet template.xls"); xlapp.workbooks.open(file); workbook = (excel.workbook)xlapp.activeworkbook; xlapp.visible = true; range.get_range("d6"); range.value2 = nametake.text; workbook.saveas(path.combine(application.startuppath, nametake.text + " quote.xls")); }
so here's deal... puts there info (in case name) text box. click button 1 export thier name excel template saves in thier own name.
the problem when gets to
range.get_range("d6"); /*and*/ range.value2 = nametake.text;
it throws , exception saying
object reference not set instance of object
when try set instance (i.e)
range = new excel.range();
it throws error saying
cannot create instance of abstract class or interface 'microsoft.office.interop.excel.range
probably obvious. can guys please me :)
updated code:
private void button1_click(object sender, eventargs e) { file = path.combine(application.startuppath, "quote sheet template.xls"); xlapp.workbooks.open(file); workbook = (excel.workbook)xlapp.activeworkbook; xlapp.visible = true; range = worksheet.cells["d6"] excel.range; if (range != null) { //range.value2 = nametake.text; workbook.saveas(path.combine(application.startuppath, nametake.text + " quote.xls")); } }
getting exception(for range) : object reference not set instance of object
you never assign variable range
in code hence null , error appropriate.
in vsto work off getting target range current sheet range such as:
var range = thesheet.cells[ 1, 4 ] excel.range; // [ row, column ]
verify have not null work; programming. such check if workbook in above code not null before use. such in example above, follow this
if (range != null) { ... valid operation ... }
Comments
Post a Comment