Excelブックを読み込もうとすると、奇妙な動作をします。
COM相互運用機能を備えた.NETで記述されたExcel-AddInがあります。 主に、独自のリボンタブを作成し、メニューからワークブックをロードし、プロジェクト管理を行うために使用されます。
2つの方法を使用してブックを開こうとすると、異なる結果が得られます。
まず、アドイン内からワークブック(Excel 2003-Version)を読み込むと、すべて正常に機能します。リボンのボタンイベントから、パブリック関数 openWorkbook
アドインの application.workbooks.open(...)
Excelブックをロードします。
これにより、ワークブックがエラーなしで開きます。
次に、次のようなコードを使用してVBA内からアドイン関数を呼び出そうとすると、
Set addIn = Application.COMAddIns("WMExcelAddin1")
Set automationObject = addIn.Object
automationObject.openWorkbook (filename)
エラーメッセージが表示されます。
コンパイルエラー
自動化エラー
IDEは、ワークブックモジュールの1つで条件付きコンパイルが最初に発生したときに停止し、次のようになります。
#const ebind = 0
[...]
sub proc1()
#if ebind = 1 then " IDE Stops here
[...]
#else
[...]
#end if
end sub
同じ効果を持つ数値の代わりにブールデータ型を使用しようとしました。
私は「気の利いた」終わりです。
回答:
回答№1は1自動化モード 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");
}