使用RDCOMClient时路径错误

时间:2018-07-23 14:02:42

标签: r rdcomclient

在将其保存在R中后,我试图使用RDCOMClient在Excel文件上运行宏。

我正在使用的功能是:

runMacro <- function(xlsmFilePath, macroName) 
  {

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


  ## make the spreadsheet visible
  xlApp[['Visible']] = TRUE 

  xlApp[["DisplayAlerts"]] <- FALSE

  print(xlsmFilePath)
  xlWbk <- xlApp$Workbooks()$Open(xlsmFilePath)


  xlSheet <- xlWbk$Worksheets(3)$Activate()


  # this line of code might be necessary if you want to see your spreadsheet:

  Sys.sleep(5)
  # Run the macro called "MyMacro":
  #xlApp$Run("ThisWorkbook.Formatting")

  xlApp$Run(macroName)

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


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

  }

但是,执行此命令时,我收到了一条错误的错误路径错误消息:xlWbk <- xlApp$Workbooks()$Open(xlsmFilePath)

我得到的错误如下:

<checkErrorInfo> 80020009
Warnung: Error in .COM: Microsoft Excel cannot access the file 'C://Users/Documents/test.xlsm. There are several possible reasons:

 The file name or path does not exist.
 The file is being used by another program.
 The workbook you are trying to save has the same name as a currently open workb
ook. (Microsoft Excel)
  [No stack trace available]

但是,奇怪的是,打印命令(导致错误的代码行)显示了另一条路径(即正确的路径):C:/Users/Documents/test.xlsm

P.S:请注意,即使始终在运行相同的代码,有时也会发生此问题,有时不会发生。

有人知道为什么会发生这种问题吗?

0 个答案:

没有答案