Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Excel spreadsheets to one workbook
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 7
Miscellaneous
Thread ID:
01101620
Message ID:
01101685
Views:
16
>>There are eleven separate Excel spreadsheets in a directory on the hard disk. I want to create a new Excel workbook and bring in each of the eleven spreadsheets as a page in the workbook. So there will be eleven sheets in the workbook.
>>
>>Is it possible to bring in the individual spreadsheets and combine them into one workbook using VFP code?
>>
>>Cyril
>
>Something like the following should work or at least get you close. Send the function an array of the xls files (fullpath and filename) you want combined. IMPORTANT NOTE: The code will erase the orginals. If this is not the behavior you want then you can get rid of the Erase code at the bottom of the function.
>
>
FUNCTION CombineExcelFiles (taXLSFiles)
>	EXTERNAL ARRAY taXLSFiles
>	LOCAL loDestinationWrkBk as Excel.Application, loSourceWrkBk, lnCounter, lnSheetCount, loWorkSheet
>	loDestinationWrkBk = NEWOBJECT("Excel.Application")
>	loDestinationWrkBk.ScreenUpdating = .F.
>	loDestinationWrkBk.DisplayAlerts = .F.
>	loDestinationWrkBk.Workbooks.Open(taXLSFiles(1,1))
>	lnCounter = 0
>	FOR EACH loWorkSheet IN loDestinationWrkBk.Workbooks(1).WorkSheets
>		lnCounter = lnCounter + 1
>		IF lnCounter > 1
>			loWorkSheet.Delete
>		ENDIF
>	ENDFOR
>	lnSheetCount = loDestinationWrkBk.Workbooks(1).Worksheets.Count
>	FOR lnCounter = 2 TO ALEN(taXLSFiles,1)
>		IF FILE(taXLSFiles(lnCounter, 1))
>			loWorkBook = loDestinationWrkBk.Workbooks.Open(taXLSFiles(lnCounter, 1))
>			loWorkBook.Worksheets(1).Copy(NULL, loDestinationWrkBk.Workbooks(1).Worksheets(loDestinationWrkBk.Workbooks(1).Worksheets.Count))
>			loDestinationWrkBk.Workbooks(1).ActiveSheet.Name = RIGHT(ALLTRIM(CHRTRAN(taXLSFiles(lnCounter, 2),CHRTRAN(taXLSFiles(lnCounter, 2),"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ","")," ")), 31) &&loWorkBook.Name
>			loWorkBook.Close(.F.)
>		ENDIF
>	ENDFOR
>	loDestinationWrkBk.Save()
>	loDestinationWrkBk.ScreenUpdating = .T.
>	loDestinationWrkBk.DisplayAlerts = .T.
>	loDestinationWrkBk.Quit()
>	FOR lnCounter = 2 TO ALEN(taXLSFiles,1)
>		IF FILE(taXLSFiles(lnCounter, 1))
>			ERASE (taXLSFiles(lnCounter, 1))
>		ENDIF
>	ENDFOR
>ENDFUNC
Thanks Craig for your very comprehensive answer.
I am getting an error on this line:

loDestinationWrkBk.Save()
OLE IDispatch exception code 0 from Microsoft Excel: Unable to get the Save property of the Application class..

Does this look like a code error, or something I'm doing wrong in my testing?

Thanks again
Cyril
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform