Friday, May 8, 2015

VFP function that combines individual xls files

VFP function that combines individual xls files

I need to put get data from VFP and put it into multiple sheets in an Excel file. I found this code on Internet. I have not tested it so far.

--------- Original link and post: ---------

https://social.msdn.microsoft.com/Forums/en-US/a9b81c52-2969-4403-81e2-b5ce891f1a2f/copy-to-excel-and-adding-worksheets?forum=visualfoxprogeneral

I have this function that we use to combine individual xls files:

************************************************************
* Function CombineExcelFiles
************************************************************
* Created...........: Craig Boyd 3/6/2006 23:55:50
*) Description.......:
* Calling Samples...: DIMENSION aXLSFiles(3)
*!*     aXLSFiles(1) = "C:\temp1.xls"
*!*     aXLSFiles(2) = "C:\temp2.xls"
*!*     aXLSFiles(3) = "C:\temp3.xls"
*!*     CombineExcelFiles(@aXLSFiles, "C:\XLSCombined.xls")
* Parameter List....:
* Major change list.:
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.) && Do not 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



Naomi Nosonovsky, Sr. Programmer-Analyst

Useful Excel Automation examples

 
Useful Excel Automation examples
 
Posted: 5 Oct 03 (Edited 8 Oct 03)

Here is an on-going compilation of Excel automation samples.
  1. How to copy a .jpg from a general field to an Excel sheet.

    oExcel =CREATEOBJECT("excel.application")
    oWorkBook = oExcel.workbooks.add()
    oSheet = oWorkbook.activesheet
    USE e:\trans\pics AGAIN IN 0 && The table with the general field that holds the jpg.
    LOCATE && Go op
    KEYBOARD "{CTRL+C}{CTRL+W}" && Copy the jpg
    MODIFY GENERAL pics.pic
    oSheet.paste() && Paste the clipboard content in the the sheet
    oExcel.visible = .t.

  2. How to create a chart via Excel automation

    #DEFINE xlColumnClustered 51
    LOCAL oExcel as Excel.application
    LOCAL oWorkbook,oSheet
    oExcel = CREATEOBJECT("Excel.application")
    oWorkbook= oExcel.Workbooks.Add()
    oSheet = oWorkbook.activesheet
    WITH oSheet
    .Range("A1").Select
    .Range("A1").FormulaR1C1 = "1"
    .Range("A2").Select
    .Range("A2").FormulaR1C1 = "2"
    .Range("A3").Select
    .Range("A3").FormulaR1C1 = "3"
    .Range("A4").Select
    .Range("A4").FormulaR1C1 = "4"
    .Range("A5").Select
    .Range("A5").FormulaR1C1 = "5"
    .Range("A6").Select
    .Range("A6").FormulaR1C1 = "6"
    .Range("B1").Select
    .Range("B1").FormulaR1C1 = "10"
    .Range("B2").Select
    .Range("B2").FormulaR1C1 = "11"
    .Range("B3").Select
    .Range("B3").FormulaR1C1 = "50"
    .Range("B4").Select
    .Range("B4").FormulaR1C1 = "60"
    .Range("B5").Select
    .Range("B5").FormulaR1C1 = "70"
    .Range("B6").Select
    .Range("B6").FormulaR1C1 = "90"
    .Range("A1:B6").Select
    ENDWITH
    WITH oWorkbook
    .Charts.Add
    .ActiveChart.ChartType = xlColumnClustered
    .ActiveChart.SetSourceData(oSheet.Range("A1:B6"))
    .ActiveChart.HasTitle = .f.
    ENDWITH
    oExcel.Visible =.t.


  3. How to delete a sheet from a workbook
    Local oSheet,oWorkBook,oExcel
    oExcel = CREATEOBJECT("Excel.application")
    oWorkBook = oExcel.Workbooks.Add()
    oSheet = oWorkBook.activeSheet
    oSheet.Delete()
    oExcel.Visible = .t.


  4. How to add a sheet to a workbook
    Local oSheet,oWorkBook,oExcel
    oExcel = CREATEOBJECT("Excel.application")
    oWorkBook = oExcel.Workbooks.Add()
    oWorkbook.Sheets.Add
    oExcel.Visible = .t.


  5. How to move a sheet within a Workbook.
    oExcel = CREATEOBJECT("excel.application")
    oWorkbook = oExcel.Workbooks.Add()
    oWorkbook.Sheets.Add
    oSheet = oWorkbook.ActiveSheet
    oSheet.Move(,oWorkbook.Sheets(4)) && Move after sheet3
    oSheet.Move(oWorkbook.Sheets(4),) && Move before sheet3
    oExcel.Visible =.t.

Mike Gagnon