[VB6] Problème Excel avec sélection de ligne

Résolu
cs_pong77186 Messages postés 3 Date d'inscription mardi 4 avril 2006 Statut Membre Dernière intervention 4 mai 2006 - 3 mai 2006 à 17:47
cs_pong77186 Messages postés 3 Date d'inscription mardi 4 avril 2006 Statut Membre Dernière intervention 4 mai 2006 - 4 mai 2006 à 11:21
Bonjour tout le monde
J'ai un souci VB6 avec EXCEL qui est assez irritant.
Voila dans un 1er bouton ,j'ouvre un fichier excel dans lequel je sélectionne toutes les lignes non vides.
Et dans un 2ème bouton, je reitère la même chose mais avec un fichier différent.

Voila le code :

Option explicit
Dim appExcel As Excel.Application
================================
Private Sub Command1_Click()
'Déclaration des variables

Dim wbExcel As Excel.Workbook
Dim wsExcel As Excel.Worksheet 'Feuille Excel

'Ouverture de l'application

Set appExcel = CreateObject("Excel.Application")
Set wbExcel = appExcel.Workbooks.Open(App.Path & "\Listing.xls")
Set wsExcel = appExcel.ActiveWorkbook.Worksheets("Feuil1")

appExcel.UserControl = True
appExcel.Application.DisplayAlerts = False
appExcel.Visible = True

With wsExcel

.Rows("4:4").Select
.Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
appExcel.Application.Workbooks("Listing.xls").Save
End With

Set appExcel = Nothing
Set wsExcel = Nothing

End Sub

=======================================
Private Sub Command2_Click()

Dim wbExcel As Excel.Workbook
Dim wsExcel As Excel.Worksheet 'Feuille Excel

'Ouverture de l'application

Set appExcel = CreateObject("Excel.Application")
Set wbExcel = appExcel.Workbooks.Open(App.Path & "\Listing_save.xls")
Set wsExcel = appExcel.ActiveWorkbook.Worksheets("Détail")

appExcel.UserControl = True
appExcel.Application.DisplayAlerts = False
appExcel.Visible = True

With wsExcel

.Rows("4:4").Select
.Range(Selection, ActiveCell.SpecialCells(xlCellTypeLastCell)).Select
appExcel.Application.Workbooks("Listing_save.xls").Save
End With

Set appExcel = Nothing
Set wsExcel = Nothing

End Sub
=======================================
Mon problème est que la sélection pour un 1er fichier fonctionne parfaitement quand je lance command 1, mais lorsque je lance Command 2 j'ai une erreur "une erreur de type variable objet ou variable bloc with non définie" sur la ligne .Range(Selection, ActiveCell.SpecialCells(xlCellTypeLastCell)).Select.
et lorsque je lance command 2 en 1er, c'est sur command 1 que l'erreur se manifeste.

Alors si quelqu'un pouvait m'aider, cela m'aiderait bien
Merci d'avance

4 réponses

valtrase Messages postés 937 Date d'inscription lundi 19 janvier 2004 Statut Membre Dernière intervention 9 mai 2022 3
3 mai 2006 à 19:29
Salut,
Tu devrais tester en utilisant GetObject en lieu et place de CreateObject en faisant ceci tu testes si excel et déjà ouvert donc cela donnerais un truc dans le genre

On Error Resume Next 'Utilisé pour vérifier si Excel s'éxécute


Set appExcel = GetObject(, "Excel.Application") 'Vérifie si Excel est ouvert


If Err.Number <> 0 Then
blRunning = False 'Excel n'était pas en éxecution
Set appExcel = CreateObject("Excel.Application")
Else
blRunning = True
End If
Err.Clear
' ****************************************
ICI TON CODE
' ****************************************

'Si Excel était ouvert on ne le ferme pas
If Not (blRunning) Then
appExcel.Quit
Set appExcel = Nothing
End If
'On libère la mémoire
'Set appExcel = Nothing
Set wbExcelModel = Nothing
Set wbExcelTemp = Nothing

Tu peu toujours aller voir ma source pour plus d'info

Cordialement, Jean-Paul
______________________________________________________________________

Le Savoir n'a de valeur que s'il est partagé
3
fiko81 Messages postés 381 Date d'inscription vendredi 24 septembre 2004 Statut Membre Dernière intervention 5 septembre 2010 3
3 mai 2006 à 18:52
J'ai bien l'impression que tu oublis de fermer ton fichier après ouverture : essai un truc du genre : appExcel.close

Fiko ;-)
0
cs_pong77186 Messages postés 3 Date d'inscription mardi 4 avril 2006 Statut Membre Dernière intervention 4 mai 2006
3 mai 2006 à 19:03
Ca n'apparait pas dans le code, mais j'ai déjà essayé de fermé les applications EXCEL, avec wbExcel.close , et appExcel.application.quit, et j'ai toujours le même souci
0
cs_pong77186 Messages postés 3 Date d'inscription mardi 4 avril 2006 Statut Membre Dernière intervention 4 mai 2006
4 mai 2006 à 11:21
Je te remercie, Jean Paul, tu me retires une épine du pied là.
0
Rejoignez-nous