dridbul2 Maroc Membre Simple # 0000003037 enregistré le 28/01/2011
62 ans DRIDBA Omar 20100 Casablanca de la société SAFILAIT S.A Fiche personnelle
Note des membres pas de note
Contributions > 09 - Automation > Excel
Tester si Excel est lancé
# 0000000780
ajouté le 28/01/2011 12:32:13 et modifié le 28/01/2011
consulté 6950 fois
Niveau
initié
Version(s) Foxpro : VFP 9.0
Description
Avant de lancer Excel, s'assurer qu'il ne l'est pas déjà.
Code source :
FUNCTION Excel PRIVATE ok,loExcel
m.ok=.t. ONERROR ok=.f.
loExcel = GETOBJECT(, "Excel.Application") IF !m.ok
m.ok=.t.
loExcel = createobject('excel.application') ENDIF RETURN ok
Commentaires
le 29/01/2011, Jean à Grenoble a écrit : Si on est sous VFP9, il y a une excellente alternative à ON ERROR : TRY.
En effet, dans cet exemple, il faudrait commencer par mémoriser l'éventuelle commande initiale de ON ERROR, puis remettre cette commande à la fin de la fonction. Il y a beaucoup plus simple : TRY ... CATCH ... ENDTRY
ATTENTION : je ne sais pas ce que je fais comme bétise, mais malgré les espaces mis, j'ai un problème avec l'indentation !
FUNCTION Excel LOCAL ok, loExcel
TRY loExcel = GETOBJECT(, "Excel.Application") ok = .F. CATCH ok = .T. ENDTRY IF !m.ok TRY. loExcel = createobject('excel.application') ok=.t CATCH ok = .F. ENDTRY ENDIF RETURN m.ok
Si on est sous VFP9, il y a une excellente alternative à ON ERROR : TRY.
En effet, dans cet exemple, il faudrait commencer par mémoriser l'éventuelle commande initiale de ON ERROR, puis remettre cette commande à la fin de la fonction.
Il y a beaucoup plus simple : TRY ... CATCH ... ENDTRY
ATTENTION : je ne sais pas ce que je fais comme bétise, mais malgré les espaces mis, j'ai un problème avec l'indentation !
FUNCTION Excel
LOCAL ok, loExcel
TRY
loExcel = GETOBJECT(, "Excel.Application")
ok = .F.
CATCH
ok = .T.
ENDTRY
IF !m.ok
TRY.
loExcel = createobject('excel.application')
ok=.t
CATCH
ok = .F.
ENDTRY
ENDIF
RETURN m.ok