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

Macros et VBA Excel Discussion :

Création de userform à la volée et editeur vba


Sujet :

Macros et VBA Excel

  1. #1
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Août 2005
    Messages
    96
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Territoire de Belfort (Franche Comté)

    Informations forums :
    Inscription : Août 2005
    Messages : 96
    Par défaut Création de userform à la volée et editeur vba
    Bonjour, je viens de créer un userform, ses boutons et ses macros à la volée, et je m'aperçois que l'éditeur vba s'ouvre au même moment, permettant à l'utilisateur de voir le code généré.

    Comment rendre cette création transparente ?

    merci d'avance

    nota : sous excel 2000

  2. #2
    Expert confirmé
    Avatar de fring
    Homme Profil pro
    Engineering
    Inscrit en
    Février 2008
    Messages
    3 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : Belgique

    Informations professionnelles :
    Activité : Engineering

    Informations forums :
    Inscription : Février 2008
    Messages : 3 900
    Par défaut
    Bonjour,

    Si je comprend bien, tu crées un USF et ses boutons via une macro ?
    Si c'est le cas, tu peux montrer le code stp ? Cela aidera à dépatouiller le schmilblick.

  3. #3
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Août 2005
    Messages
    96
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Territoire de Belfort (Franche Comté)

    Informations forums :
    Inscription : Août 2005
    Messages : 96
    Par défaut
    voici une partie du 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
    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
     
    Sub usf()
    Dim X As Object
    Dim i As Integer
    Dim VBComp As VBComponent
     
    Set X = creation_usf()
     
    Worksheets("Config").Range("A3").Value = X.Name
    X.Show
     
    usf = Worksheets("Config").Range("A3").Value
    Set VBComp = ThisWorkbook.VBProject.VBComponents(usf)
    ThisWorkbook.VBProject.VBComponents.Remove VBComp
    Set VBComp = Nothing
    Worksheets("Chrono").Activate
    End Sub
     
    Function creation_usf() As Object
    Dim ObjCommandButton As Object
    Dim ws As Worksheet
     
    Set ws = Worksheets("Config")
    ' Création du UserForm
    Set usf = ThisWorkbook.VBProject.VBComponents.Add(3)
        With usf
           .Properties("Width") = Parent.Width * 0.98 
           .Properties("Height") = Parent.Height * 0.98 
           .Properties("ShowModal") = True
        End With
     
     
    Set ObjCommandButton = usf.Designer.Controls.Add("Forms.CommandButton.1")
        With ObjCommandButton
            .Name = "AddChrono"
            .Caption = "Ajouter au chrono"
            .Width = Parent.Width * 0.98 * 0.5
            .Top = 1 ' Parent.Height * 0.98 * 0.9
            .Left = Parent.Width * 0.98 - .Width - 5
            .Height = 18
        End With
     
    With usf.CodeModule
    ' Initialize
            .CreateEventProc "Initialize", "UserForm"
            j = .ProcStartLine("Userform_Initialize", 0)
            .InsertLines j + 2, "  dHeight = Me.Height * 0.98"
    ' AddChrono
            .CreateEventProc "Click", "AddChrono"
            j = .ProcStartLine("AddChrono_Click", 0)
            .InsertLines j + 2, "  dim msg as variant" & vbCrLf & _
            "  msg = inputbox(""Info à ajouter au chrono :"" ,""Inputbox Perso"") " & vbCrLf & _
            "  if msg <> """" then " & vbCrLf & _
            "     call logEtat(""'** INFO **"",cstr(msg)) " & vbCrLf & _
            "  end if"
    End With
    VBA.UserForms.Add (usf.Name)
    Set creation_usf = UserForms(UserForms.Count - 1)
     
    End Function
    c'est là une version très épurée...
    qui permet au moins de voir l'appel de la fonction de création du userform...

  4. #4
    Expert confirmé
    Avatar de fring
    Homme Profil pro
    Engineering
    Inscrit en
    Février 2008
    Messages
    3 900
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 62
    Localisation : Belgique

    Informations professionnelles :
    Activité : Engineering

    Informations forums :
    Inscription : Février 2008
    Messages : 3 900
    Par défaut
    c'est là une version très épurée...
    sans doute un peu trop épurée parce que j'ai des bug de tous les côtés

    En me basant sur le tuto de SilkyRoad j'ai un peu adapté ton code et la création/suppression du UserForm se déroule sans encombre.
    A partir de là essaye de le réadapter à tes besoins
    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
    Option Explicit
    Dim usf As Object
     
    Sub Start_Procedure()
    Dim X As Object
    Dim i As Integer
    Dim strCdeBut As String
     
    strCdeBut = "CommandButton1"
    Set X = creation_usf(strCdeBut)
     
    X.Show
     
    ThisWorkbook.VBProject.VBComponents.Remove usf
    Set usf = Nothing
    End Sub
     
    Function creation_usf(nomBouton As String) As Object
    Dim ObjCommandButton As Object
    Dim ws As Worksheet, j As Integer
     
    'Set ws = Worksheets("Config")
    ' Création du UserForm
    Set usf = ThisWorkbook.VBProject.VBComponents.Add(3)
        With usf
           .Properties("Width") = Parent.Width * 0.98
           .Properties("Height") = Parent.Height * 0.98
        End With
     
     
    Set ObjCommandButton = usf.Designer.Controls.Add("Forms.CommandButton.1")
        With ObjCommandButton
            .Name = "AddChrono"
            .Caption = "Ajouter au chrono"
            .Width = Parent.Width * 0.98 * 0.5
            .Top = 1 ' Parent.Height * 0.98 * 0.9
            .Left = Parent.Width * 0.98 - .Width - 5
            .Height = 18
        End With
     
    With usf.CodeModule
    ' Initialize
            .CreateEventProc "Initialize", "UserForm"
            j = .ProcStartLine("Userform_Initialize", 0)
            .InsertLines j + 2, "  dHeight = Me.Height * 0.98"
    ' AddChrono
            .CreateEventProc "Click", "AddChrono"
            j = .ProcStartLine("AddChrono_Click", 0)
            .InsertLines j + 2, "  dim msg as variant" & vbCrLf & _
            "  msg = inputbox(""Info à ajouter au chrono :"" ,""Inputbox Perso"") " & vbCrLf & _
            "  if msg <> """" then " & vbCrLf & _
            "     call logEtat(""'** INFO **"",cstr(msg)) " & vbCrLf & _
            "  end if"
    End With
    VBA.UserForms.Add (usf.Name)
    Set creation_usf = UserForms(UserForms.Count - 1)
     
    End Function

  5. #5
    Membre confirmé
    Homme Profil pro
    Inscrit en
    Août 2005
    Messages
    96
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Territoire de Belfort (Franche Comté)

    Informations forums :
    Inscription : Août 2005
    Messages : 96
    Par défaut
    En fait, la création et suppressions se déroulent sans encombre...
    le seul hic est le fait de voir s'afficher et rester l'éditeur VBA au lancement du userform.

    un essai de ton code me donne le même résultat, l'éditeur VBA s'ouvre et reste visible (même si il est en arrière plan).

    j'ai continué les essais et si je protège le projet VBA par un mot de passe, la création du userform ne peut plus se faire....

  6. #6
    Membre émérite

    Profil pro
    Inscrit en
    Mai 2007
    Messages
    514
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2007
    Messages : 514
    Par défaut
    Bonjour,

    Essaye ceci:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    Application.VBE.MainWindow.Visible = False
    Cordialement,

    Tirex28/

Discussions similaires

  1. Création de classes à la volée...
    Par ouiffi dans le forum Langage
    Réponses: 8
    Dernier message: 16/12/2005, 18h01
  2. [ImageMagick] Création d'image à la volée
    Par gdawirs dans le forum Bibliothèques et frameworks
    Réponses: 4
    Dernier message: 21/11/2005, 15h53
  3. Réponses: 4
    Dernier message: 28/10/2005, 20h58
  4. [JMenuBar] Création d'objets à la volée
    Par Rampa dans le forum Composants
    Réponses: 5
    Dernier message: 29/06/2005, 13h56
  5. [IB][IBQUERY][D7 pro] Création de Triggers à la volée.
    Par N1bus dans le forum Bases de données
    Réponses: 6
    Dernier message: 13/10/2004, 14h23

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