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

Access Discussion :

MsgBox personnalisé en VBA [AC-2010]


Sujet :

Access

  1. #1
    Membre expert
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Octobre 2012
    Messages
    1 869
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Service public

    Informations forums :
    Inscription : Octobre 2012
    Messages : 1 869
    Points : 3 448
    Points
    3 448
    Par défaut MsgBox personnalisé en VBA
    Bonjour,

    Après plusieurs recherche je ne trouve pas de solution à mon problème. Est-il possible de modifier le dernier bouton d'un MsgBox? Que je choisisse n'importe quel type de MsgBox le dernier bouton n'est jamais modifiable (accessible), du moins je n'ai pas trouvé comment...

    Nom : MsgBox.PNG
Affichages : 7823
Taille : 25,7 Ko


    Voici le code (adapté d'un forum Excel: excelabo)
    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
    Public Function fuMsgBox_ChangeBtnText()
      Const WH_CBT = 5
      Const Title As String = "Vérification de l'enveloppe"
      Const msg As String = "Est-ce que l'enveloppe est présente?" & vbCrLf & "Si la réponse est non, ignorer!"
      Dim Reply As Integer
     
      BtnTitle(1) = "Commune": BtnTitle(2) = "Oui": BtnTitle(3) = "Non"
      'set up the hook
      msgHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxTitleBtnProc, hInstance, GetCurrentThreadId())
      'call  function
      Reply = MsgBox(msg, vbAbortRetryIgnore + vbQuestion, Title)
      If Reply = vbAbort Then
        fuMsgBox_ChangeBtnText = "Commune"
      ElseIf Reply = vbRetry Then
        fuMsgBox_ChangeBtnText = "Oui"
      Else
        fuMsgBox_ChangeBtnText = "Non"
      End If
      Erase BtnTitle
    End Function
     
    Private Function MsgBoxTitleBtnProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
      Const HCBT_ACTIVATE = 5
      Const GW_CHILD = 5
      Const GW_HWNDNEXT = 2
      If nCode < 0 Then
        MsgBoxTitleBtnProc = CallNextHookEx(msgHook, nCode, wParam, lParam)
        Exit Function
      End If
      If nCode = HCBT_ACTIVATE Then
        Dim hWndChild As Long, hWndChildD As Long
        'premier bouton: Abort button
        hWndChildD = GetWindow(wParam, GW_CHILD)
        'changer pour Commune
        Call SetWindowText(hWndChildD, BtnTitle(1))
        'deuxième bouton: Retry button
        hWndChild = GetWindow(hWndChildD, GW_HWNDNEXT)
        'changer pour Oui
        Call SetWindowText(hWndChild, BtnTitle(2))
        'Ici je voudrais changer le troisième bouton
        'Je ne trouve pas l'adresse, semble ne pas exister
        '.....................................
        'Release the hook
        UnhookWindowsHookEx msgHook
      End If
      ' processing...
      MsgBoxTitleBtnProc = False
    End Function
    Merci à l'avance

    Robert
    Ce qui se conçoit bien s’énonce clairement et les mots pour le dire arrivent aisément. Nicolas Boileau
    Si tout est OK, n'oubliez pas de cliquer sur

  2. #2
    Expert éminent
    Avatar de LedZeppII
    Homme Profil pro
    Maintenance données produits
    Inscrit en
    Décembre 2005
    Messages
    4 485
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Yvelines (Île de France)

    Informations professionnelles :
    Activité : Maintenance données produits
    Secteur : Distribution

    Informations forums :
    Inscription : Décembre 2005
    Messages : 4 485
    Points : 7 759
    Points
    7 759
    Par défaut
    Bonjour,

    J'ai essayé comme ceci:
    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
      If ncode = HCBT_ACTIVATE Then
        Dim hWndChild As Long, hWndChilNext As Long
        'premier bouton: Abort button
        hWndChild = GetWindow(wParam, GW_CHILD)
        'changer pour Commune
        Call SetWindowText(hWndChild, BtnTitle(1))
     
        'deuxième bouton: Retry button
        hWndChilNext = GetWindow(hWndChild, GW_HWNDNEXT)
        hWndChild = hWndChilNext
        'changer pour Oui
        Call SetWindowText(hWndChild, BtnTitle(2))
     
        'Ici je voudrais changer le troisième bouton
        'Je ne trouve pas l'adresse, semble ne pas exister
        '.....................................
        hWndChilNext = GetWindow(hWndChild, GW_HWNDNEXT)
        hWndChild = hWndChilNext
        'changer pour Oui
        Call SetWindowText(hWndChild, BtnTitle(3))
     
      End If
    Pour moi ça fonctionne (je suis en Access 2007).

    Remarque: j'ai du déplacer UnhookWindowsHookEx msgHook dans la fonction fuMsgBox_ChangeBtnText(), après le MsgBox, pour que ça fonctionne.
    Là où il était placé, ça ne fonctionnait pour aucun des trois boutons chez moi.

    A+

  3. #3
    Membre expert
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Octobre 2012
    Messages
    1 869
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Service public

    Informations forums :
    Inscription : Octobre 2012
    Messages : 1 869
    Points : 3 448
    Points
    3 448
    Par défaut
    Bonjour LedZeppII,

    C'est exactement ça.

    Chez moi la fonction est ok avec 2010 sauf qu'il ne faut pas être en mode debug.

    Mille fois merci!!!
    Ce qui se conçoit bien s’énonce clairement et les mots pour le dire arrivent aisément. Nicolas Boileau
    Si tout est OK, n'oubliez pas de cliquer sur

  4. #4
    Rédacteur/Modérateur
    Avatar de Jeannot45
    Homme Profil pro
    Retraité
    Inscrit en
    Octobre 2004
    Messages
    3 871
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 75
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2004
    Messages : 3 871
    Points : 8 489
    Points
    8 489
    Par défaut
    J'ai lu avec beaucoup d’intérêt ton problème concernant la personnalisation de la MsgBox

    Je me permets de te contacter par MP car le sujet étant clos, je ne savais pas si je pouvais le rouvrir afin de compléter le sujet pour les autres membres.
    Si tu le souhaites, je peux rouvrir le fil et continuer le sujet.

    Voici donc ma problématique :
    J'ai récupéré ton code. Deux soucis émergent :

    Comment appeler ces fonctions dans une procédures pour personnaliser mon MsgBox
    Récupère-t-on automatiquement les MsgBox stantdard en suite ?



    Merci beaucoup pour ta contribution
    Merci
    Jeannot

    Liens Office indispensables à visiter: Cours (Tutos), F.A.Q., Sources VBA

    Ne posez pas de questions par MP, je n'ai pas le temps d'y répondre

  5. #5
    Membre expert
    Homme Profil pro
    Consultant informatique
    Inscrit en
    Octobre 2012
    Messages
    1 869
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Canada

    Informations professionnelles :
    Activité : Consultant informatique
    Secteur : Service public

    Informations forums :
    Inscription : Octobre 2012
    Messages : 1 869
    Points : 3 448
    Points
    3 448
    Par défaut
    Bonjour Jean,

    Pour la deuxième question, oui on récupère les messages standards pas la suite.

    Voici le code complet avec différents tests. J'ai modifié la fonction tel que discuté en MP pour que celle-ci prenne certains arguments.

    Premièrement un module standard que j'ai nommé Mod_Env:
    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
    Option Compare Database
    Option Explicit
     
    'Fonctions windows
    Private Declare Function GetCurrentThreadId Lib "kernel32" () As Long
    Private Declare Function SetWindowsHookEx& Lib "USER32" Alias "SetWindowsHookExA" (ByVal idHook&, ByVal lpfn&, ByVal hmod&, ByVal dwThreadId&)
    Private Declare Function UnhookWindowsHookEx& Lib "USER32" (ByVal hHook&)
    Private Declare Function CallNextHookEx Lib "USER32" (ByVal hHook As Long, ByVal CodeNo As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Declare Function GetWindow Lib "USER32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
    Private Declare Function SetWindowText Lib "USER32" Alias "SetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String) As Long
     
    'Variables
    Private Type CWPSTRUCT
      lParam As Long
      wParam As Long
      Message As Long
      hWnd As Long
    End Type
     
    Private msgHook&, hInstance&
    Private TitleMsgBox As String, strMsg As String, BtnTitle(3) As String
     
    Public Function fuMsgBox_ChangeBtnText(sTitle As String, sMsg As String, sB1 As String, sB2 As String, sB3 As String)
    'Pour personnalisé le titre, le message et les boutons
    'Ici 3 boutons obligatoires.  On pourrait aller plus loin en faisant un select case pour le nombre de bouton,
    'Il faudrait alors ajuster le choix du type de message de base. Ici j'ai choisi vbAbortRetryIgnore parce que le but
    'était d'avoir 3 boutons.
      Const WH_CBT = 5
      Dim Reply As Integer
      'Ici on inscrit les nouveaux titres sur les boutons
      BtnTitle(1) = sB1: BtnTitle(2) = sB2: BtnTitle(3) = sB3
      'set up the hook
      msgHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxTitleBtnProc, hInstance, GetCurrentThreadId())
      'call  function
      Reply = MsgBox(sMsg, vbAbortRetryIgnore + vbQuestion, sTitle)
      'free the hook
      UnhookWindowsHookEx msgHook
      'Selon le bouton choisi si on veut autre chose que les constantes Windows
      'On pourrait passer la réponse dans la fonction pour avoir autre chose que le titre des boutons.
      Select Case Reply
        Case vbAbort
           fuMsgBox_ChangeBtnText = sB1
        Case vbRetry
            fuMsgBox_ChangeBtnText = sB2
        Case vbIgnore
            fuMsgBox_ChangeBtnText = sB3
      End Select
      Erase BtnTitle
      'Test pour voir que les msgbox sont de retour
      MsgBox "Est-ce que les boutons sont de retour!!!", vbAbortRetryIgnore
     
    End Function
     
    Private Function MsgBoxTitleBtnProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
     
      Const HCBT_ACTIVATE = 5
      Const GW_CHILD = 5
      Const GW_HWNDNEXT = 2
      If nCode < 0 Then
        MsgBoxTitleBtnProc = CallNextHookEx(msgHook, nCode, wParam, lParam)
        Exit Function
      End If
        If nCode = HCBT_ACTIVATE Then
        Dim hWndChild As Long, hWndChildNext As Long
        'premier bouton: Abort button
        hWndChild = GetWindow(wParam, GW_CHILD)
        'changer pour sB1
        Call SetWindowText(hWndChild, BtnTitle(1))
        'deuxième bouton: Retry button
        hWndChildNext = GetWindow(hWndChild, GW_HWNDNEXT)
        hWndChild = hWndChildNext
        'changer pour sB2
        Call SetWindowText(hWndChild, BtnTitle(2))
        'Troisième bouton: Ignore
        hWndChildNext = GetWindow(hWndChild, GW_HWNDNEXT)
        hWndChild = hWndChildNext
        'changer pour sB3
        Call SetWindowText(hWndChild, BtnTitle(3))
     
      End If
      ' processing...
      MsgBoxTitleBtnProc = False
     
    End Function
    Puis on appelle cette fonction comme suit:
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    Private Sub btnMessage_Click()
     
        Dim strReponse As String
     
        strReponse = fuMsgBox_ChangeBtnText("Ici le titre", "Ici le message", "Bouton # 1", "Bouton # 2", "Bouton # 3")
        MsgBox "Vous avez choisi :  " & strReponse
     
    End Sub
    Les tests ont été faits avec Access 2010.

    En complément, le changement du titre des boutons ne fonctionne pas en mode pas à pas.

    Bonne journée
    Ce qui se conçoit bien s’énonce clairement et les mots pour le dire arrivent aisément. Nicolas Boileau
    Si tout est OK, n'oubliez pas de cliquer sur

  6. #6
    Rédacteur/Modérateur
    Avatar de Jeannot45
    Homme Profil pro
    Retraité
    Inscrit en
    Octobre 2004
    Messages
    3 871
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 75
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : Retraité
    Secteur : Enseignement

    Informations forums :
    Inscription : Octobre 2004
    Messages : 3 871
    Points : 8 489
    Points
    8 489
    Par défaut




    La réponse est complète et ça fonctionne à merveille

    Merci pour cette très intéressante contribution
    Jeannot

    Liens Office indispensables à visiter: Cours (Tutos), F.A.Q., Sources VBA

    Ne posez pas de questions par MP, je n'ai pas le temps d'y répondre

  7. #7
    Nouveau membre du Club
    Homme Profil pro
    activités diverses et variées
    Inscrit en
    Juillet 2013
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : activités diverses et variées

    Informations forums :
    Inscription : Juillet 2013
    Messages : 25
    Points : 28
    Points
    28
    Par défaut bonjour!
    grâce à ce sujet, j'utilise des msgbox avec 3 boutons : "ouvrir", "recopier" et "supprimer"
    ça fonctionne très bien !
    petite question : la croix dans le coin nord/est ne fonctionne pas, ni la touche "escape". Aurai-je un moyen de sortir de ma msgbox?

  8. #8
    Rédacteur/Modérateur
    Avatar de argyronet
    Homme Profil pro
    Panseur de bobos en solutions ETL
    Inscrit en
    Mai 2004
    Messages
    5 123
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Panseur de bobos en solutions ETL
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2004
    Messages : 5 123
    Points : 12 169
    Points
    12 169
    Billets dans le blog
    5
    Par défaut
    Bonjour,

    L'effet de la touche Esc n'est possibe qu'à partir du moment où tu as un Cancel donc soit un vbOkCancel ou un vbYesNoCancel ou encore un vbRetryCancel.

    Argy
    Ce qui donne son sens à la communication, c´est la réponse que l´on obtient. Si vous n´obtenez pas la réponse voulue, communiquez différemment.

    Ils comptent sur vous...
    Web Site@Mail
    Tutoriels : Déployez vos applications Access 2010 à 2019 */* Réalisez un Assistant de présaisie...
    MDB Viewer : Visionneuse Access v4.0
    *** Je recherche des profils (2 ans min.) Java EE, Fullstack, Front, .Net, Mobile... pour CDI ***

  9. #9
    Nouveau membre du Club
    Homme Profil pro
    activités diverses et variées
    Inscrit en
    Juillet 2013
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : activités diverses et variées

    Informations forums :
    Inscription : Juillet 2013
    Messages : 25
    Points : 28
    Points
    28
    Par défaut
    merci pour l'info!

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

Discussions similaires

  1. [XL-2007] désactiver/réactiver un menu personnalisé depuis VBA
    Par Bubar1er dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 22/04/2014, 18h39
  2. [XL-2010] Ruban personnalisé en VBA
    Par Daejung dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 23/12/2011, 10h39
  3. Réponses: 5
    Dernier message: 16/11/2008, 09h06
  4. [Outlook 2003] Parcourir un formulaire personnalisé en VBA
    Par Rikikix dans le forum VBA Outlook
    Réponses: 1
    Dernier message: 02/10/2008, 14h42
  5. comment avoir MsgBox personnalisé
    Par husamus156 dans le forum Général VBA
    Réponses: 1
    Dernier message: 05/06/2007, 17h58

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