条件编译的自动化错误

时间:2014-10-07 08:31:06

标签: excel vba excel-vba conditional-compilation

尝试加载Excel工作簿时发现了奇怪的行为。

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

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

首先,当我从Addin中加载工作簿(Excel 2003-Version)时,一切正常。在功能区的Button-Event中,调用加载项的公共函数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

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

我的智慧结束了。

1 个答案:

答案 0 :(得分: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");
}