Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
First time with VFP to Excel
Message
General information
Forum:
Visual FoxPro
Category:
COM/DCOM and OLE Automation
Title:
First time with VFP to Excel
Miscellaneous
Thread ID:
00643532
Message ID:
00643532
Views:
142
Hi Friends!

I´m trying to send a report to a Excel Sheet, but´s my first and I´m having lot of errors. I was reading about, but I don´t know what I´m doing wrong. I think (or better, I hope) that solving some errors I´ll solve the others. (Using VFP 7.0, Win 98, Excel 97)
#INCLUDE Excel.h && Think this include Excel Parameters. Is it true?
#DEFINE Falso .F.
#DEFINE True .T.
#DEFINE N_Fuente "Arial"
#DEFINE T_Fuente 12
WAIT WINDOW NOWAIT "En proceso de selección de filas..."
USE "c:\mis documentos\respaldo\cybercafe ser\temp_repo1"
SELECT * FROM temp_repo1 INTO CURSOR SALIDA

LOCAL loExcel, lcErrorAnterior, lcRango, lnHojas, lnContador
WAIT WINDOW NOWAIT "En proceso de inicio de Excel..."
lcErrorAnterior = ON("ERROR")
ON ERROR loExcel = .NULL.
loExcel = GETOBJECT(, "Excel.application")
ON ERROR &lcErrorAnterior

IF ISNULL(loExcel)
	loExcel = CREATEOBJECT("Excel.Application")
ENDIF

WITH loExcel
	.Workbooks.ADD 
	.DisplayAlerts = Falso 
	lnHojas = .Sheets.COUNT
	FOR lnContador = 1 TO lnHojas -1
		.Sheets(1).DELETE
	ENDFOR

	WITH .Sheets(1)
		.SELECT
		.NAME = "Reporte Comaco Server"
	ENDWITH

	.ActiveWindow.DisplayGridLines = Falso 

	WAIT WINDOW NOWAIT "Construyo la fila de encabezados..."

	WITH .RANGE("A1")
		.VALUE = "Reporte de Movimientos COMACO Server"
		WITH .FONT
			.Bold = .T.
			.SIZE = 14
		ENDWITH
	ENDWITH

	WITH .RANGE("A1:G1")
* Problems start here:
* VFP Doesn´t recognize xlCenter, xlBottom, xlCenter, etc., suposedly
* Added with #Include. 
		.HorizontalAlignment = xlCenter
		.VerticalAlignment = xlBottom
*No problem  with next, just last 2 lines
*In fact, the problem is with all this kind of expression
*(alignment and lines attributes) in all program.
		.WrapText = Falso
		.ORIENTATION = 0
		.ShrinkToFit = Falso
		.MergeCells = Falso
		.Merge
	ENDWITH

	WITH .RANGE("B2")
		.VALUE = "Del Día"
		.HorizontalAlignment = xlCenter
	ENDWITH

	WITH .RANGE("C2")
		.VALUE = SALIDA.tfecha1
		.HorizontalAlignment = xlLeft
		.NumberFormat = "m/d/yy"
	ENDWITH

	WITH .RANGE("E2")
		.VALUE = "Del Día"
		.HorizontalAlignment = xlCenter
	ENDWITH

	WITH .RANGE("F2")
		.VALUE = SALIDA.tfecha1
		.HorizontalAlignment = xlLeft
	ENDWITH
	.RANGE("A4").VALUE = "Fecha"
	.RANGE("B4").VALUE = "Renta"
	.RANGE("C4").VALUE = "Impresiones"
	.RANGE("D4").VALUE = "Consumos"
	.RANGE("E4").VALUE = "V. Directa"
	.RANGE("F4").VALUE = "Descuentos"
	.RANGE("G4").VALUE = "Total Dia"

	lcRango = "A4:G4"
	WITH .RANGE(lcRango)
		.FONT.Bold = .T.
		.FONT.SIZE = T_Fuente
		.FONT.NAME = N_Fuente
		.HorizontalAlignment = xlCenter
		WITH .BORDERS(xlEdgeBottom)
			.Weight = xlMedium
			.LineStyle = xlContinuous
		ENDWITH
	ENDWITH

	WAIT WINDOW NOWAIT "En proceso de carga de celdas..."
	SELECT SALIDA
	GO TOP

	lnFila = 6
	SCAN
		WAIT WINDOW NOWAIT "Carga de la celda: Registro " + ALLTRIM(STR(RECNO()))+ ;
			" De " + ALLTRIM(STR(RECCOUNT()))
