c# - Unable to write to or read from Worksheet.CustomProperties in a console application -


in console application, trying write , read customproperties of excel worksheet. have reference microsoft.office.interop.excel v14 assembly.

at line calls firstworksheet.customproperties.add method, exception hresult 0x800a03ec.

below relevant bit of code:

static void writetoexcelcustomdocumentproperties(     string excelfile,     string outputfolder,     string propertyname,      object propertyvalue) {     excel::application excel = null;     workbook workbook = null;     worksheet firstworksheet = null;      try     {         excel = new excel::application();          workbook = excel.workbooks.open(excelfile);          firstworksheet = workbook.worksheets[1] worksheet;          firstworksheet.customproperties.add(propertyname, propertyvalue);          var outputfilepath = getoutputfilepath(excelfile, outputfolder);          workbook.saveas(outputfilepath);     }     catch(exception ex)     {         console.writeline("\nerror:");         console.writeline($"{excelfile}!{firstworksheet.name}");         console.writeline($"{ex.message}\n");     }         {         if (workbook != null)             workbook.close();          if (excel != null)             excel.quit();     } } 

and below error receive:

{"exception hresult: 0x800a03ec"}     data: {system.collections.listdictionaryinternal}     errorcode: -2146827284     hresult: -2146827284     helplink: null     ipforwatsonbuckets: 0x7177fe49     innerexception: null     istransient: false     message: "exception hresult: 0x800a03ec"     remotestacktrace: null     source: ""     stacktrace: "   @ system.runtimetype.forwardcalltoinvokemember(string membername, bindingflags flags, object target, int32[] awrappertypes, messagedata& msgdata)\r\n   @ microsoft.office.interop.excel.customproperties.add(string name, object value)\r\n   @ customdocumentproperties.program.writetoexcelcustomdocumentproperties(string excelfile, string outputfolder, string propertyname, object propertyvalue) in c:\\sathyaish\\dotnet\\customdocumentproperties\\customdocumentproperties\\program.cs:line 61"     targetsite: {system.object forwardcalltoinvokemember(system.string, system.reflection.bindingflags, system.object, int32[], system.runtime.remoting.proxies.messagedata byref)}     watsonbuckets: null     _hresult: -2146827284     _classname: null     _data: {system.collections.listdictionaryinternal}     _dynamicmethods: null     _exceptionmethod: {system.object forwardcalltoinvokemember(system.string, system.reflection.bindingflags, system.object, int32[], system.runtime.remoting.proxies.messagedata byref)}     _exceptionmethodstring: null     _helpurl: null     _innerexception: null     _ipforwatsonbuckets: 0x7177fe49     _message: "exception hresult: 0x800a03ec"     _remotestackindex: 0     _remotestacktracestring: null     _safeserializationmanager: {system.runtime.serialization.safeserializationmanager}     _source: ""     _stacktrace: {sbyte[96]}     _stacktracestring: null     _watsonbuckets: null     _xcode: -532462766     _xptrs: 0x00000000 

and if try read information using code listed below, exception follows code listing.

static object readfromexcelcustomdocumentproperties(     string excelfile,      string propertyname) {     excel::application excel = null;     workbook workbook = null;     worksheet firstworksheet = null;     object value = null;      try     {         excel = new excel::application();          workbook = excel.workbooks.open(excelfile);          firstworksheet = workbook.worksheets[1] worksheet;          value = firstworksheet.customproperties[(object)propertyname].value;     }     catch (exception ex)     {         console.writeline($"\nerror in {nameof(readfromexcelcustomdocumentproperties)}:");         console.writeline($"{excelfile}!{firstworksheet.name}");         console.writeline($"{ex.message}\n");     }         {         if (workbook != null)             workbook.close();          if (excel != null)             excel.quit();     }      return value; } 

gives me following error:

enter image description here

below dump of exception class object.

{"type mismatch. (exception hresult: 0x80020005 (disp_e_typemismatch))"}     data: {system.collections.listdictionaryinternal}     errorcode: -2147352571     hresult: -2147352571     helplink: null     ipforwatsonbuckets: 0x7177fe49     innerexception: null     istransient: false     message: "type mismatch. (exception hresult: 0x80020005 (disp_e_typemismatch))"     remotestacktrace: null     source: ""     stacktrace: "   @ system.runtimetype.forwardcalltoinvokemember(string membername, bindingflags flags, object target, int32[] awrappertypes, messagedata& msgdata)\r\n   @ microsoft.office.interop.excel.customproperties.get__default(object index)\r\n   @ customdocumentproperties.program.readfromexcelcustomdocumentproperties(string excelfile, string propertyname) in c:\\sathyaish\\dotnet\\customdocumentproperties\\customdocumentproperties\\program.cs:line 131"     targetsite: {system.object forwardcalltoinvokemember(system.string, system.reflection.bindingflags, system.object, int32[], system.runtime.remoting.proxies.messagedata byref)}     watsonbuckets: null     _hresult: -2147352571     _classname: null     _data: {system.collections.listdictionaryinternal}     _dynamicmethods: null     _exceptionmethod: {system.object forwardcalltoinvokemember(system.string, system.reflection.bindingflags, system.object, int32[], system.runtime.remoting.proxies.messagedata byref)}     _exceptionmethodstring: null     _helpurl: null     _innerexception: null     _ipforwatsonbuckets: 0x7177fe49     _message: "type mismatch. (exception hresult: 0x80020005 (disp_e_typemismatch))"     _remotestackindex: 0     _remotestacktracestring: null     _safeserializationmanager: {system.runtime.serialization.safeserializationmanager}     _source: ""     _stacktrace: {sbyte[96]}     _stacktracestring: null     _watsonbuckets: null     _xcode: -532462766     _xptrs: 0x00000000 

it appears this answer above observed behavior may attributed bug in interop assemblies.

however, this answer seems suggest poster has been able run code successfully.

are able run code successfully? have seen error , know fix?

it's possible don't know mean custom properties, if referring properties file->info section in excel:

enter image description here

like come sharepoint, access them using contenttypeproperties collection of workbook object.

here example of how access them in image above:

// excel.workbook wb;  string dmdregion = wb.contenttypeproperties["demand region"].value.tostring(); wb.contenttypeproperties["demand region"].value = "emea"; 

your example showed worksheet object, again, may have totally missed boat.


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -