vb6 voi excel

Download VB6 Voi Excel

Post on 07-Jul-2015

221 views

Category:

Documents

15 download

Embed Size (px)

TRANSCRIPT

1

Chng 24: Lin kt gia Excel vi Visual Basic 6.0Chng ny c trch t cun Lp trnh VBA trong Excel (Nh xut bn thng k)http://www.giaiphapexcel.com/forum/showthread.php?t=22105

Trc khi tm hiu mi quan h ny, bn bit rng Visual Basic 6.0 (VB 6.0) c xy dng bi tp on Microsoft. Hin ny VB 6.0 c thay th bi VB.NET, tuy nhin VB 6.0 vn l phn mm c nhiu nh lp trnh s dng rng ri nht trn th gii. VB 6.0 mnh m hn ngi anh em VBA v VB 6.0 l ngn ng lp trnh hot ng mang tnh c lp. Nhn chung ni dung, cu trc, m lnh trong th tc ca VB 6.0 v VBA rt ging nhau. V vy, nhng ngi am hiu v VBA th c th nhanh chng tip cn v s dng VB 6.0. Chng ny s ch dn bc u to mi lin kt gia Excel vi VB 6.0 v nhng l do ti sao s dng VB 6.0 cho cc d n VBA ca bn. VB 6.0 c th to ra hn 6 kiu ng dng, nhng ch c hai kiu lin kt c vi Excel l ActiveX DLL v Standard EXE. Mc ny s hng dn bn cch s dng ActiveX DLL v Standard EXE trong VB 6.0 cho Excel vi ng dng n gin Hello World. Chng ta s khm ph s lin kt gia Excel v Standard EXE trong mc tip theo. Nh vy bn s thc mc ti sao cn phi s dng VB 6.0 trong khi VBA sn c trong Excel. Di y l nhng nt chnh bn quyt nh c nn s dng VB 6.0 lin kt vi Excel hay khng?! - Kh nng bo mt code: VBA c chc nng bo mt code chng ngi khc c th xem d n VBA ca bn (xem mc 11.2). iu l cn thit khi bn b cng sc xy dng sn phm ca mnh. Tuy nhin hin nay c rt nhiu chng trnh c th d tm v ph c kha. Chng trnh ca bn lc rt d b phn tn mi ngi s dng ngoi tm kim sot. Nguyn nhn l do VBA khng bin dch hay m ho c code, iu khng th ngn chn c ngi khc truy cp vo. VB 6.0 kh nng bin dch thnh th vin lin kt ng (Dynamic Link Library - vit tt l DLL), chng trnh to ra t VB 6.0 hot ng c lp (Standard EXE), do kh nng bo mt s cao hn. - S dng VB 6.0 Form nng cao: iu khin xy dng trong Form ca VB 6.0 phong ph hn so vi VBA. i tng Form c to trong ng dng Excel c gi l MSForm, cn trong VB 6.0 th c hiu l Ruby Form. S ging nhau v bn ngoi gia chng l giao din chng trnh, Form bn c th xy dng, iu khin cc i tng trong Form. Ngoi ra cc iu khin trong c hai kiu u s dng lp trnh s kin xy ra vi chng

Tc gi: Phan T Hng (tuhuongdcct36@gmail.com)- B mn a k thut Trng H Kin trc HN

2

Chng 24: Lin kt gia Excel vi Visual Basic 6.0thi hnh th tc. ng nhin gia chng c s khc nhau, v d nh thuc tnh, phng thc, s kin v k c cc iu khin ca VB 6.0 phong ph hn,... - H tr iu khin ActiveX tt hn: Khng ch mi VB 6.0 Form cung cp kh nng h tr tt hn so vi UserForm ca Excel. VB 6.0 cn cung cp hng trm iu khin ActiveX nhm 3 m khng c y UserForm ca Excel. Ngoi ra kh nng iu khin mng d liu lin lin kt c tng cng hn so vi VBA. ng dng Active DLL Hello World di y s gii thch mt cch to lin kt t Excel ti DLL. Excel s lin kt vi DLL v DLL s lin kt tr li vi Excel. Tip theo chng ta s nghin cu cch s dng Form ca VB 6.0 nh l UserForm sn c trong Excel.

24.1. To d n ActiveX DLLKhi m chng trnh VB 6.0 th ca s New Project hin ra nh hnh 24-1. Trong trng hp ca s New Project khng hin ra, bn vo menu File/New Project. Ti ca s New Project chn kiu d n ActiveX DLL. Sau khi bn la chn kiu d n v bm vo nt Open, VB 6.0 s to ra mt d n mi ActiveX DLL.

