运行 Excel 中的宏

运行在已启用宏 .xlsm Excel 文件中定义的 宏 🡥

命令可用性: 本地 IBM RPA SaaS 和 IBM RPA

脚本语法

IBM RPA 的专有脚本语言的语法与其他编程语言类似。 该脚本语法在脚本文件中定义命令的语法。 您可以在 IBM RPA Studio的 脚本 方式下使用此语法。

excelMacro --macro(String)

依赖关系

  • Microsoft™ Office 2010 或更高版本是必需的。
  • 确保 Office 应用程序, IBM RPA和操作系统的体系结构 (位) 匹配。 例如,如果使用的是 64 位操作系统,那么 IBM RPA 和 Office 也必须是 64 位。
  • 必须打开 Excel 应用程序。 否则,该命令将不起作用。

要使用此命令,必须在 Excel 中启用宏。 以下过程显示了如何启用宏:

  1. 打开 Excel。
  2. 单击 文件 选项卡。
  3. 单击 选项
  4. 单击 信任中心
  5. 单击 信任中心设置 按钮。
  6. 转至 宏设置 并选择 启用 VBA 宏 (不推荐; 可能运行危险的代码)
  7. 单击 好的 并关闭应用程序。
重要信息:
  • 启用此选项可能会使您的机器易受恶意代码攻击。 确保仅使用 IBM RPA运行安全宏。
  • 确保包含宏的 VBA 项目未加密密码。

输入参数

下表显示了此命令中提供的输入参数的列表。 在表中,您可以看到在 IBM RPA Studio的脚本方式及其 Designer 方式等效标签中工作时的参数名称。

设计器方式标签 脚本方式名称 必需的 接受的变量类型 描述
macro Required Text 要在 Excel 文件中运行的宏。

示例

例 1: " 打开 Office 文件 " (officeOpen) 命令将打开一个 Excel Office 应用程序,该程序存储在变量 "officeApplication" 中。 在 Excel 中运行宏 命令在此应用程序中运行宏,使值的总和为 "1 + 2" ,并将总和的结果保存在单元格 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 |.

示例 2: 以下示例类似于 示例 1,主要更改是使用 启动并附加窗口 (attachWindow) 来打开 Excel 应用程序,以重申为了使命令工作,必须打开该命令并将其作为焦点。

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}

限制

  • 该命令仅对 .xlsm 文件起作用。
  • 该命令无法运行密码加密的 VBA 项目中的宏。