【问题标题】:Run VBA script from R从 R 运行 VBA 脚本
【发布时间】:2013-10-24 14:49:45
【问题描述】:

我必须管理涉及 R 脚本和 VBA-code 的工作流程。 我想在 R(我的大部分代码所在的位置)中运行该过程,然后不时调用 VBA-code 进行具体计算。

我会在 R 中为 VBA 准备输入,在某处写入结果(.csv、数据库),然后在 R 脚本的其余部分中使用结果。

最好的办法当然是将整个代码移到 R 中,但目前还不可能。 VBA-code 相当复杂。将其翻译成 R 语言将是一项具有挑战性的长期任务。

有没有可能在 R 中管理这样的工作流程?

【问题讨论】:

  • VBA 代码作为 Excel 宏还是 VBScript?
  • 嗨马雷克。为了让事情变得复杂,我当然有:-)

标签: r vba workflow


【解决方案1】:

这是一种不需要 VBscript 包装器的方法。你需要安装RDCOMClient 包

library(RDCOMClient)

# Open a specific workbook in Excel:
xlApp <- COMCreate("Excel.Application")
xlWbk <- xlApp$Workbooks()$Open("C:\\Temp\\macro_template.xlsm")

# this line of code might be necessary if you want to see your spreadsheet:
xlApp[['Visible']] <- TRUE 

# Run the macro called "MyMacro":
xlApp$Run("MyMacro")

# Close the workbook and quit the app:
xlWbk$Close(FALSE)
xlApp$Quit()

# Release resources:
rm(xlWbk, xlApp)
gc()

【讨论】:

  • 信用到期:here is the related question 这激发了我的回答。
  • 在我的例子中,xlWbk &lt;- xlApp$Workbooks()$Open("C:\\Temp\\macro_template.xlsm") 行产生错误Error in .COM(x, name, ...) : Cannot locate 0 name(s) Workbooks in COM object (status = -2147418111)
  • @user3507584 在您的C:\Temp 文件夹中是否有一个名为macro_template.xlsm 的文件?
  • 我正在使用我自己的文件的路径。它现在正在工作(也许其他应用程序正在调用它)。
  • @lebelinoz 如何将参数传递给宏(从 excel 调用时)?
【解决方案2】:
  1. 编写调用 VBA 的 VBscript 包装器。见Way to run Excel macros from command line or batch file?

  2. 通过 R 的 system 或 shell 函数运行 VBscript。

【讨论】:

  • 可能会很方便:shell(shQuote(normalizePath(path_to_vbs_file)), "cscript", flag = "//nologo").
  • Here's a way 从 R 运行 VBA,您可以在 R 中指定输入和参数,而不是调整 VBScript。
  • 谢谢,我使用这种方法整理了一个完整的示例(以及具有最佳实践的相关博客)。见这里:github.com/brshallo/macro-shell-example。随意克隆,可以看到启用宏的 Excel 电子表格、包装器 VBScript 和 R 脚本,它们通过 shell 命令等启动一切。
【解决方案3】:

我使用 RDCOM 运行 Excel 宏数月来从 SAP 中提取数据,今天尝试退出程序时它开始抛出错误。不知道为什么。这是我的解决方案,如果无法实现和平退出,则终止任务。

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~ Define hard coded variables
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Define paths to VBA workbooks and macro name
path_xlsb <- "I:/EXAMPLE_WORKBOOK.xlsb"
xlsb_macro_name <- "launch_SAP"

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~ Load or install packages
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# librarian
if(require(librarian) == FALSE){
  install.packages("librarian")
  if(require(librarian)== FALSE){stop("Unable to install and load librarian")}
}
librarian::shelf(tidyverse, readxl, RODBC, odbc,lubridate, pivottabler, xlsx, openxlsx, htmlTable)

# Load or install RDCOM Client 
if(require(RDCOMClient) == FALSE){
  install.packages("RDCOMClient", repos = "http://www.omegahat.net/R")
  if(require(RDCOMClient)== FALSE){stop("Unable to install and load RDCOMClient")}
}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#~~ Run VBA Macro in Excel 
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

# Kill any existing Excel processes
try(system("Taskkill /IM Excel.exe /F"),silent = TRUE)

# Kill any existing SAP processes (only relevant if you're working with SAP)
try(system("Taskkill /IM saplogon.EXE /F"),silent = TRUE)

# Open a specific workbook in Excel:
xlApp <- COMCreate("Excel.Application")
xlWbk <- xlApp$Workbooks()$Open(path_xlsb_reconnect)

# Set to true if you want to see your spreadsheet:
xlApp[['Visible']] <- TRUE 

# Run the macro 
Sys.sleep(2) # Wait for the workbook to load
xlApp$Run(xlsb_macro_name)

# Attempt to close the workbook peacefully  
Sys.sleep(2) # Wait for 2 seconds
try(xlApp$Quit())
try(rm(xlWbk, xlApp))
try(gc())

# Kill any Excel processes
try(system("Taskkill /IM Excel.exe /F"),silent = TRUE)

# Kill any existing SAP processes (only relevant if you're working with SAP)
try(system("Taskkill /IM saplogon.EXE /F"),silent = TRUE)

【讨论】:

  • 您好,我了解您的代码,但无法为自己工作。您是否通过 Analysis 插件从 SAP 获取数据?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2013-11-22
相关资源
最近更新 更多