Tc gi: Phan T Hng (tuhuongdcct36@gmail.com)- B mn a k thut Trng H Kin trc HN

3

Chng 24: Lin kt gia Excel vi Visual Basic 6.0Hnh 24-1: Ca s New Project ca VB 6.0

Chng ta s xy dng ng dng Hello World u tin, ActiveX DLL s ch cha hai phn Project (d n) v Class Module (hnh 24-2). D n s xc nh tn ca ng dng trong DLL, v Class Module s th hin cc tnh nng ca DLL trong cc chng trnh khc. Hnh 24-2 th hin cu trc ca mt d n mi ActiveX DLL trong ca s Project. Nu thy ca s Project xut hin trng rt quen thuc, iu khng c nh hng g c. Bn s tm thy mi trng gia VB 6.0 v VBA ging nhau n mc d gy nn s xo trn khi lm vic vi chng. iu s gip bn d dng lm vic vi VB 6.0 khi bn c kinh nghim v VBA. Chng ta s dng ca s Project trong VB 6.0 gn nh tng t vi VBA.

Hnh 24-2: D n lc u

Hnh 24-3: D n t tn

Hnh 24-4: Lu Class Module HelloWorld di dng file Class Files (*.cls)

Hy thay i tn ca Project thnh AFirstProject, tn ca Class1 thnh HelloWorld bng cch g vo trong hp Name ca ca s Properties ca chng nh hnh 24-6. Kt qu thayTc gi: Phan T Hng (tuhuongdcct36@gmail.com)- B mn a k thut Trng H Kin trc HN

4

Chng 24: Lin kt gia Excel vi Visual Basic 6.0i th hin ti hnh 24-3. Bn s thy tn file trong ngoc n nm pha bn ngoi l thnh phn trong d n, l mt s khc nhau gia VBA v VB 6.0. Trong VBA, cc thnh phn ca d n c lu bn trong file ring ca Excel. Trong VB 6.0, tt c cc thnh phn c lu gi trong cc file ring bit ca d n. Sau bn lu gi tn d n li vi hai phn Project (d n) v Class Module (hnh 24-6) vi tn mc nh t. Bn s thy khi d n c lu th tn d n vi ui xc nh s hin th trong ca s Project (hnh 24-5).

Hnh 24-5: Lu d n AFirstProject di dng file Project Files (*.vbp)

24.1.1. Cch to lin kt n gin ActiveX DLL mt hngBy gi chng ta s tm hiu cu trc ca d n ActiveX DLL hon chnh, hy b sung th tc thc hin cng vic no . Trong d n Hello World, DLL s cho hin hp thng bo vi ni dung Hello World! khi thc hin lnh. Vic s c hon thnh khi bn thm phng thc ti Class Module HelloWorld, chnh l th tc s hin th hp thng bo khi c gi. Vic thm phng thc ti VB 6.0 Class Module s lm vic tng t nh trong VBA. Bn bm p chut vo Class Module HelloWorld trong ca s Project, ca s son code hin ra. Th tc ShowMessage nh sau (hnh 24-6):Public Sub ShowMessage() MsgBox "Hello World!", vbInformation End Sub

Tc gi: Phan T Hng (tuhuongdcct36@gmail.com)- B mn a k thut Trng H Kin trc HN

5

Chng 24: Lin kt gia Excel vi Visual Basic 6.0Cng vic tht n gin! By gi chng ta cn phi bin dch ActiveX DLL ca bn v gi ra c t ng dng Excel hin th li cho . Nhng trc ht hy bin dch code . u tin vo menu File trong ca s VB 6.0, chn Make AFirstProject.dll... (hnh 24-7). Ca s Make Project hin ra nh hnh 24-8, chn nt OK to AFirstProject.dll trong th mc cha d n ca bn.

Hnh 24-6: To th tc ShowMeesage trong Class Modules HelloWorld

Tip theo bn s lm g trong Excel? Cng vic rt d dng khin bn phi ngc nhin. Bc u tin khi ng Excel, sau bn m ca s VBE. Vo menu Tools/References..., ca s References - VBAProject hin ra nh hnh 24-9. Tm d n AFirstProject.dll trong danh sch Available References v chn. Nu d n khng xut hin trong danh sch th bn bm no nt Browse... tm kim ni lu tr, sau bm OK xc nhn.

Tc gi: Phan T Hng (tuhuongdcct36@gmail.com)- B mn a k thut Trng H Kin trc HN

6

Chng 24: Lin kt gia Excel vi Visual Basic 6.0

Hnh 24-7: To DLL cho d n AFirstProject

Tc gi: Phan T Hng (tuhuongdcct36@gmail.com)- B mn a k thut Trng H Kin trc HN

