cs_CanisLupus
Messages postés3757Date d'inscriptionmardi 23 septembre 2003StatutMembreDernière intervention13 mars 200620 8 janv. 2005 à 18:21
Salut,
Pour détecter si Excel est installé sur un poste il y a plusieurs solutions.
D'abord, un test simple avec dir du genre :
pour excel 97
if dir("C:\Program Files\Microsoft Office\Office\Excel.exe") = "" then msgbox "pas installé"
ou pour excel 2002
if dir("C:\Program Files\Microsoft Office\Office10\Excel.exe") = "" then msgbox "pas installé"
Mais ce n'est pas fiable à cause des versions différentes et de la possibilité d'installer ailleurs que sur c: et dans un autre dossier.
Personellement, j'utilise une autre façon (en VB6), je lis dans la registry.
Dans un module, tu colles ça :
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const REG_SZ As Long = 1
Private Const ERROR_SUCCESS = 0&
Private Declare Function RegOpenKey _
Lib "advapi32" Alias "RegOpenKeyA" _
( _
ByVal hKey As Long, _
ByVal lpSubKey As String, _
phkResult As Long _
) As Long
Private Declare Function RegQueryValueEx _
Lib "advapi32" Alias "RegQueryValueExA" _
( _
ByVal hKey As Long, _
ByVal lpValueName As String, _
ByVal lpReserved As Long, _
lpType As Long, _
lpData As Any, _
lpcbData As Long _
) As Long
' Lecture dans la base de registre
Private Function GetString(hKey As Long, strPath As String, strValue As String) As String
Dim Keyhand As Long, datatype As Long, lResult As Long
Dim strBuf As String, lDataBufSize As Long, intZeroPos As Integer
Dim r As Long, lValueType As Long
r = RegOpenKey(hKey, strPath, Keyhand)
lResult = RegQueryValueEx(Keyhand, strValue, 0&, lValueType, ByVal 0&, lDataBufSize)
If lValueType = REG_SZ Then
strBuf = String(lDataBufSize, " ")
lResult = RegQueryValueEx(Keyhand, strValue, 0&, 0&, ByVal strBuf, lDataBufSize)
If lResult = ERROR_SUCCESS Then
intZeroPos = InStr(strBuf, Chr$(0))
If intZeroPos > 0 Then
GetString = Left$(strBuf, intZeroPos - 1)
Else
GetString = strBuf
End If
End If
End If
End Function
Public Function Localisation_Appli(Appli As String) As String
Dim ClsId As String
Dim Localisation_Exe As String
Dim pos As Integer
' essai de récupération de la clé de l'appli
ClsId = GetString(HKEY_CLASSES_ROOT, Appli & "\CLSID", "")
If ClsId <> "" Then
' essai de lecture du chemin de l'appli dans la clé
Localisation_Exe = GetString(HKEY_CLASSES_ROOT, "CLSID" & ClsId & "\LocalServer32", "")
' elimination des paramètres de lancement de l'appli
pos = InStr(Localisation_Exe, "/")
If pos <> 0 Then
Localisation_Exe = Trim(Left$(Localisation_Exe, pos - 1))
End If
End If
Localisation_Appli = Localisation_Exe
End Function
Au cas où Excel aurait été effacé ou mal désinstallé, on peut combiner avec un dir. Dans ton cas, tu peux appeler la fonction comme ça :
If Dir(Localisation_Appli("Excel.Application")) = "" Then MsgBox "Excel pas installé"
cs_CanisLupus
Messages postés3757Date d'inscriptionmardi 23 septembre 2003StatutMembreDernière intervention13 mars 200620 9 janv. 2005 à 19:25
Alors, je ne sais pas, tu n'aurais pas installé +sieurs versions d'excel sans avoir désinstallé les précédentes par hasard ?
Parce que, normalement, une désinstall supprime l'exe de l'appli.
La fonction Localisation_Appli("Excel.Application") renvoie le chemin & nom de l'exe qu'elle trouve dans la registry, en l'occurence "c:\program files\microsoft office\office\excel.exe" pour excel 97 ou
"c:\program files\microsoft office\office10\excel.exe" pour excel 2002. Je n'ai pas essayé avec d'autres versions.
Pose un msgbox Localisation_Appli("Excel.Application") pour voir ce que te renvoie la fonction. Puis va vérifier avec l'explorateur si l'exe est encore là.