Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trapping an Excel import error

Status
Not open for further replies.

Mrall

Programmer
Nov 22, 2008
64
US
I have an import routine that imports an excel spreadsheet. It works just fine unless the user forgets to do a save as Excel 5.0/95 Workbook file format. The program hangs up and corrupts the database file. Is there a way to error trap for this or detect the file type before importing.

Thanks
 
Without getting into verifying the excel file itself against the BIFF5/7 specifications, I'd suggest importing the file to a temporary cursor first with error trapping.

If the file imports to the temporary cursor without errors, then append to the main table.


Code:
  llOK = .T.
  TRY
    SELECT tempcursor
    APPEND FROM (lcXLSFile) XLS
  CATCH to oError
    llOK = .F.
  ENDTRY
  IF m.llOK
    *!* import from tempcursor to main table
  ELSE
    *!* There was an error importing to the tempcursor
  ENDIF
 
It works just fine unless the user forgets to do a save as Excel 5.0/95 Workbook file format.

Since you mention doing a SaveAs, I assume that you are doing your Import through Excel Automation instead of by using APPEND FROM....

If you are using Excel Automation, then why should the user need to be involved in the SaveAs format? You can have your Excel Automation do that for you programatically.

If you are not using Excel Automation, then perhaps if you explain the process you are using for your Excel Import, we can better target our responses.

Good Luck,
JRB-Bldr
 
Mrall,

You asked if you can trap the error. Yes, you can:

Code:
lnError = 0
TRY
  SELECT MyTable
  APPEND FROM SomeFile.xlsx TYPE XL8
CATCH TO loErr
  lnError = loErr.ErrorNo
ENDTRY

IF lnError = 1661
  * Invalid format
ELSE
  * All is well
ENDIF


Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips, training, consultancy
 
I would like to do the Excel Automation but have no clue as to how this is done. I have Excel 2003 but some of the users have Excel 2010. Does this have any effect on the outcome? The big question is, how do I do this?

Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top