Main Content

Distribute Add-Ins and Integrate intoMicrosoftExcel

To create aMicrosoft®Excel®add-in, seeCreate Excel Add-In from MATLAB.

After you have created your Excel add-in, follow these steps to deploy the add-in on another machine.

  1. Verify that you have received all the files necessary for deployment.

  2. InstallMATLAB®Runtime.

  3. Register your add-in DLL using the generated installer, the_install.batinstallation script, ormwregsvr.

  4. Register the utility librariesmwcomutil.dllandmwcommgr.dllusingmwregsvr.

  5. Install the add-in in Excel.

  6. Call the add-in functions directly in your worksheet.

  7. (Optional) Create macros using the Function Wizard or custom VBA code.

Files forExcelAdd-In Deployment

If you create your Excel add-in using the Library Compiler or create an installer usingcompiler.package.installer, distribute the generated installer to your end user. The installer installs the Excel add-in andMATLAB Runtimeand registers the add-in DLL on the target machine. The Library Compiler also generates an installation script named_install.batthat registers the add-in DLL. To register the DLL without using the installer or script, seeRegister Add-Ins and COM Componentsbelow.

If you do not create an installer, distribute the files that the Library Compiler generates in thefor_redistribution_files_onlyfolder. For a list of files generated in each folder, seeFiles Generated After Packaging MATLAB Functions.

InstallMATLABRuntime

MATLAB Runtimecontains the libraries needed to use compiled MATLAB code on a target system without a licensed copy of MATLAB. For instructions, seeInstall and Configure MATLAB Runtime.

TheMATLAB Runtimeinstaller for Windows®automatically sets the system path and registers theMATLAB RuntimeDLLs required for Excel add-ins. To perform these steps manually, seeRun Applications Using a Network Installation of MATLAB Runtime.

Register Add-Ins and COM Components

Note

COM components are used in bothMATLAB Compiler™andMATLAB Compiler SDK™, therefore some of the instructions relating to building and packaging COM components and add-ins can be shared between products.

When you create your COM component, it is registered in eitherHKEY_LOCAL_MACHINEorHKEY_CURRENT_USER, based on your log-in privileges.

If you find you need to change your run-time permissions due to security standards imposed by Microsoft or your installation, you can do one of the following before deploying your COM component or add-in:

  • Log on asadministratorbefore running your COM component or add-in

  • Run the followingmwregsvrcommand prior to running your COM component or add-in, as follows:

    mwregsvr [/u] [/s] [/useronly]project_name.dll
    where:

    • /uallows any user to unregister a COM component or add-in for this server

    • /sruns this command silently, generating no messages. This is helpful for use in silent installations.

    • /useronlyallows only the currently logged-in user to run the COM component or add-in on this server

Caution

If your COM component is registered in theUSERhive, it will not be visible to Windows Vista™ orWindows 7users running asadministratoron systems with UAC (User Access Control) enabled.

If you register a component to theUSERhive underWindows 7or Windows Vista, your COM component may fail to load when running with elevated (administrator) privileges.

如果发生这种情况,做以下重新注册the component to theLOCAL MACHINEhive:

  1. Unregister the component with this command:

    mwregsvr /u /useronlymy_dll.dll

  2. Reregister the component to theLOCAL MACHINEhive with this command:

    mwregsvrmy_dll.dll

RegisterMATLABUtility Libraries

To handle data conversion, the VBA code that you generate withMATLAB Compilerreferences themwcomutil.dllandmwcommgr.dllutility libraries. To register these libraries:

  1. Open a system command prompt

  2. Navigate tomatlabroot\bin\win64, wherematlabrootrepresents the location of MATLAB orMATLAB Runtimethat corresponds to the MATLAB release that you used to compile the Excel add-in.

  3. Run the following commands:

    mwregsvr mwcomutil.dll
    mwregsvr mwcommgr.dll

InstallExcelAdd-Ins

Install the add-in inMicrosoft Excelby following the steps for your Excel version.

Excel2010 or Newer

  1. In Excel, click theFiletab.

  2. On the left navigation pane, selectOptions.

  3. In the Excel Options dialog box, on the left navigation pane, selectAdd-ins.

  4. In the Manage drop-down list, selectExcel Add-ins, and clickGo.

  5. In the Add-ins dialog box, clickBrowse.

  6. Browse to and select your add-in. ClickOK.

  7. In the Excel Add-ins dialog box, verify that the entry containing your add-in is selected. ClickOK.

Excel2007

  1. StartMicrosoft Excel.

  2. Click theOfficebutton () and selectExcel Options.

  3. In the left pane of the Excel Options dialog box, clickAdd-ins.

  4. In the right pane of the Excel Options dialog box, selectExcel Add-insfrom theManagedrop-down list.

  5. ClickGo.

  6. ClickBrowse.

  7. Browse to and select your add-in. ClickOK.

  8. In the Excel Add-ins dialog box, verify that the entry containing your add-in is selected. ClickOK.

UseMATLABFunctions inExcelSpreadsheet

After you have installed the add-in in Excel, invoke the add-in function directly with a method call in the form of an Excel custom function.

可选地,使用Excel宏指定输入一个d output cells, execute multiple functions at a time, or handle multiple outputs. Create a macro using the Function Wizard add-in provided withMATLAB Compiler. For additional functionality, create or modify macros using custom VBA code.

Call Method Directly in Spreadsheet

To run the MATLAB function in the Excel spreadsheet, invoke the function with a method call in the target output cells. For example, if you deployed MATLAB code calledmymagic.mor a figure calledmymagic.fig, invoke that code by entering the following function in a cell in the spreadsheet:

=mymagic()

If your function returns an array, select a range of cells to match the array size.

Tip

If the method call does not evaluate immediately, pressCtrl,Shift, andEntersimultaneously.

CreateExcelMacros Using Function Wizard

The Function Wizard provides a control panel for creating macros using one or more compiled MATLAB functions. Use the Function Wizard to control the placement of MATLAB input and output data in Excel worksheets.

For more details, seeInstall and Use Function Wizard.

CreateExcelMacros Using VBA

Use VBA code to create Excel macros with more functionality than the Function Wizard allows. You can use variables, loops, forms, controls, and other programming techniques to create custom Excel programs.

For an example on using VBA code to create a custom graphical Excel application, seeImplement User Interface Using Visual Basic Form Controls.

Related Topics