Microsoft - Visual FoxPro - Excel Import
Microsoft - Visual FoxPro - Excel Import
Go
Find A Forum
Go
Home > Forums > Programmers > DBMS Packages > Microsoft: Visual FoxPro Forum thread184-1654224
Share This
Excel Import
Could someone show me a drop dead example of pulling rows out of Excel and into VFP9? We get a spreadsheet by email several times a day and I've been able to importing them. Well, they have changed the format with about 5 lines of "Header Junk" and the data now starts on row 6 vs row 1. I know I need to use Automation but I don't really know how. A start would be great help!
TamarGranor (Programmer)
7 Jul 11 16:20
L O C A Lo X L ,o B o o k ,o S h e e t o X L=C R E A T E O B J E C T ( " E x c e l . A p p l i c a t i o n " ) o B o o k=o X L . W o r k b o o k s . O p e n ( " Y o u rf i l ew i t hp a t h " ) o S h e e t=o B o o k . W o r k s h e e t s [ 1 ] n L a s t R o w=o S h e e t . U s e d R o w s . C o u n t *A s s u m e sd a t as t a r t si nr o w1 ,c o l1 .A d j u s ta sn e c e s s a r y F O Rn R o w=1T Om . n L a s t R o w c I t e m 1=o S h e e t . C e l l s ( m . n R o w ,1 ) c I t e m 2=o S h e e t . C e l l s ( m . n R o w ,2 ) *e t c . I N S E R TI N T OY o u r T a b l e; V A L U E S( m . c I t e m ,m . c I t e m 2 ) E N D F O R o B o o k . C l o s e ( ) o X L . Q u i t ( ) Tamar
seckner (Programmer)
7 Jul 11 16:37
Wow - Thank you! Small problem, again it's my lack of knowledge - when I try to run this I get an error stating that USEDROWS does not evaluate to an
www.tek-tips.com/viewthread.cfm?qid=1654224 1/7
8/16/13
TamarGranor (Programmer)
8 Jul 11 16:31
That's because I got it wrong, typing off the top of my head. Try UsedRange.Rows where I had UsedRows. Tamar
Nifrabar (Programmer)
10 Aug 11 7:30
fwiw I once found this info somewhere on the www. so credits for Alex Feldstein, Tom Cerul, Stuart Dunkeld, David Fung and Cetin Basoz! It might help making a start with excel-automation
CODE
W i d eE x c e lA u t o m a t i o n ( U p d a t e d :2 0 0 7 . 0 8 . 2 40 1 : 4 7 : 1 0P M ) N a m e s p a c e :V F P
E d i tF i n d -R e c e n tC h a n g e sC a t e g o r i e sR o a dM a p[ A F ][ G P ][ Q Z ]N e wT o p i cH o m eS e a r c h : F o rc l a r i t y ' ss a k e ,t h ef u C o n n e c t i n gt oE x c e l W e ' l la s s u m et h r o u g h o u tt h i sp a g et h a ty o un a m e dy o u rE x c e lo b j e c to E x c e la n dy o u rw o r k b o o ko W o r k b o o k . o E x c e l=C r e a t e O b j e c t ( " E x c e l . A p p l i c a t i o n " ) i fv a r t y p e ( o E x c e l )! =" O " *c o u l dn o ti n s t a n t i a t eE x c e lo b j e c t *s h o wa ne r r o rm e s s a g eh e r e r e t u r n. F . e n d i f C o n s t a n t sf o rE x c e l E x c e lC o n s t a n t sd o c u m e n t st h ec o n s t a n t st h a tE x c e l ' sm a c r o su s e( x l T h i s ,x l T h a te t c ) 2 0 0 7 / 0 8 / 3 0M i k eM o h r-E x c e l2 0 0 3C o n s t a n t sM S D NL i b r a r y 2 0 0 7 / 0 8 / 3 0M i k eM o h r-E x c e l2 0 0 7( o f f i c e1 2 )C o n s t a n t sM S D NL i b r a r y O p e n i n ga ne x i s t i n gX L S o W o r k b o o k=o E x c e l . A p p l i c a t i o n . W o r k b o o k s . O p e n ( " C : \ t e m p \ t e s t . x l s " )
C r e a t i n gab l a n kw o r k b o o k : o W o r k b o o k=o E x c e l . A p p l i c a t i o n . W o r k b o o k s . A d d ( )
8/16/13
C o n t r o l l i n gv i s i b i l i t y I ft h eE x c e lw i n d o wi sn o tv i s i b l ei ti sh a r d e rf o rt h eu s e rt oi n t e r a c tw i t hE x c e l .T h i sm a k e si ts l i g h t l ys a f e r o E x c e l . v i s i b l e=. T . o E x c e l . v i s i b l e=. F . C o n t r o l l i n gI n t e r a c t i o n A l s o ,i fi ti sp r e f e r r e dt h a tE x c e lb es e e nd u r i n ga u t o m a t i o ns e tt h e s et w op r o p e r t i e st o. F . o E x c e l . A p p l i c a t i o n . U s e r C o n t r o l = . F . o E x c e l . A p p l i c a t i o n . I n t e r a c t i v e = . F . A f t e rc o m p l e t i n ga u t o m a t i o n ,r e t u r nt h e i rv a l u et o. T .t oa l l o wt h eu s e rt os t a r ti n t e r a c t i o n o E x c e l . A p p l i c a t i o n . U s e r C o n t r o l = . T . o E x c e l . A p p l i c a t i o n . I n t e r a c t i v e = . T . S t o r i n gd a t at oac e l l o E x c e l . R a n g e ( " b 2 " ) . V a l u e=" H e l l ow o r l d " S e tt h ef o n tc o l o ra n ds t y l e o E x c e l . R a n g e ( " B 6 " ) . f o n t . b o l d=. t . o E x c e l . R a n g e ( " B 6 " ) . f o n t . c o l o r i n d e x=3& &r e d o r o E x c e l . R a n g e ( " B 6 " ) . S e l e c t ( ) o E x c e l . S e l e c t i o n . f o n t . c o l o r i n d e x=3& &r e d o E x c e l . S e l e c t i o n . f o n t . b o l d=. t .
-D a v i dF u n g G e t t i n gd a t ai n t oE x c e l A s s u m i n gy o u rt a b l ei ss e l e c t e d .F i r s tt h ee a s yw a y .Y o uc a nm a k ean e wf i l ei na no l dE x c e lf o r m a tw h i c ha l lv e r c o p yt oc : \ m y t a b l e . x l st y p ex l 5
O ri fy o uh a v eap r e f o r m a t t e dt e m p l a t e( . X L So r. X L T )t h a ty o uw a n tt op a s t ei n t o .N o t et h a tt h i sm e t h o dw i l ln o _ V F P . D a t a T o C l i p ( , , 3 ) & &c u r r e n tt a b l eo n t ot h ec l i p b o a r d ,d e l i m i t e dw i t ht a b o E x c e l . R a n g e ( " A 1 " ) . S e l e c t o E x c e l . A c t i v e S h e e t . P a s t e ( ) & &f r o mc l i p b o a r d .s i n c ed e l i m i t e dw i t ht a b ,s t o r ed a t ai n t oc o l u m n s S e l e c t i n gar a n g eu s i n gt h eC e l l sc o l l e c t i o n o E x c e l . R a n g e ( o E x c e l . c e l l s ( 1 ,1 ) ,o E x c e l . C e l l s ( 3 . 3 ) ) . S e l e c t R e s i z ea l lc o l u m n s o E x c e l . A c t i v e S h e e t . U s e d R a n g e . E n t i r e C o l u m n . A u t o f i t I n s e r tt w or o w sb e f o r et h ef i r s tr o w o E x c e l . R o w s ( " 1 : 2 " ) . I n s e r t ( 4 1 2 1 )& &x l D o w n -D a v i dF u n g www.tek-tips.com/viewthread.cfm?qid=1654224 3/7
8/16/13
C l o s i n gE x c e l Y o u ' l ls t i l ln e e dt oh a n d l ec l o s i n gq u e s t i o n sl i k es a v i n gc h a n g e sa n df i l ef o r m a tc h a n g e s .A n dy o u ' l ln e e dt or e l o E x c e l . q u i t ( ) C l o s i n gE x c e lw i t ha l lc h a n g e sd i s c a r d e d-n oq u e s t i o na s k e d o E x c e l . D i s p l a y A l e r t s=. F . o E x c e l . A c t i v e W i n d o w . C l o s e ( . f . )& &a s s u m i n go n l yo n ea c t i v ew i n d o w o E x c e l . q u i t ( ) -D a v i dF u n g I t e r a t et h r o u g hE x c e l ' sW o r k s h e e t s F o ri = 1T oo E x c e l . A c t i v e W o r k b o o k . S h e e t s . C o u n t ?o E x c e l . A c t i v e W o r k b o o k . S h e e t s ( i ) . N a m e E n d f o r F i n d i n gt e x ti nE x c e l S e a r c h i n gf o r" T a x " o E x c e l . R a n g e ( " A 1 : H 2 5 0 " ) . F i n d ( " T a x " ) . S e l e c t & &s i m p l ed e f a u l ts e a r c h *A l t e r n a t e l y *R a n g e . F i n d ( W h a t ,A f t e r ,L o o k I n ,L o o k A t ,S e a r c h O r d e r ,S e a r c h D i r e c t i o n ,M a t c h C a s e ,M a t c h B y t e ,S e a r c h F o r m a t ) o E x c e l . R a n g e ( " A 1 : H 2 5 0 " ) . F i n d ( " T a x " ,o E x c e l . A c t i v e C e l l ,4 1 2 3 ,1 ) . S e l e c t
Y o uh a v et ob ec a r e f u lw h e ns p e c i f y i n gt h ee x t r ap a r a m e t e r st oF i n da st h e yp e r s i s tb e t w e e ns e a r c h e s ,a ss p e c i f i e
T h es e t t i n g sf o rL o o k I n ,L o o k A t ,S e a r c h O r d e r ,a n dM a t c h B y t ea r es a v e de a c ht i m ey o uu s et h i sm e t h o d .I fy o ud o n ' t -S t u a r tD u n k e l d D e v e l o p i n gn e wc o d e S o m e t i m e st h ee a s i e s tw a yt of i g u r eo u th o wt oc o d ea na u t o m a t i o nr o u t i n ei st h i s :o p e nE x c e l ,t e l li tt or e c o r d P u t t i n gi ta l lt o g e t h e r ,ar u n n a b l ee x a m p l e F i r s t ,C O P YT Oa l lf i e l d s( o rs o m e )i nE x c e lF o r m a t
# d e f i n ex l L a s t C e l l1 1 # d e f i n ex l M a x i m i z e d4 1 3 7 # d e f i n ex l R a n g e A u t o f o r m a t C l a s s i c 22 # d e f i n ex l P o r t r a i t1 u s eM y T a b l e & &o rS E L E C T*I N T OM y C u r s o r c F i l e N a m e=" M y X L S F i l e " & &o rw h a t e v e r ,i n c l u d i n gp a t h * c o p yt o( c F i l e N a m e )f i e l d s( c F i e l d s )T Y P Ex l s c o p yt o( c F i l e N a m e )T Y P Ex l s *t h e no p e ne x c e la n dm a k et h ed a t al o o kg o o d ,l i k et h i s o E x c e l=C r e a t e O b j e c t ( " E x c e l . A p p l i c a t i o n " ) i fv a r t y p e ( o E x c e l )! =" O " *c o u l dn o ti n s t a n t i a t eE x c e lo b j e c t *s h o wa ne r r o rm e s s a g eh e r e r e t u r n. F . e n d i f *m a k ee x c e lv i s i b l ed u r i n gd e v e l o p m e n t * o E x c e l . v i s i b l e=. T . *o p e nt h ew o r k b o o ky o uj u s tc r e a t e d o E x c e l . S h e e t s I n N e w W o r k B o o k=1 o W o r k b o o k=o E x c e l . W o r k b o o k s . O p e n ( c F i l e N a m e )
www.tek-tips.com/viewthread.cfm?qid=1654224 4/7
8/16/13
*r e n a m et h eS h e e tt ow h a t e v e ry o ul i k e o A c t i v e S h e e t=o E x c e l . A c t i v e S h e e t o A c t i v e S h e e t . N a m e=" M y D a t a " o E x c e l A p p=o E x c e l . A p p l i c a t i o n o E x c e l A p p . W i n d o w S t a t e=x l M a x i m i z e d *f i n da d d r e s so fl a s to c c u p i e dc e l l l c L a s t C e l l=o E x c e l . A c t i v e C e l l . S p e c i a l C e l l s ( x l L a s t C e l l ) . A d d r e s s ( ) *r e s i z ea l lc o l u m n s l n M a r k e r 1=a t ( " $ " , l c L a s t C e l l , 1 ) & &i . e .1w h e nl c L a s t C e l l=" $ A F $ 1 0 5 " l n M a r k e r 2=a t ( " $ " , l c L a s t C e l l , 2 ) & &i . e .4w h e nl c L a s t C e l l=" $ A F $ 1 0 5 " l n S t a r t P o s=l n M a r k e r 1+1 l n S t r L e n=l n M a r k e r 2-l n S t a r t P o s o E x c e l . C o l u m n s ( " A : "+s u b s t r; ( l c L a s t C e l l , l n S t a r t P o s , l n S t r L e n ) ) . E n t i r e C o l u m n . A u t o F i t *y o uc a ne v e na d dan i c ea u t o f o r m a t o E x c e l . R a n g e ( " A "+a l l t r i m ( s t r ( n T O P B L A N K R O W S + 1 ) )+" : "+l c L a s t C e l l ) . S e l e c t o E x c e l . S e l e c t i o n . A u t o F o r m a t ( x l R a n g e A u t o f o r m a t C l a s s i c 2 , . t . , . t . , . t . , . t . , . t . , . t . ) *s e tE x c e lP r i n tA r e a o A c t i v e S h e e t . P a g e S e t u p . P r i n t A r e a=" $ A $ 1 : "+l c L a s t C e l l *d e f i n ep r i n t e dp a g ef o o t e r W i t hl o A c t i v e S h e e t . P a g e S e t u p * . L e f t H e a d e r =" " * . C e n t e r H e a d e r=" " * . R i g h t H e a d e r =" " . L e f t F o o t e r =" & B M yF o o t e rg o e sh e r e & B " . C e n t e r F o o t e r=" & D " . R i g h t F o o t e r =" P a g e& P " * . P r i n t H e a d i n g s =. F . . P r i n t G r i d l i n e s=. F . . C e n t e r H o r i z o n t a l l y=. T . . C e n t e r V e r t i c a l l y=. F . . O r i e n t a t i o n=x l P o r t r a i t e n d w i t h *s a v eE x c e lf i l ei nn e wE x c e lf o r m a t( C O P YT OX L Su s e so l df o r m a t ) o W o r k b o o k . S a v e ( ) *d i s p l a yf i n i s h e dp r o d u c tt ot h eu s e r o E x c e l . v i s i b l e=. T . -A l e xF e l d s t e i n S o m e t i m e st h eL a s tC e l li sn o tu p t o d a t ea f t e rd e l e t i n gar o wi nE x c e l , C a l l i n gA c t i v e S h e e t . U s e d R a n g ea f t e rd e l e t i n gar o ww i l lk e e pL a s tC e l l u p t o d a t e . l o E x c e l=c r e a t e o b j e c t ( ' E x c e l . A p p l i c a t i o n ' ) l o E x c e l . W o r k b o o k s . O p e n ( t c F i l e ) l o E x c e l . R o w s ( " 1 " ) . D e l e t e ( x l U p ) l n L a s t R o w I n c o r r e c t=l o E x c e l . C e l l s . S p e c i a l C e l l s ( x l C e l l T y p e L a s t C e l l ) . R o w l o E x c e l . A c t i v e S h e e t . U s e d R a n g e& &a d dt h i sl i n e l n L a s t R o w C o r r e c t=l o E x c e l . C e l l s . S p e c i a l C e l l s ( x l C e l l T y p e L a s t C e l l ) . R o w -D a v i dF u n g
8/16/13
B o t hf a i lt ot r a n s f e rm e m of i e l d sa n dC S Vm i g h th a v ep r o b l e m sw i t hd a t a t y p e sc o n v e r t e dc o r r e c t l y( m o s t l yw i t hd a t
S i m i l a rt oD a t aT oC l i p ( )y o uc a nc o p yt oat a bd e l i m i t e df i l e ,r e a di ti n t oc l i p b o a r dw i t hF i l e T o S t r ( )a n dp a s t e E x c e l( e s p e c i a l l yn e w e rv e r s i o n s )a l s or e c o g n i z e sX M La n dH T M(t a b l et a g s) .
M yb e s tp r e f e r a n c ei st ot r a n s f e rd a t au s i n gA D Oi n s t e a d .P a s s i n gw i t hA D Ou s e sE x c e l ' so w nV B Ac o m m a n d st o' p a s t
L O C A Lo E x c e l o E x c e l=C r e a t e o b j e c t ( " E x c e l . A p p l i c a t i o n " ) W i t ho E x c e l . W o r k B o o k s . A d d . V i s i b l e=. T . V F P 2 E x c e l ( _ s a m p l e s + ' d a t a \ t e s t d a t a . d b c ' , ' s e l e c t*f r o me m p l o y e e ' , . A c t i v e S h e e t . R a n g e ( ' A 1 0 ' ) ) E n d w i t h f u n c t i o nV F P 2 E x c e l l p a r a m e t e r st c D a t a S o u r c e ,t c S Q L ,t o R a n g e L o c a ll o C o n nA sA d o D B . C o n n e c t i o n ,; l o R SA sA d o D B . R e c o r d s e t , ; i x l o C o n n=C r e a t e o b j e c t ( " A d o d b . c o n n e c t i o n " ) l o C o n n . C o n n e c t i o n S t r i n g=" P r o v i d e r = V F P O L E D B ; D a t aS o u r c e = " + m . t c D a t a S o u r c e l o C o n n . O p e n ( ) l o R S=l o C o n n . E x e c u t e ( m . t c S Q L ) F O Ri x = 1T Ol o R S . F i e l d s . C o u n t t o R a n g e . O f f s e t ( 0 , m . i x 1 ) . V a l u e=P R O P E R ( l o R s . F i e l d s ( m . i x 1 ) . N a m e ) t o R a n g e . O f f s e t ( 0 , m . i x 1 ) . F o n t . B o l d=. t . E N D F O R t o R a n g e . O f f s e t ( 1 , 0 ) . C o p y F r o m R e c o r d S e t (l o R S) l o R s . C l o s e l o C o n n . C l o s e
8/16/13
A b o v ec o d ea l s os h o w sa n o t h e ra l t e r n a t i v et ot r a n s f e r r i n gd a t at oE x c e lv i aa r r a yi n s t e a do fp a s t i n g . C e t i nB a s o z C o n t r i b u t o r s :A l e xF e l d s t e i nT o mC e r u lS t u a r tD u n k e l dD a v i dF u n gC e t i nB a s o z C a t e g o r yC o d eS a m p l e sC a t e g o r yA u t o m a t i o n
E d i tF i n d -R e c e n tC h a n g e sC a t e g o r i e sR o a dM a p[ A F ][ G P ][ Q Z ]N e wT o p i cH o m eS e a r c h : h t t p : / / f o x . w i k i s . c o m-al
-Bart
TamarGranor (Programmer)
11 Aug 11 15:48
Rather than pasting a whole page from the VFP wiki here, why not just link the page: http://fox.wikis.com/wc.dll?Wiki~ExcelAutomation~VFP Tamar
Nifrabar (Programmer)
12 Aug 11 8:32
Tamar, You are right, but I once copied this information and stored it in my archives without storing the exact source-location as well. So I didnot remember where I took it from but nevertheless I found it worth to share this. -Bart
TamarGranor (Programmer)
12 Aug 11 16:51
My suggestion is that when you do that, you paste the URL in your archive as well. It's generally considered rude (or worse) to post the full exact text of another person's web page. Also, pages like that one on a Wiki keep getting updated, so there may be more information there now than when you copied it. Finally, by pointing someone to a page on the VFP Wiki, you introduce them to the tool, one that every VFP developer should know about. Tamar
www.tek-tips.com/viewthread.cfm?qid=1654224
7/7