DrM*_*use 9 excel vba conditional-compilation excel-vba
尝试加载Excel工作簿时发现奇怪的行为.
我有一个Excel-AddIn,用.NET Interop编写.它主要用于创建我自己的Ribbon-Tab,从菜单中加载工作簿并进行一些项目管理.
当我尝试使用两种方式打开工作簿时,我会得到不同的结果:
首先,当我从Addin中加载工作簿(Excel 2003-版本)时,一切正常.从功能区的Button-Event中,openWorkbook调用application.workbooks.open(...)加载项的公共函数,用于加载Excel工作簿.
这样,工作簿打开时没有错误.
其次,当我尝试使用以下代码从VBA中调用Addin-Function时:
Set addIn = Application.COMAddIns("WMExcelAddin1")
Set automationObject = addIn.Object
automationObject.openWorkbook (filename)
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息:
编译错误
自动化错误
并且IDE在其中一个工作簿模块中第一次出现条件编译时停止,如下所示:
#const ebind = 0
[...]
sub proc1()
#if ebind = 1 then ' IDE Stops here
[...]
#else
[...]
#end if
end sub
Run Code Online (Sandbox Code Playgroud)
我试图使用布尔数据类型而不是具有相同效果的数字.
我的智慧结束了.
在自动化模式下,Excel 默认情况下不加载加载项。Excel 使用编译加载项时所用的条件加载加载项。为了使加载项在自动化模式下工作,应强制 Excel 在加载依赖于该加载项的任何工作簿之前加载它。下面我提供了来自我的真实项目(有一些版本)的代码示例,它在 JScript 中实现了这个加载序列。评论解释步骤。
function run_excel() {
dbg_log("run_excel");
g_xla_addin = null;
g_xla = null;
try {
g_add_ins = get_excel_app().AddIns;
dbg_log("finding add_in.xlam");
//Searching for the installed add-in like Application.COMAddIns("WMExcelAddin1")
g_xla_addin = find_addin(g_add_ins, "add_in.xlam");
} catch(v_err) {
throw new error(
"xla_loading"
, CR_xla_loading
, 'Unexpected error occurred while determining if add_in.xlam is installed.'
, v_err
);
}
if (g_xla_addin == null) throw new error(
"xla_loading"
, CR_xla_not_installed
, "MS Excel addin is not installed."
);
try {
dbg_log("opening add_in.xlam");
//In the example, the add-in has the name of its workbook
try { g_xla = g_excel.Workbooks(g_xla_addin.Name); } catch(e) {}
if (g_xla == null) {
g_excel.AutomationSecurity = 1; // 1 == msoAutomationSecurityLow
//Loading the add-in. The add-in also handles `OpenWorkbook` at this time.
g_xla = g_excel.Workbooks.Open(
g_xla_addin.FullName //FileName
, 2 //UpdateLinks
, true //ReadOnly
, null //Format
, null //Password
, null //WriteResPassword
, true //IgnoreReadOnlyRecommended: not display the read-only recommended message
, 2 //Origin: xlWindows
, null //Delimiter
, null //Editable
, null //Notify
, null //Converter
, false //AddToMru: don't add this workbook to the list of recently used files
, true //Local: saves files against the language of Microsoft Excel.
, 0 //CorruptLoad: xlNormalLoad
);
hide_excel(); //To speed up and not interfere with user actions
}
} catch(v_err) {
throw new error(
"xla_loading"
, CR_xla_loading
, 'Unexpected error occurred while loading add_in.xlam:\n'
, v_err
);
}
//Now the Add-In is loaded, so the VBA engine knows its API, and the workbook referencing to it are loaded fine.
try {
g_sig_cat_wbk = g_excel.Workbooks.Open(
g_sig_cat_fn //FileName
, 2 //UpdateLinks
, true //ReadOnly
, null //Format
, null //Password
, null //WriteResPassword
, true //IgnoreReadOnlyRecommended: not display the read-only recommended message
, 2 //Origin: xlWindows
, null //Delimiter
, null //Editable
, null //Notify
, null //Converter
, false //AddToMru: don't add this workbook to the list of recently used files
, false //Local: saves files against the language of Microsoft Excel.
, 0 //CorruptLoad: xlNormalLoad
);
//Calling on the loaded workbook the target macro from the loaded add_in.xlam
vba_ret(g_excel.Run(g_xla_addin.Name+"!my_addin.prepare_sig_import", g_sig_cat_wbk.Name));
} catch(v_err) {
throw new error(
"sig_cat_loading"
, CR_sig_cat_loading
, 'Error occured while loading catalog of signals:\n'
, v_err
);
}
finally {
g_sig_cat_wbk.Close(false);
g_sig_cat_wbk = null;
}
dbg_log("run_excel done");
}
Run Code Online (Sandbox Code Playgroud)