Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
How to create cursor from object (GATHER NAME on steroid
Message
De
05/03/2006 11:39:52
Dragan Nedeljkovich (En ligne)
Now officially retired
Zrenjanin, Serbia
 
 
À
05/03/2006 08:21:18
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP1
Divers
Thread ID:
01101568
Message ID:
01101579
Vues:
22
>If you have an object created by some external application (in this case Outlook) and would like to create a table with all properties as fields, what is the most direct way?

I don't see a direct way, for several reasons.

First, because of VFP's late binding of COM objects, aMembers() most probably won't give you much... OOPsss, it does now in 9sp1. Here goes:
#Define hTab Chr(9)
#Define hCR Chr(13)
oEx=Createobject("excel.application")
N=Amembers(a, oEx, 3)  && 3 gives you COM object PEMs

cList=""
cComma=" ("
oEmpty=createobject("empty")
For i=1 To N
	If a[i,2]="PropertyGet"
		cName=a[i,1]
		Try
			lOK=.T.
			uvalue=Getpem(oEx,cName)
		Catch
			lOK=.F.
		Endtry
		If Not lOK
			Loop
		Endif
		cType=Vartype(uvalue)
		Do Case
*-- skip object and null properties
			Case cType$"OX"
				lOK=.F.
			Case 	cType="C"
				nLen=Len(uvalue)
				*-- scheck for null strings, and add space just in case
				nLen=iif(nlen=0, 10, int(nLen*1.5))
				cLen="("+Transform(nLen)+")"
			Case cType$"NIFBY"
*-- do a more precise one if needed... still, how to recognize integers?
				cLen="(18,2)"
			Case cType$"LDT"
				cLen=""
			Otherwise
				cLen=""
		Endcase
		If Not lOK
			Loop
		Endif
		addproperty(oEmpty, cName, uValue)
		cList = cList + cComma+cName + hTab+ cType+" "+cLen
		If i%3=2
			cComma=","+hTab
		Else
			cComma=","+hTab+";"+hCR
		Endif
	Endif
Endfor
cList = cList + ")"
*-- TEXT BLOCK BEGIN
TEXT TO lcPrg NOSHOW TEXTMERGE
lparameters oE
create cursor crsFromExcel <<cList>>
insert into crsFromExcel from name oE
edit
ENDTEXT
*-- TEXT BLOCK END
Strtofile(lcPrg, "cursorFromExcel.prg")
execscript(lcPrg, oEmpty)
Tried, works. At least for Excel.

You see the flaws here - we don't know the exact lengths of the strings, we don't know the exact format of the numbers, we need to do a lot of more tweaking to guess which fields should actually be memos (I figure you want a typical Outlook object, which would contain at least one longish text field), many of the properties are null at the time you build the cursor (and so we don't know whether they'd be strings, objects or what once they're not).

Another thing to beware of - what do you do if there are more than 255 eligible properties? That can easily happen with any rich enough DOM.

Worst of all, you can't Insert Into YourCursor from oEx (or oOL in your case) because that simply doesn't work. Probably because the properties aren't just exposed as in VFP, they're PropertyGet methods, so you have to pull them one by one like I did here. So you'd need a loop like this to just add properties to an empty object, and insert from that.

back to same old

the first online autobiography, unfinished by design
What, me reckless? I'm full of recks!
Balkans, eh? Count them.
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform