270912018 Excel - How to do Aulomation from VFP - Microsoft: Visual FoxPro FAQ Tek-Tips
COM and Automation
Excel - How to do Automation from VEFP by jimoo
faqis4-4428
Posted: 12 Nov 03 (Edited 9 Apr 04)
* Simple automation with Excel. Just copy this faq into prg file and run it.
* include an EXCEL header file and reference values by name.
* INCLUDE C:\MyProject\INCLUDE\x/5en32.h
* If you do not have a header file and need to create one. Refer to FAQ:
= How to create office header files in VFP FAQ184-2749
* creates random numbers for quarterly data,
* adds some detail records
(CREATE CURSOR curCompany (Company C20), Qte1 N(10,2), atr2 N(10,2),atr3 N(1O,2), 4 NC10,2))
FOR Ini = 170 10
‘APPEND BLANK
REPLACE curCompany.company WITH S¥S(2015)
REPLACE curCompany.atri WITH 1 + 1000 * RAND( )
REPLACE curCompany.ctr2 WITH 1 + 1000 * RAND( }
REPLACE curCompany.qtr3 WITH 1 + 1000 * RAND( }
REPLACE curCompany.qtr4 WITH 1 + 1000 * RAND( }
ENDFOR
™ Excel: HorizontalAlignment
local o€xcel, oSheet
‘oExcel = CreateObject( {Excel Application])
‘oxcel Visible = .T.
‘Excel Workbooks Add)
‘Sheet = oFxcel.ActiveSheet
InRow = 0
‘SELECT curCompany
GO TOP
DO WHILE NOT EOF()
InRow = InRow + 1
IF InRow = 1
‘Sheet Cells(InRow,1) Value = [FoxPro Rocks!)
oSheet.Range({C3]).Select
‘Sheet Cells(InRow,InCol). Value = (Qtr 1)
‘Sheet. Cells(InRow,InCol)-Font.Bold = 7.
*oSheet.Cells(nRow,InCol) HorizontalAlignment
‘Sheet. Cells(InRow, InCol).Horizontalalignment
InCol = InCol + 1
‘Sheet. Range([D3]) Select
‘Sheet Cells(InRow,InCol) Value = (Qtr 2]
‘Sheet. Cells(inRow, InCol)-Font.Bold = 7,
*oSheet.Cells(InRow,InCol).HorizontalAlignment
‘Sheet. Cells(InRow,InCol).HorizontalAlignment = 3
InGol = Incol + 4
‘Sheet Range({E3]).Select
‘Sheet Cells(InRow,InCol). Value = (Qtr 3]
‘Sheet. Cells(InRow,InCol)-Font.Bold = 7.
*oSheet.Cells(!nRow,InCol).Horizontalalignment
‘Sheet Cells(InRow,InCol) HorizontalAlignment = 3
hitpslwwwcteltps.comifags.cfm?nd=4428
18270912018 Excel - How to do Aulomation from VFP - Microsoft: Visual FoxPro FAQ Tek-Tips
InCol = Incol + 1
Sheet Range({F3]) Select
‘Sheet. Cells(InRow,InCol).Value = (Qtr 4]
‘Sheet Cells{InRow,InCol) Font. Bold = 7.
*oSheet.Cells(InRow,inCol).HorizontalAlignment = xiCenter
‘Sheet. Cells(InRow,InCol).HorizontalAlignment = 3
InRow = 4
InBeginRange = InRow
ENDIF
oSheet-Cells(InRow,1).Value = curCompany.Company
oSheet.Cells(InRow,3).Value = curCompany.qtr
oSheet.Cells(InRow,4) Value = curCompany.qtr2
oSheet.Cells(InRow,5)-Value = curCompany.qtr3
oSheet.Cells(InRow,6).Value = curCompany, qtr
SKIP
ENDDO
* Create the formula rather than hardcoding total so the user can
* change the spreadsheet and it will reflect new totals.
= Example: =SUM(D5:D10)
FOR Ini = 1704
IcFormula = [=SUM(] + CHR(64 + Ini) + ALLTRIM(STR(m.InBeginRange)) + [:] +;
CHR(64 + 3 + Ini) + ALLTRIM(STR(m.InRow)) + J]
‘Sheet Cells(InRow+1,2+Ini) Formul
ENDFOR
[alcFormuta]
Late Edition.
These miscellaneous Excel automation command are compliments of jrbbldr
JRB-Bidr
VisionQuest Consulting
Business Analyst & CIO Consulting Services
C1OServices@yahoo.com
‘tmpsheet = CREATEOBJECT(‘excel.application’)
‘oExcel = tmpsheet APPLICATION
‘Set Excel to only have one worksheet —~
‘oF xcel SheetsInNewWorkbook = 1
* ~ Delete the Default Workbook that has 3 worksheets —
‘oExcel, Workbooks. CLOSE
Now Add a new book with only 1 worksheet —~
‘Excel, Workbooks.ADD
xIBook = oExcel.ActiveWorkbook.FULLNAME
xISheet = o€xcel activesheet
Name Worksheet ~
xISheet. NAME = "Sheet Name"
Make Excel Worksheet Visible To User
‘oFxcel. VISIBLE = .T. && Set. if you want to print only
-
‘oExcel, WINDOWS(xIBook) ACTIVATE
xISheet.RANGE((A2]) SELECT
hitpslwwwcteltps.comifags.cfm?nd=4428
28270912018 Excel - How to do Aulomation from VFP - Microsoft: Visual FoxPro FAQ Tek-Tips
Save Excel Results
‘oExcel,CutCopyMode = .F. && Clear the clipboard from previous Excel Paste
‘oFxcel-DisplayAlerts =F.
Save Results ~
xISheet.SAVEAS(mcExclFName)
Close the Worksheet —
‘oExcel. workbooks.CLOSE
* = Quit Excel
(oExcel.QUIT
RELEASE o€xcel
tmpsheet = CREATEOBJECT(‘excel application’)
‘oExcel = tmpsheet APPLICATION
‘oxcel.ReferenceStyle = 1 && Ensure Columns in A-B Format instead of 1-2 Format
mcStrtColRow = 'AL"
mcEndColRow = ‘ABS!
meLastCol = 'AZ:"
Time Masquerading As Text Format Cells ~
xISheet.RANGE[meStrtColRow,mcEndColRow]-EntireColumn. NumberFormat
‘Standard Text Format Cells
xlSheet.RANGE[meStrtColRow,mcEndColRow]-EntireColumn.NumberFormat
Date Format Cells
xlSheet RANGE{meStriColRow,mcEndColRow],EntireColumn. NumberFormat = "mm/dd/yyyy"
*—-- Auto-Fit All Columns -—
xISheet.COLUMNS("A:" + mcLastCol) EntireColumn.AutoFit
hitpslwwwcteltps.comifags.cfm?nd=4428
38