MATLAB から COM Automation Server として起動されたEx​celにてアドインの​マクロを使用すること​はできますか?

13 Ansichten (letzte 30 Tage)
MathWorks Support Team
MathWorks Support Team am 17 Aug. 2017
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

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 22 Feb. 2021
Bearbeitet: MathWorks Support Team am 22 Feb. 2021
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);

Weitere Antworten (0)

Kategorien

Mehr zu MATLAB の COM オブジェクト finden Sie in Help Center und File Exchange

Produkte


Version

R2007a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!