Hello all!

I have a code in R which basically opens, saves and closes all excel sheets
in a given directory. The code is following:

----------------------------------------- R code start
------------------------------------------
normal_update <- function() {

# Updating files in main directory

  Dir <- choose.dir()
  setwd(Dir)

  library(RDCOMClient)
  library(SWinTypeLibs)

  ex = COMCreate("Excel.Application")

  Files <- list.files()[grep(".xls",list.files())]
    n <- length(Files)

# Choose an add in to load into excel
  add_in <- file.choose()

  for(i in 1:n) {

#    ex = COMCreate("Excel.Application")
      addin = ex$Workbooks()$Open(add_in)
      book = ex$Workbooks()$Open(paste(c(getwd(), "/", Files[i]), collapse =
""))
#    shell(paste("ECHO Y | DEL", '"C:\\Dokumente und
Einstellungen\\Praktikant1\\Eigene Dateien\\RESUME.XLW"'))

    ex$Save()
    book$Close()

    rm(list = c("book", "addin"))

  }

}
 ----------------------------------------- R code end
------------------------------------------

The algorithm is:
- the first setwd sets the desired directory (in my case I need only the
above one)
- create an Excel instance
- get all the files with the ending .xls*
- start loop:
-- load the addin into excel
-- open the i-th excel file
-- save excel
-- close the excel book (not the whole excel, just the book which is open)
-- clean up the files

The problem: After the first iteration of the for-loop, when the code tries
to save and close the file I get the error message

Browse[1]> i
debug: 2
...
debug: ex$Save()
Browse[1]>
Fehler in .COM(x, name, ...) :
  Cannot locate 0 name(s) Save in COM object (status = -2147418111)

What is the reason for this problem and how can I bypass it?

Greetings,
David Seres

        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to