*Can´t use this, says "Expression is not valida outside With/endwith
*I can´t use .cells even with some diferent ways of with-endwith
			.Cells(lnFila,1).VALUE = SALIDA.tdia
			.Cells(lnFila,2).VALUE = SALIDA.tinternet
			.Cells(lnFila,3).VALUE = SALIDA.timpresio
			.Cells(lnFila,1).VALUE = SALIDA.tconsumos
			.Cells(lnFila,1).VALUE = SALIDA.tdirecta
			.Cells(lnFila,1).VALUE = SALIDA.tdesconta
			.Cells(lnFila,1).VALUE = SALIDA.ttotaldia
			lnFila = lnFila + 1
		
	ENDSCAN


	.Cells(lnFila + 2, 1).VALUE = "Totales"
	WITH .Cells(lnFila + 2, 2)
		.VALUE = "=SUM(B6:B" + ALLTRIM(STR(lnFila-1)) + ")"
		WITH .BORDERS(xlEdgeBottom)
			.Weight = xlMedium
			.LineStyle = xlDouble
		ENDWITH
	ENDWITH

	WITH .Cells(lnFila + 2, 3)
		.VALUE = "=SUM(C6:C" + ALLTRIM(STR(lnFila-1)) + ")"
		WITH .BORDERS(xlEdgeBottom)
			.Weight = xlMedium
			.LineStyle = xlDouble
		ENDWITH
	ENDWITH

	WITH .Cells(lnFila + 2, 4)
		.VALUE = "=SUM(D6:D" + ALLTRIM(STR(lnFila-1)) + ")"
		WITH .BORDERS(xlEdgeBottom)
			.Weight = xlMedium
			.LineStyle = xlDouble
		ENDWITH
	ENDWITH

	WITH .Cells(lnFila + 2, 5)
		.VALUE = "=SUM(E6:E" + ALLTRIM(STR(lnFila-1)) + ")"
		WITH .BORDERS(xlEdgeBottom)
			.Weight = xlMedium
			.LineStyle = xlDouble
		ENDWITH
	ENDWITH

	WITH .Cells(lnFila + 2, 6)
		.VALUE = "=SUM(F6:F" + ALLTRIM(STR(lnFila-1)) + ")"
		WITH .BORDERS(xlEdgeBottom)
			.Weight = xlMedium
			.LineStyle = xlDouble
		ENDWITH
	ENDWITH

	WITH .Cells(lnFila + 2, 7)
		.VALUE = "=SUM(G6:G" + ALLTRIM(STR(lnFila-1)) + ")"
		WITH .BORDERS(xlEdgeBottom)
			.Weight = xlMedium
			.LineStyle = xlDouble
		ENDWITH
	ENDWITH

	lcRango = "A6:G" ALLTRIM(STR(lnFila+2))
	WITH .RANGE(lcRango)
		.FONT.SIZE = T_Fuente
		.FONT.NAME = N_Fuente
		.NumberFormat = "($#,##0.00_); [Red]($#,##0.00)"

		.COLUMNS("A:G").EntireColumn.Autofit
	ENDWITH
ENDWITH
WAIT CLEAR
loExcel.VISIBLE = .T.
RELEASE.loExcel
CLOSE TABLES
RETURN
So it seems like only 2 errors: .cell procedures and some Excel attributes. Thanks in advance to those who expend a little time reading my code! like alignment and lines...
By two common phrases in his vocabulary, we know Hercule Poirot was a great Developer: his use of "the little grey cells" and "order and method".
Next
Reply
Map
View

Click here to load this message in the networking platform