CLOSE all USE koko local lnMax lnMax = 18 local array laXLSFiles[lnMax] FOR i=1 TO lnMax COPY FOR numbers=i TO ("sheet" + TRANSFORM(i)) TYPE XL5 laXLSFiles[m.i] = sys(5) + curdir() + 'sheet' + transform(m.i) + 'xls' ENDFOR =CombineExcelFiles(laXLSFiles, 'c:\dept\amman.xls') function CombineExcelFiles (taXLSFiles, tcDestination, tlDeleteOriginal) external array taXLSFiles local loExcel as Excel.application, ; loWorkBook as Excel.Worksbook, ; loWorkSheet , ; lnCounter, lcWorkSheetCaption, lcError, ; lcValidChars lcError = "" try lcValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 " loExcel = newobject("Excel.Application") with loExcel .ScreenUpdating = .f. .DisplayAlerts = .f. .WorkBooks.add() lnCounter = 0 ** Delete all existing worksheets except 1 for each loWorkSheet in .WorkBooks(1).WorkSheets lnCounter = m.lnCounter + 1 if m.lnCounter > 1 loWorkSheet.delete endif endfor for lnCounter = 1 to alen(taXLSFiles,1) if file(taXLSFiles[m.lnCounter]) lcWorkSheetCaption = juststem(taXLSFiles[m.lnCounter]) loWorkBook = .WorkBooks.open(taXLSFiles[m.lnCounter]) loWorkBook.WorkSheets(1).copy(null, ; .WorkBooks(1).WorkSheets(.WorkBooks(1).WorkSheets.count)) .WorkBooks(1).ActiveSheet.name = ; right(alltrim(chrtran(m.lcWorkSheetCaption, ; chrtran(m.lcWorkSheetCaption,m.lcValidChars,"")," ")), 31) &&loWorkBook.Name loWorkBook.close(.f.) && Don't save changes if m.tlDeleteOriginal erase (taXLSFiles[m.lnCounter]) endif endif endfor ** Remove the first original sheet from (Sheet1) .WorkBooks(1).WorkSheets(1).delete .WorkBooks(1).saveas(m.tcDestination) .ScreenUpdating = .t. .DisplayAlerts = .t. endwith catch to loError lcError = Log_Error(m.loError) finally if vartype(m.loExcel) = 'O' with loExcel .ScreenUpdating = .t. .DisplayAlerts = .t. .quit() endwith endif endtry return m.lcError endfunc>Try
>local lnMax >lnMax = 18 >local array laXLSFiles[lnMax] > >FOR i=1 TO lnMax > COPY FOR numbers=i TO ("sheet" + TRANSFORM(i)) TYPE XL5 > laXLSFiles[m.i] = sys(5) + curdir() + 'sheet' + transform(m.i) + 'xls' >ENDFOR > >=CombineExcelFiles(laXLSFiles, 'c:\dept\amman.xls') >>
>>function CombineExcelFiles (taXLSFiles, tcDestination, tlDeleteOriginal) >>external array taXLSFiles >>local loExcel as Excel.application, ; >> loWorkBook as Excel.Worksbook, ; >> loWorkSheet , ; >> lnCounter, lcWorkSheetCaption, lcError, ; >> lcValidChars >> >>lcError = "" >> >>try >> lcValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 " >> loExcel = newobject("Excel.Application") >> with loExcel >> .ScreenUpdating = .f. >> .DisplayAlerts = .f. >> .WorkBooks.add() >> lnCounter = 0 >> >>** Delete all existing worksheets except 1 >> for each loWorkSheet in .WorkBooks(1).WorkSheets >> lnCounter = m.lnCounter + 1 >> if m.lnCounter > 1 >> loWorkSheet.delete >> endif >> endfor >> >> for lnCounter = 1 to alen(taXLSFiles,1) >> if file(taXLSFiles[m.lnCounter]) >> lcWorkSheetCaption = juststem(taXLSFiles[m.lnCounter]) >> loWorkBook = .WorkBooks.open(taXLSFiles[m.lnCounter]) >> loWorkBook.WorkSheets(1).copy(null, ; >> .WorkBooks(1).WorkSheets(.WorkBooks(1).WorkSheets.count)) >> .WorkBooks(1).ActiveSheet.name = ; >> right(alltrim(chrtran(m.lcWorkSheetCaption, ; >> chrtran(m.lcWorkSheetCaption,m.lcValidChars,"")," ")), 31) &&loWorkBook.Name >> loWorkBook.close(.f.) && Don't save changes >> if m.tlDeleteOriginal >> erase (taXLSFiles[m.lnCounter]) >> endif >> endif >> endfor >>** Remove the first original sheet from (Sheet1) >> .WorkBooks(1).WorkSheets(1).delete >> >> .WorkBooks(1).saveas(m.tcDestination) >> .ScreenUpdating = .t. >> .DisplayAlerts = .t. >> endwith >> >>catch to loError >> lcError = Log_Error(m.loError) >>finally >> if vartype(m.loExcel) = 'O' >> with loExcel >> .ScreenUpdating = .t. >> .DisplayAlerts = .t. >> .quit() >> endwith >> endif >>endtry >> >>return m.lcError >>endfunc >>