IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Outlook Discussion :

Fermer excel depuis userform [OL-2007]


Sujet :

Outlook

  1. #1
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 11
    Points : 3
    Points
    3
    Par défaut Fermer excel depuis userform
    Bonjour à tous,

    J'ai créer une macro capable d'ouvrir excel, et a partir d'un Userform je remplis des cellules d'excel et je modifie les info d'un contact.

    Mon problème et pour fermer le classeur excel lors de la fermeture de mon userform ! Cela ne fonctionne pas !

    Il me dit : Erreur d'éxécution '91' : Variable objet ou variable de bloc with non définie.

    Voici mon code :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
     
    '------------------Fermeture excel---------------------------------
    Dim appExcel As Excel.Application
    Dim wbExcel As Excel.Workbook
     
    Set appExcel = CreateObject("Excel.Application")
    appExcel.Application.DisplayAlerts = False
    appExcel.ActiveWorkbook.Saved = True
    appExcel.ActiveWorkbook.Close
    appExcel.Quit
    appExcel.Visible = True
    appExcel.Application.DisplayAlerts = True
     
     
    '------------------Transfert vers note du contact---------------------------------
    Set myolapp = CreateObject("Outlook.Application")
    Set myinspector = myolapp.ActiveInspector
     
    With myinspector.CurrentItem
    .Body = Existant.note
    End With
    End Sub
    Merci d'avance pour votre aide.

  2. #2
    Invité
    Invité(e)
    Par défaut
    Bonjour

    Je ne comprends pas ton code

    Tu crées une instance d'Excel, tu la sauvegarde et tu la ferme ? Sans rien faire d'autre ?

    Si tu es sur OutLook pourquoi recréer une instance ?

    Philippe

  3. #3
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    Bonjour Philippe,

    Et merci de répondre à mon post.

    Mon problème et peut-être là, de re-créer des instances ?

  4. #4
    Invité
    Invité(e)
    Par défaut
    Re

    Si tu l'as déjà crée pourquoi la recréer, il faut que ta première instance ait été déclaré en publique pour pouvoir la fermer.

    Philippe

  5. #5
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    Re,

    Petite question : comment déclarer en public ?

    Merci

  6. #6
    Invité
    Invité(e)
    Par défaut
    Re

    Montres nous déjà comment et où tu as fait tes déclarations et affectations.

    Philippe

  7. #7
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    Re,

    Voilà, ci dessous le code situé dans un useform appelé "Existant" avec comme macro Initialize et Queryclose. L'ouverture se fait par un bouton dans la barre d'outils personnalisé lorsqu'un contact est affiché :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    Private Sub UserForm_Initialize()
     
    Set myolapp = CreateObject("Outlook.Application")
        Set myinspector = myolapp.ActiveInspector
        With myinspector.CurrentItem
        If .CompanyName = "" Then ref = .LastName & " " & .FirstName Else ref = .CompanyName
        End With
     
    '-------------------------------Création listview-----------------------------------------
    With Existant.ListView1
        .View = lvwReport
        .FullRowSelect = True
    End With
    With Existant.ListView1
        With .ColumnHeaders
            .Clear
            .Add , , "Lots", 100
        End With
    End With
     
    Dim x As Integer
     
    With Existant.ListView1
        .ListItems.Clear
        .GridLines = True
    End With
     
    Existant.ListView1.ListItems.Add , , "Lot 1"
    Existant.ListView1.ListItems.Add , , "lot 2"
     
     
    '-------------------------------Ouverture fichier excel-----------------------------------------
     
            Dim appExcel As Excel.Application    'Application Excel
            Dim wbExcel As Excel.Workbook    'Classeur Excel
            Dim wsExcel As Excel.Worksheet    'Feuille Excel
     
            'Ouverture de l'application
            Set appExcel = CreateObject("Excel.Application")
            appExcel.Visible = True
     
            'Ouverture d'un fichier Excel
            Dim Presence As Boolean
            Presence = False
                For Each w In appExcel.Workbooks
                    If w.Name = "Ratio 2011.xls" Then Presence = True
                Next w
                If Presence = True Then
                Else
                    Set wbExcel = appExcel.Workbooks.Open("C:\Documents\Ratio 2011.xls")
                End If
            'wsExcel correspond à la première feuille du fichier
            Set wsExcel = wbExcel.Worksheets(1)
     
     
    '-------------------------------Alimentation de la listview suivant fichier excel-----------------------------------------
     
    Dim v As Integer
     v = "0"
     
    For x = 4 To appExcel.Workbooks("Ratio 2011.xls").Sheets("Ratio").Range("c65536").End(xlUp).Row
        If appExcel.Workbooks("Ratio 2011.xls").Sheets("Ratio").Cells(x, 3).Value = ref Then
           v = v + 1
            With Existant.ListView1
                With .ColumnHeaders
                    .Add , , "Type", 100
                        Existant.ListView1.ListItems(1).ListSubItems.Add , , "Devis n°" & v
                        Existant.ListView1.ListItems(2).ListSubItems.Add , , "Type :" & appExcel.Workbooks("Ratio 2011.xls").Sheets("Ratio").Cells(x, 4)
     
                    .Add , , "Prix", 50
                        Existant.ListView1.ListItems(1).ListSubItems.Add , , "Année : " & appExcel.Workbooks("Ratio 2011.xls").Sheets("Ratio").Cells(x, 2)
                        Existant.ListView1.ListItems(2).ListSubItems.Add , , "Mois : " & appExcel.Workbooks("Ratio 2011.xls").Sheets("Ratio").Cells(x, 1)
                    .Add , , "Ratio", 50
                        Existant.ListView1.ListItems(1).ListSubItems.Add , , ""
                        Existant.ListView1.ListItems(2).ListSubItems.Add , , ""
                End With
            End With
        End If
    Next x
     
    End Sub
     
    Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
     
    '------------------Fermeture excel---------------------------------
    appExcel.Application.DisplayAlerts = False
    appExcel.ActiveWorkbook.Saved = True
    appExcel.ActiveWorkbook.Close
    appExcel.Quit
    appExcel.Visible = True
    appExcel.Application.DisplayAlerts = True
     
     
    '------------------Transfert vers note du contact---------------------------------
    Set myolapp = CreateObject("Outlook.Application")
    Set myinspector = myolapp.ActiveInspector
     
    With myinspector.CurrentItem
    .Body = Existant.note
    End With
    End Sub

  8. #8
    Invité
    Invité(e)
    Par défaut


    Ton code est dans Excel ou Outlook ?

    Pourquoi ouvrir Excel si tu es dans Excel et pourquoi ouvrir OutLook si tu es dans Outlook ?

    Philippe

  9. #9
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 11
    Points : 3
    Points
    3
    Par défaut


    Re,

    Mon code est dans outlook !

  10. #10
    Membre expert

    Homme Profil pro
    Spécialiste progiciel
    Inscrit en
    Février 2010
    Messages
    1 747
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 37
    Localisation : France, Haute Loire (Auvergne)

    Informations professionnelles :
    Activité : Spécialiste progiciel
    Secteur : Service public

    Informations forums :
    Inscription : Février 2010
    Messages : 1 747
    Points : 3 016
    Points
    3 016
    Par défaut
    Bonjour,

    Réinitialises tes variables quand tu n'en as plus besoin.

    Cordialement,
    Christophe

    Merci de ne pas oublier de mettre résolu quand le sujet l'est. Cela aide tous les DVPnautes dans leur recherche

  11. #11
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    Bonjour Christophe,

    Merci pour t'as réponse, mais rien y fait !

  12. #12
    Candidat au Club
    Profil pro
    Inscrit en
    Mars 2010
    Messages
    11
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2010
    Messages : 11
    Points : 3
    Points
    3
    Par défaut
    Bonjour à tous,

    J'ai trouvé réponse à mon problème :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    appExcel.Application.DisplayAlerts = False
    For Each w In Workbooks
       w.Save
    Next w
    appExcel.Quit
    appExcel.Application.DisplayAlerts = True
    Merci à tous pour votre aide.

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Fermer Excel depuis une macro
    Par Hubs702 dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 12/05/2011, 15h36
  2. [AC-2003] Lancer macro Excel et fermer Excel depuis Access
    Par sinifroth dans le forum VBA Access
    Réponses: 7
    Dernier message: 28/05/2009, 17h14
  3. Fermer un fichier excel depuis word
    Par liop49 dans le forum VBA Word
    Réponses: 6
    Dernier message: 31/08/2007, 15h29
  4. fermer Excel depuis Access
    Par jeanluc065 dans le forum Access
    Réponses: 4
    Dernier message: 27/11/2006, 06h56
  5. comment fermer un fichier Excel depuis Access?
    Par audrey_desgres dans le forum Access
    Réponses: 14
    Dernier message: 21/06/2005, 12h43

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo