C# Excel VBA make module names not depended on the language -
excelfile.vbproject.vbcomponents.add(vbext_componenttype.vbext_ct_stdmodule);
this code in english version of office creates module named: "module1". if office language different "module1" in language. need know how module called in code.
var standardmodule = excelfile.vbproject.vbcomponents.item("thisworkbook");
the same problem here in english version of office "thisworkbook" exits, in language called differently.
it's possible make code language independent?
the first 1 easy - vbcomponents.add
returns vbcomponent
. can inspect .name
property:
var module = excelfile.vbproject.vbcomponents.add(vbext_componenttype.vbext_ct_stdmodule); debug.writeline(module.name);
the second 1 bit trickier. you'll need loop through of vbcomponents , test 2 things unique workbook object. have .type
of vbext_ct_document
, 134 properties in .properties
collection default:
vbcomponent thisworkbook; foreach (var module in excelfile.vbproject.vbcomponents) { var test = module vbcomponent; if (test.type == vbext_componenttype.vbext_ct_document && test.properties.count == 134) { thisworkbook = test; debug.writeline(thisworkbook.name); break; } }
edit: linq solution looks this, it's possible leave dangling interop references way. if want try it, can't hurt - first place i'd if excel doesn't shut down properly:
var thisworkbook = (excelfile.vbproject.vbcomponents).cast<vbcomponent>() .first(x => x.type == vbext_componenttype.vbext_ct_document && x.properties.count == 134);
edit2: pointed out @mat'smug in comments, property count specific version - value above specific excel 2013. new workbook, thisworkbook module 1 highest property count. should work on version:
vbcomponent thisworkbook = null; foreach (var component in excelfile.vbproject.vbcomponents.cast<vbcomponent>()) { if (thisworkbook == null || component.properties.count > thisworkbook.properties.count) { thisworkbook = component; } } debug.writeline(thisworkbook.name);
linq:
var thisworkbook = excelfile.vbproject.vbcomponents.cast<vbcomponent>() .aggregate((p, x) => (p.properties.count > x.properties.count ? p : x));
Comments
Post a Comment