MATLAB から COM Automation Server として起動されたExcelにてアドインのマクロを使用することはできますか?
13 ビュー (過去 30 日間)
古いコメントを表示
MathWorks Support Team
2017 年 8 月 17 日
編集済み: MathWorks Support Team
2021 年 2 月 22 日
MATLAB から COM Automation Server としてExcel を起動しました。つぎにこのExcelから自分のExcel アドインのマクロを使用させようとするとエラーとなります。
コード:
Excel = actxserver('Excel.Application');
Workbooks = get(Excel,'Workbooks');
fWorkbook = invoke(Workbooks,'Add');
fWorksheet = get(fWorkbook,'Activesheet');
fRange = fWorksheet.Range('A1:B2');
fRange.Value{1,1} = 'name';
fRange.Value{2,1} = 'foo';
fRange.Value{1,2} = 'amount';
fRange.Value{2,2} = 100;
Excel.Run('myMacro');
エラー:
ERROR: % ??? Invoke Error, Dispatch Exception:
% Source: Microsoft Office Excel
% Description: The macro 'myMacro' cannot be found.
% Help File: D:\Applications\MSOffice\OFFICE11\1033\xlmain11.chm
% Help Context ID: 0
採用された回答
MathWorks Support Team
2021 年 2 月 22 日
編集済み: MathWorks Support Team
2021 年 2 月 22 日
MATLABから ActiveX を使用して Excel を起動することは可能ですが、この場合は Excel からアドインを使用することは出来ません。 これは、COM Automation Serverとして起動された Excel はアドインのロードを行わないためです。
マイクロソフト社の情報:
代わりの方法としては、.xlaファイルをロードすることによりExcel上でアドインを使用可能にする方法があります。
サンプルコード:
%%
Excel = actxserver('Excel.Application');
Workbooks = get(Excel,'Workbooks');
fWorkbook = invoke(Workbooks,'Add');
fWorksheet = get(fWorkbook,'Activesheet');
fRange = fWorksheet.Range('A1:B2');
fRange.Value{1,1} = 'name';
fRange.Value{2,1} = 'foo';
fRange.Value{1,2} = 'amount';
fRange.Value{2,2} = 100;
%% Now load the add-in, using the method suggested by Microsoft:
% https://mskb.pkisolutions.com/kb/213489
% Open file on disk
Excel.Workbooks.Open('D:\Work\myAddin.xla');
% Use this method to execute any Auto_Run macros.
Excel.Workbooks.Item('myAddin.xla').RunAutoMacros(1);
% Now that the Add-in is loaded, execute the macro it contains:
Excel.Run('myMacro');
%% Finally, make Excel visible.
set(Excel,'Vis',1);
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で MATLAB の COM オブジェクト についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!