270912018 Excel Automation - Visual FoxPro Wik
wide
“8% Excel Automation
For danty’s sake, the full bject hierarchy Is used for each commane. Using
"Wh oBxcl'willtand to make your code more readable Contents:
Connecting to Excel peng ar exzing 1s
Well assume throughout tis page that you name your Exel object renin nk
‘tcl and your werkbook oWlerceok. [Crs ne wortuoe red on 2 ene
‘obxcel = CreateObject (“Excel Application")
vartype (
+ mow an error message here
endit Seng ange ang Cal coat
Constants for Excel
‘Excel Conetants documents the constants that Excel's macros use (xIThs,
saat ete)
2007/08/30 Mike Mont - Excel 2003 Constants MSDN Library
2007/09/30 Mike Mohr - Excel 2007 (fice 12) Constants MSDN Library
Opening an existing XLS
ollorkbook = ofscel . Application. workbooks open ("Ci \tenp\test.x18")
Creating a blank workbook:
oltozkbeok = cfcel application lorkbooks.Addt)
Creating a new workbook based on a templ
lfockbock = oBxcel appli
‘You can tum off Exca's alerts with the Display Alerts property: when set to False, Excel automatically chooses the default answer to any
message:
Saving a workbook as Excel 97/03 from Excel 2007
Using Save As while automating Excel 2007 creates @ 2007 style workbook with an XLS extension (regardless of compatibilty settings) unless
the file format is specie:
4S vai fobecel version) > 12
okorkbook. Savena("C:\tenp\foobar.s.e", 56) 66 xlbxcel®
ntpfox.wkis.comive-dI7Wiki-ExcelAutomation
19270912018 Excel Automation - Visual FoxPro Wik
Controlling visibility
the Excel window isnot vee es harder forthe user to interact with Excel. This makes fe slghtly safer for your automation asthe user is less
Hkaly to esue commands inthe midale of your automation.
oproel.visible = 1.
Controlling Interaction
iso, iit is preerres that Excel be seen uring automation set these two properties to
obxcel Applicat ien.Interactive
‘Aer completing automation, returs ther value to: to ollow the user to star interaction
obxcel -Application.tnteractive=.7.
[The tataractive property is the one tht controls whether the users allowed to interact with Excel When set to F, the user gets the
hourglass icon when they hover over Excel, and mouse clicks on the Excel application are ignore.
‘The UserControl property does NOT prevent the user rom interacting with Excel. That property indlcates whether the Excel application was
opened by the user (1), or whether the Excel application was opened programmatically via CREATEOBJECT () (.). 1f you used GETOBJECT ,
"Excel Application” } to get # reference tothe Excel application, you can use tis property te determine if you are sharing the application
withthe user or not. One cool hing i that tis property is automatically updated ifthe user closes Excel.
‘See UserControl Property [Excel 2007 Developer Reference]
Bottom ne: eventhough you CAN change UserControl, I recommend that you NOT do thet
ike Poter
Storing data to a cell
(= ange (th2") Vale = “Hello world
Set the font color and style
opxcel Range ("BG") .font.bold = .t
ofeee! Range ("BE") font-eoloringes = 3 64 ed
obxoe! Range ("BE") Select ()
obxoel Selection. font bold = «t
= Dovid Fung
Set Excel Cell to Text Format
I your app exports user-antared tax data from a felé into an Excel cll, you always convert the cells rumberformat t text prior to setting the
‘value, forgat hiss an app once, anda user entered a long string of equal signs into 2 notes eld in VFP, which generates 9 COM error when
ntpfox.wkis.comive-sI7Wiki-ExcelAutomation270912018 Excel Automation - Visual FoxPro Wik
sting the cals value (Excel doesnt ke"
as 2 value, thinks it's an incorrectly written formula)
ogee! Range ("AI") Value ~ eUrsora.MenoPSeld
Bryan Palmer
Getting data into Excel
‘Assuming your table s selected. First the easy way. You can make 2 new len an old Excel format which al version should be able to reas
copy to et\mytable.xle type x18
Cr you have a pre-ormatted template (X15 oF XLT) tht you went to paste into. Note that this method will nat handle Memo ees.
“VE. CatsToel p(s (3) G6 current table onto the clipboard, delimized with tab
Gexcel Range ("AI"). Select
pbteel Aetivasheet Paste () Ge fron clipboard. aines Wf with tab, store data into colume
Selecting a range using the Cells collection
aixco! Range otixcel cela{Iy 1), alice? Celle (3-3)) Select
Resize all columns
Actsvosheot.Usediange..EntareColumn aucotit
Insert two rows before the first row
obxcel. Rona (%:2") tngert(-4121) 46 slbown
David Fung
Closing Excel
‘Youl stil need to handle closing questions Ike saving changes and fle format changes. And you'll need to release your of3cel object
opnoed att t)
1 find that an instanceof Excel remsins ater quting, To Clear isue
Closing Excel with all changes discarded - no question asked
ntpfox.wkis.comive-sI7Wiki-ExcelAutomation