Run Macro in Excel

Runs a macro 🡥 defined in an enabled-macro .xlsm Excel file.

Command availability: IBM RPA SaaS and IBM RPA on premises

Script syntax

IBM RPA's proprietary script language has a syntax similar to other programming languages. The script syntax defines the command's syntax in the script file. You can work with this syntax in IBM RPA Studio's Script mode.

excelMacro --macro(String)

Dependencies

  • Microsoft™ Office 2010 or higher is required.
  • Ensure that the architecture (bitness) of your Office apps, IBM RPA, and your operating system match. For example, if you are using a 64-bit operating system, IBM RPA and Office must be 64-bit too.
  • The Excel application must be open. Otherwise, the command won't work.

To use this command, you must enable macros in Excel. The Following procedure shows you how to enable macros:

  1. Open Excel.
  2. Click the File tab.
  3. Click Options.
  4. Click Trust Center.
  5. Click the Trust Center Settings button.
  6. Go to Macro Settings and select Enable VBA macros (not recommended; potentially dangerous code can run).
  7. Click Ok and close the application.
Important:
  • Enabling this option can make your machine vulnerable to malicious code. Ensure that you only run safe macros with IBM RPA.
  • Ensure that the VBA project that contains the macro is not password encrypted.

Input parameters

The following table displays the list of input parameters available in this command. In the table, you can see the parameter name when working in IBM RPA Studio's Script mode and its Designer mode equivalent label.

Designer mode label Script mode name Required Accepted variable types Description
Macro macro Required Text Macro to run in the Excel file.

Example

Example 1: The Open Office File (officeOpen) command opens an Excel Office application, stored in the variable "officeApplication". The Run Macro in Excel command runs a macro in this application, making the sum of the values ​​"1 + 2" and saving the result of the sum in cell A1.

defVar --name example --type Excel
defVar --name ID --type Numeric
defVar --name officeApplication --type OfficeApplication
// Download the following file to run the command.
officeOpen --path "Samplefile(excelMacroFile.xlsm)" --type "Excel" --keepvisible officeApplication=officeapplication ID=processid
excelMacro --macro "Range(\"A1\").Value = 1+2"
delay --timeout 00:00:15
officeClose --officeapplication ${officeApplication}
// When checking the open Excel file, the following result is obtained:
// |A1|B1|
// |3 |2 |.

Example 2: The following example is similar to the Example 1, the main change being using the Launch and Attach Window (attachWindow) to open the Excel application, to reiterate that, in order for the command to work, it must be open and in focus.

defVar --name excelFile --type String
defVar --name excelWindow --type Window
defVar --name example --type Excel
defVar --name ID --type Numeric
defVar --name officeApplication --type OfficeApplication
launchWindow --executablepath "${excelFile}" --safesearch  excelWindow=value
excelMacro --macro "Range("B3").Value = Range("A1").Value"
delay --timeout "00:00:15"
officeClose --officeapplication ${officeApplication}

Limitations

  • The command works only on .xlsm files.
  • The command cannot run Macros that are inside a password-encrypted VBA project.