VBS - Récuperer la valeur d'une cellule excel

Résolu
didieraucun Messages postés 29 Date d'inscription samedi 30 juin 2007 Statut Membre Dernière intervention 2 janvier 2008 - 27 déc. 2007 à 23:04
didieraucun Messages postés 29 Date d'inscription samedi 30 juin 2007 Statut Membre Dernière intervention 2 janvier 2008 - 30 déc. 2007 à 21:26
Bonsoir,

J'ai créer un fichier VBS pour remplir automatiquement des champs d'un logiciel

Comment faire pour récupérer la valeur d'une cellule d'un fichier Excel ?

 




set shell = WScript.CreateObject("WScript.Shell" )

shell.AppActivate "Easy Access"

WScript.Sleep(3000)  ' Attendre 3 secondes

shell.SendKeys "ZZ30"

shell.SendKeys "~"

WScript.Sleep(2000)  ' Attendre 2 secondes

shell.SendKeys "{DEL}{DEL}{DEL}"

shell.SendKeys "120"

shell.SendKeys "{DOWN}{DOWN}{DOWN}"

shell.SendKeys "76500"

shell.SendKeys "~"

WScript.Sleep(2000)  ' Attendre 2 secondes

shell.SendKeys "{TAB}"

shell.SendKeys "ACC{TAB}"

shell.SendKeys valeur_cellule_excel 'Valeur de la cellule A1 du fichier Toto.xls
<hr size ="2" width="100%" />

7 réponses

cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
30 déc. 2007 à 19:11
 Bonsoir,

Si le fichier Toto.xls ne comporte qu'un onglet:

shell.SendKeys "{TAB}"
shell.SendKeys "ACC{TAB}"
shell.SendKeys ShowCellExcel()

Function ShowCellExcel()
Dim objExcel, objClasseur, ExcelFile, strCell
ExcelFile = "C:\Toto.xls"
Set objExcel = CreateObject("Excel.Application")
Set objClasseur = objExcel.WorkBooks.Open(ExcelFile)
objExcel.DisplayAlerts = False
objExcel.Application.Visible = False
strCell = objExcel.Worksheets(1).Cells(1, 1).Value
objExcel.Quit
Set objExcel = Nothing
Set objClasseur = Nothing
ShowCellExcel = strCell
End Function

jean-marc
3
didieraucun Messages postés 29 Date d'inscription samedi 30 juin 2007 Statut Membre Dernière intervention 2 janvier 2008
30 déc. 2007 à 20:20
Merci Jean marc

Et si je souhaite récuperer d'autres valeurs de cellule A1, D4 et F6
je repete 3 fois le sous programme Function ShowCellExcel()
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
30 déc. 2007 à 20:53
Re,

Il suffit de passer la cellule (ligne et colonne) en paramètre.

MsgBox  ShowCellExcel(1,2),,"B1"
MsgBox  ShowCellExcel(5,4),,"D5"
MsgBox  ShowCellExcel(10,6),,"F10"

Function ShowCellExcel(Ligne,Colonne)
Dim objExcel, objClasseur, ExcelFile, strCell
ExcelFile = "C:\Toto.xls"
Set objExcel = CreateObject("Excel.Application")
Set objClasseur = objExcel.WorkBooks.Open(ExcelFile)
objExcel.DisplayAlerts = False
objExcel.Application.Visible = False
strCell = objExcel.Worksheets(1).Cells(Ligne,Colonne).Value
objExcel.Quit
Set objExcel = Nothing
Set objClasseur = Nothing
ShowCellExcel = strCell
End Function

jean-marc
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
30 déc. 2007 à 21:12
Re,

Evidemment, il est inutile d'appeler trois fois la fonction (ouvrir/fermer le même fichier toto.xls).

shell.SendKeys "{TAB}"
shell.SendKeys "ACC{TAB}"

Dim objExcel, objClasseur, ExcelFile
ExcelFile = "C:\Toto.xls"
Set objExcel = CreateObject("Excel.Application")
Set objClasseur = objExcel.WorkBooks.Open(ExcelFile)
objExcel.DisplayAlerts = False
objExcel.Application.Visible = False

shell.SendKeys objExcel.Worksheets(1).Cells(1,1).Value
shell.SendKeys objExcel.Worksheets(1).Cells(4,4).Value
shell.SendKeys objExcel.Worksheets(1).Cells(6,6).Value

objExcel.Quit
Set objExcel = Nothing
Set objClasseur = Nothing

jean-marc
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
didieraucun Messages postés 29 Date d'inscription samedi 30 juin 2007 Statut Membre Dernière intervention 2 janvier 2008
30 déc. 2007 à 21:21
J'ai modifié comme suit pour ne faire qu'un accès au fichier excel :

set shell = WScript.CreateObject("WScript.Shell")
shell.Run "notepad.exe"

Dim objExcel, objClasseur, ExcelFile, strCell_B1, strCell_D5, strCell_F10
ExcelFile = "C:\Toto.xls"
Set objExcel = CreateObject("Excel.Application")
Set objClasseur = objExcel.WorkBooks.Open(ExcelFile)
objExcel.DisplayAlerts = False
objExcel.Application.Visible = False
strCell_B1 = objExcel.Worksheets(1).Cells(1,2).Value
strCell_D5 = objExcel.Worksheets(1).Cells(5,4).Value
strCell_F10 = objExcel.Worksheets(1).Cells(10,6).Value
objExcel.Quit
Set objExcel = Nothing
Set objClasseur = Nothing

shell.SendKeys strCell_B1
shell.SendKeys "{TAB}"
shell.SendKeys strCell_D5
shell.SendKeys "{TAB}"
shell.SendKeys strCell_F10

Peut etre encore à amélioré
0
didieraucun Messages postés 29 Date d'inscription samedi 30 juin 2007 Statut Membre Dernière intervention 2 janvier 2008
30 déc. 2007 à 21:24
Nos réponse se sont croisées
0
didieraucun Messages postés 29 Date d'inscription samedi 30 juin 2007 Statut Membre Dernière intervention 2 janvier 2008
30 déc. 2007 à 21:26
Nos réponses se sont croisées
0
Rejoignez-nous