自动化错误与条件编译

当试图加载Excel工作簿时,我发现奇怪的行为。

我有一个用COM Interop编写的Excel-AddIn。 它主要用于创build我自己的Ribbon-Tab,从菜单中加载工作簿并执行一些项目pipe理。

当我尝试使用两种方法打开工作簿时,会得到不同的结果:

首先,当我从加载中加载工作簿(Excel 2003版本)时,一切正常。 从function区的Button-Event中调用openWorkbook项的公共函数openWorkbook ,该函数使用application.workbooks.open(...)加载Excel工作簿。

这样,工作簿将打开而不会出现错误。

其次,当我尝试使用如下代码从VBA中调用Addin-Function:

 Set addIn = Application.COMAddIns("WMExcelAddin1") Set automationObject = addIn.Object automationObject.openWorkbook (filename) 

我收到一条错误消息:

编译错误

自动化错误

并且IDE在其中一个工作簿模块中首次出现有条件编译时停止,如下所示:

 #const ebind = 0 [...] sub proc1() #if ebind = 1 then ' IDE Stops here [...] #else [...] #end if end sub 

我试图使用布尔数据types而不是数字具有相同的效果。

我在我的智慧结束。

在自动化模式下, 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"); } 
Interesting Posts