2014-10-07 38 views
9

当我尝试加载Excel工作簿时,发现奇怪的行为。有条件编译的自动化错误

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

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

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

这样,工作簿就会打开而不会出错。

其次,当我尝试使用类似的代码从内部VBA调用加载项,功能:

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 

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

我在我的智慧'结束。

+0

你是如何将你的类和方法暴露给VBA的? [这样的东西?](http://davecra.com/2013/02/01/how-to-expose-methods-in-your-vsto-add-in/) – 2014-10-07 08:45:12

+0

[vba4all](http:// stackoverflow .com/users/2140173/vba4all),是的 - 正如您的链接中所述。 (尽管在VB.NET \t中,而不是在C#中)。 – DrMarbuse 2014-10-07 08:50:27

+1

你有没有在vba项目中设置你的插件的参考? – ZAT 2014-10-07 09:00:40

回答

1

在自动化模式下Excel does not load add-ins by default。 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"); 
}