7

Chng 24: Lin kt gia Excel vi Visual Basic 6.0Hnh 24-8: t tn cho d n DLL ca bn

Hnh 24-9: Xc nhn d n AFirstProject trong file

Hnh 24-10: D n ca bn c to trong th mc VB6.0 vi y cc thnh phn

Sau lu workbook vi tn Hello.xls trong th mc cha d n , vic lu trong cng th mc gip bn d dng tm kim v s dng ch khng phi bt buc. Trong thc t, DLL c th s dng mi ni trong my tnh. Cng vic tip theo l thm module vo trong ca s VBE v xy dng th tc ShowDLLMessage nh sau:Public Sub ShowDLLMessage() Tc gi: Phan T Hng (tuhuongdcct36@gmail.com)- B mn a k thut Trng H Kin trc HN

8

Chng 24: Lin kt gia Excel vi Visual Basic 6.0'Khai bo bin i tng Dim HelloWorld As AFirstProject.HelloWorld Set HelloWorld = New AFirstProject.HelloWorld 'Thc hin th tc ShowMessage trong class HelloWorld HelloWorld.ShowMessage Set HelloWorld = Nothing End Sub

Ban u, bn s thy cng vic kh khn khi s dng cc th tc xy dng trong DLL. Tuy nhin khi hiu r phng thc s dng DLL bng VBA, bn s d dng thc hin c. Trong v d trn, cc i tng trong DLL c nhn bit trong Excel nh sau: Bng 24-1: M t i tng v chc nng ca n trong DLL i tng AfirstProject M t L th vin qun l cc i tng trong d n AFirstProject, d n ny qun l cc Classe Module. VBA t ng nhn bit bng chc nng Auto List Members (hnh 24-11, 24-12) L Classe Module cha trong d n AFirstProject (hnh ). Mt d n s c mt hay nhiu Classe Module. VBA t ng nhn bit bng chc nng Auto List Members (hnh 24-13). L th tc cha trong Classe Module HelloWorld. Mi Classe Module c th cha mt hay nhiu th tc.VBA t ng nhn bit bng chc nng Auto List Members (hnh 24-14).

HelloWorld

ShowMessage

Tc gi: Phan T Hng (tuhuongdcct36@gmail.com)- B mn a k thut Trng H Kin trc HN

9

Chng 24: Lin kt gia Excel vi Visual Basic 6.0Hnh 24- 11: Th vin AFirstProject v cc i tng cha bn trong

Hnh 24-12: VBA t nhn bit th vin AFirstProject bng chc nng Auto List Memmbers

Hnh 24-13: VBA t nhn bit Classe HelloWorld bng chc nng Auto List Memmbers

Hnh 24-14: VBA t nhn bit th tc ShowMessage trong Classe HelloWorld

Khi thc thi th tc ShowDLLMessage, kt qu th hin nh ti hnh 24-15. Bn s thy kt qu to hp nhn tin ny tng t khi s dng VBA.

Tc gi: Phan T Hng (tuhuongdcct36@gmail.com)- B mn a k thut Trng H Kin trc HN

10

Chng 24: Lin kt gia Excel vi Visual Basic 6.0

Hnh 24-15: Thng bo c th hin khi bn chy th tc ShowDLLMessage

V mt iu ng quan tm na l tiu (title) trong hp nhn tin. Nu khng cung cp tn tiu , hp nhn tin s th hin tn tiu mc nh, l tn ca d n ang s dng (AFirstProject). V d nu bn mun hp nhn tin hin th Control Excel thanh tiu th bn b sung ni dung Control Excel nh di y:MsgBox "Hello World!", vbInformation, "Control Excel"

24.1.2. Cch to lin kt hon thin ActiveX DLL hai hngD n u tin cho thy cch to ActiveX DLL kh n gin. Kt qu Excel c th thc lin kt vi VB6 qua DLL. Nhng vic trao i trn b gii hn bi v tt c cc lin kt ch l mt hng, Excel gi DLL. nng cao kh nng lin kt gia Excel vi VB 6.0, phi to ra cu trc m cho php lin kt c truyn theo c hai hng. Trong d n Hello World trn, chng ta m rng ng dng c th lin kt theo c hai hng, trong DLL l i tng chnh cc thnh phn tham chiu ti. DLL khng th to ra cc hnh ng trong n m ch p ng nhng yu cu t cc ng dng khc s dng n lm vic. Sau yu cu s c thc hin bi v DLL c th lin kt trc tip vi ng dng gi n ra. Vic u tin chng ta s lm cho c th lin kt hai hng gia Excel v DLL bng cch cung cp DLL