Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Strange problem with saving Excel to DBF
Message
 
 
To
All
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Title:
Strange problem with saving Excel to DBF
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01133792
Message ID:
01133792
Views:
58
Hi everybody,

I have an Excel file containing Arabic. I've simplified my convert to DBF routine to this:
************************************************************
*  FUNCTION ConvertExcelToDBF()
************************************************************
*  Author............: VCS Developers Team
*  Project...........: Visual Collections System
*  Created...........: 01/01/2000  17:00:00
*  Copyright.........: (c) Jzanus, 2006
*) Description.......: Takes an Excel file and makes a DBF out of it
*  Calling Samples...: ConvertExcelToDBF(getfile('XLS'))
*  Parameter List....: tcInputFile, tnStartFromRow, tcDbfFile
*  Major change list.:
function ConvertExcelToDBF
lparameters tcInputFile, tnStartFromRow, tcDbfFile

if empty(m.tcInputFile)
	tcInputFile = getfile('xls','Select XLS', 'Select', 0, 'Select XLS to create DBF from')
	if empty(m.tcInputFile)
		return "Input file is not passed"
	endif
endif

local OleApp, lcMsg, lcStr, lcRange, ;
	lnRow, lnCol, lnNumSheets, lnDoneSheets, lnI, loSheet, ;
	lnRows, lnCols, llValFound, loErr, ;
	lnSameField, lnK, lnSheet

lcMsg = ""
lnDoneSheets = 0

if empty(m.tnStartFromRow)
	tnStartFromRow = 1
endif

try

	OleApp = createobject("Excel.Application")
*-- Open XLS File
	OleApp.application.WorkBooks.open(m.tcInputFile)

	lnNumSheets = OleApp.application.application.WorkBooks(1).Sheets.count

	for lnSheet = 1 to m.lnNumSheets

*-- Select individual sheet from opened XLS File
		loSheet = OleApp.application.application.WorkBooks(1).Sheets(m.lnSheet)
		with loSheet
			.select

			lcStr = OleApp.application.ActiveSheet.name

** Do this only for non-SUMMARY sheets in the spreadsheet
			if !("SUMMARY" $ upper(m.lcStr))

				lnDoneSheets = m.lnDoneSheets + 1

** Delete first N unused rows (help by Sergey Berezniker)
				if m.tnStartFromRow > 1
					.range("A1" , "A" + transform(m.tnStartFromRow)).EntireRow.delete()
				endif

*-- Find total # of columns
				lnCols = .UsedRange.columns.count
				lnRows = .UsedRange.rows.count

				if empty(m.tcDbfFile)
					lcStr = strtran(m.lcStr,space(1),"_")
					lcStr = strtran(m.lcStr,".","_")
					lcStr = upper(strtran(upper(justfname(m.tcInputFile)),".XLS","") + ;
						"(" + m.lcStr + ")")
					tcDbfFile = sys(5) - curdir()- m.lcStr
				endif

				lcRange = "A1:"
				lcRange = m.lcRange + chr(64 + m.lnCols) + alltrim(str(m.lnRows))

				.range(m.lcRange).select
				OleApp.DisplayAlerts = .f.
*#DEFINE xlDBF3	8
*#DEFINE xlDBF4	11	
				OleApp.ActiveWorkbook.saveas(m.tcDbfFile, 11)
			endif
		endwith
	next

	if (m.lnDoneSheets = 0)
		lcMsg = "There are no non-summary sheets in the file - cannot process."
	else
		if (m.lnDoneSheets > 1)
			lcMsg = "There are too many sheets in the file - cannot determine which to use."
		endif
	endif

catch to loErr
	lcMsg = Log_Error(m.loErr)

finally
	if vartype(m.OleApp) = 'O'
		OleApp.DisplayAlerts = .f. && we don't want a question asked
		OleApp.quit
	endif
endtry

OleApp = null

return m.lcMsg

endfunc
Still when I try it, it creates only 8 fields our of 40. If I do the same process manually (e.g. open Excel, select used cells, save as DBF4 format), all 40 fields are created. In both cases Arabic become ?????????, but I'm not worried about this at the moment.

Do you know what's wrong with my code and why it doesn't work?

Thanks in advance.
If it's not broken, fix it until it is.


My Blog
Next
Reply
Map
View

Click here to load this message in the networking platform