UserForm - Outlook

Bonjour,

J'ai une macro qui reprend les informations d'un UserForm (Textbox et Checkbox) et qui genère un mail via Outlook à partir de ces infos. Aucun problème, pour les Textbox, pour les Checkbox c'est plus compliqué, j'aimerais si elles ne sont pas cochées par l'utilisateur qu'elles n'apparaissent pas dans le corps du mail alors voilà ce que j'ai fait :

If CheckBox1.Value = True Then
MyDoc1 = "xxxxxxxxxxxxxxxxxxxxxxx"
Else: MyDoc1 = ""
End If
If CheckBox2.Value = True Then
MyDoc2 = "xxxxxxxxxxxxxxxxxxxxxxx"
Else: MyDoc2 = ""
End If
If CheckBox3.Value = True Then
MyDoc3 = "xxxxxxxxxxxxxxxxxxx"
Else: MyDoc3 = ""
End If
If CheckBox4.Value = True Then
MyDoc4 = "xxxxxxxxxxxxxxxxxxxxxxxxxx"
Else: MyDoc4 = ""
End If

En faisant comme ça elles n'apparaissent effectivement pas dans le corps du mail ce sont des blancs/Espaces à la place mais comme il y a 36 checkboxs dans mon UserForm, si plusieurs cases ne sont pas cochées à la suite cela fait beaucoup d'espace à corriger manuellement, auriez-vous une idée pour les éviter ?

J'espère que ce n'est pas trop confus.

Merci

Salut Leyt,

Je ne sais pas à quoi correspondent tes

MydocX

mais ce genre de problème dans ta situation ou une autre est (je pense) assez répétitif quand on développe.

Si je comprends bien en fait dans ta macro qui fait ton mail tu reprends sur une ligne tes MydocX donc et quand du coup tu en as par exemple 10 d'affilés sur tes 36, cela te fait donc 10 lignes blanche dans le mail, que tu supprimes manuellement.

Si c'est bien ton problème, ne pourrais tu pas dans ta macro qui te construit le mai refaire un test? du genre

if MyDoc2 <> "" then  "tu ajoute le texte dans le mail"

En gros si ta variable n'est pas vide, tu la mets dans ton mail.

Une autre variante pour ce genre de soucis (mais je pense que cela ne s'applique pas à ton cas vu que j'imagine que tu vas à la ligne entre chaque valeur de tes checkbox, à toi de voir si tu peux t'en servir), est de construire une variable avec les informations à la suite et/ou de rajouter un caractère pour faire un split , en gros:

dim MyDocConstruction as string
MyDocConstruction = ""
If CheckBox1.Value = True Then MyDocConstruction = MyDocConstruction & "BLABLABLA"
If CheckBox2.Value = True Then MyDocConstruction = MyDocConstruction & "BLOBLO"
'puis en une ligne de code tu mets la MyDocConstruction dedans. Après bien-sur il faut faire 
'attention à la longueur de texte que l'on ajoute surtout sur 36 check comme toi, la longueur 
'd'une string n'est pas infinie ^^

Dans ton cas si tu veux vraiment faire une ligne par checkbox dans ton mail, il te faut rajouter un caractère que tu es sur de jamais avoir (celui que tu veux ajouter dans le mail quand la checkbox est true), puis faire un Split pour retrouver chaque élément:

dim MyDocConstruction as string
MyDocConstruction = ""
If CheckBox1.Value = True Then MyDocConstruction = MyDocConstruction & "/" &  "BLABLABLA"
If CheckBox2.Value = True Then MyDocConstruction = MyDocConstruction & "/" & "BLOBLO"
'puis tu fais un 
Split(MyDocConstruction, "/")
' qui va te renvoyer un tableau de chaque élément délimité par "/", tu boucles dessus et tu les ajoutes 
'dans ton mail, je n'ai pas la syntaxe exact pour le split mais sur le net tu trouveras comment l'utiliser.

Par contre en terme technique, je te conseillerais plutôt de passer par un userform.controls qui permet de boucler sur tout tes textbox et checkbox de ton userform pour un gain de temps en général surtout si tu veux rajouter un élément, tu n'auras pas à corriger 36 fois tes lignes de code pour chaque if mais une seule fois dans ta boucle userform.controls, du genre :

'si tu as des textbox de 1 à 10 nommé TextBox1, TextBox2 etc dans un userform de base appelé donc Userform1
For Balayage =  1 to 10
       'le .controls permet de passer en paramètre, ici Balayage le numéro et donc 
       'de boucler sur TextBox1, TextBox2 etc :)
    if Userform1.Controls("TextBox" & Balayage).text <> "" then
        'ajouter une ligne dans ton mail avec la valeur de la textbox elle même
        ' soit  Userform1.controls("TextBox" & Balayage).text 

        'ou tu constuit ta chaine MyDocConstruction, bref tu fais comme tu le veux.        

    end if

    'pour le cas des checkbox c'est moins évident surement vu que dans ton exemple 
    'tu ajoutais MyDoc2 = "xxxxxxx", donc tu peux           boucler sur tes checkbox
    If Userform1.Controls("CheckBox" & Balayage).value = true then
        'copier dans le mail ta valeur du text que tu affiches à coté de ta checkbox avec
        Userform1.Controls("CheckBox" & Balayage).Caption 

        'dans le cas ou tu veux ajouter un autre texte spécifique tu peux faire un select case 
        'selon la valeur de Balayage et donc du CheckBox en question
        select case Balayage
            case 1
                 'par exemple si on arrive ici c'est que le CheckBox1.value = true, tu peux donc
                 ' mettre dans ton mail la valeur que tu veux
            case 2

        end select

        'bref on peut imaginer beaucoup de chose
    end if
next

ECG

Merci beaucoup pour ta réponse, je vais essayer de modifier ça dans ma macro, je crois comprendre où tu veux en venir, merci d'avoir pris le temps de détailler autant !

Bonjour, j'ai essayé beaucoup de choses sans succès, surtout que les sauts de lignes qui me dérange concernent les checkbox....ne penses-tu pas qu'il existe un bout de code me permettant de dire que si ma variable est vide alors elle prend une valeur : " suppression d'un retour chariot" ainsi cela compensera le saut de ligne dans le corps de mon mail..du genre :

If Checkbox.Value = True Then MyDocx = "XXXXX"
Else MyDocx = 'suppression du retour à la ligne

Je continue mes recherches....

Pourrais-tu joindre ton fichier actuel voir un peu l'intégralité de ta macro svp

ECG

Je te mets le code mais je ne peux malheureusement pas joindre mon fichier car il contient des données confidentielles...

Private Sub UserForm_Click()
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
CheckBox1.Value = ""
CheckBox2.Value = ""
CheckBox3.Value = ""
CheckBox4.Value = ""
CheckBox5.Value = ""
CheckBox6.Value = ""
CheckBox7.Value = ""
CheckBox8.Value = ""
CheckBox9.Value = ""
CheckBox10.Value = ""
CheckBox11.Value = ""
CheckBox12.Value = ""
CheckBox13.Value = ""
CheckBox14.Value = ""
CheckBox15.Value = ""
CheckBox16.Value = ""
CheckBox17.Value = ""
CheckBox18.Value = ""
CheckBox20.Value = ""
CheckBox21.Value = ""
CheckBox22.Value = ""
CheckBox23.Value = ""
CheckBox24.Value = ""
CheckBox25.Value = ""
CheckBox26.Value = ""
CheckBox27.Value = ""
CheckBox28.Value = ""
CheckBox29.Value = ""
CheckBox30.Value = ""
CheckBox31.Value = ""
CheckBox32.Value = ""
CheckBox33.Value = ""
CheckBox34.Value = ""
CheckBox35.Value = ""
CheckBox36.Value = ""
End Sub

Private Sub CommandButton1_Click()

Dim Confirmation
Confirmation = MsgBox("Êtes-vous sûr de vouloir envoyer votre demande", vbYesNoCancel, "Attention !")

If Confirmation = vbNo Then
Exit Sub
ElseIf Confirmation = vbCancel Then
Exit Sub
ElseIf Confirmation = vbYes Then

'déclaration variables
Dim Myx, Myx, Myx As String
Dim MyDoc1, MyDoc2, MyDoc3, MyDoc4, MyDoc5, MyDoc6, MyDoc7, MyDoc8, MyDoc9, MyDoc10, MyDoc11, MyDoc12, MyDoc13, MyDoc14, MyDoc15 As String
Dim MyDoc16, MyDoc17, MyDoc18, MyDoc20, MyDoc21, MyDoc22, MyDoc23, MyDoc24, MyDoc25, MyDoc26, MyDoc27, MyDoc28, MyDoc29, MyDoc30 As String
Dim MyDoc31, MyDoc32, MyDoc33, MyDoc34, MyDoc35, MyDoc36 As String

Myx = TextBox1.Value
Myx = TextBox2.Value
Myx = TextBox3.Value
MyDoc1 = CheckBox1.Value
MyDoc2 = CheckBox2.Value
MyDoc3 = CheckBox3.Value
MyDoc4 = CheckBox4.Value
MyDoc5 = CheckBox5.Value
MyDoc6 = CheckBox6.Value
MyDoc7 = CheckBox7.Value
MyDoc8 = CheckBox8.Value
MyDoc9 = CheckBox9.Value
MyDoc10 = CheckBox10.Value
MyDoc11 = CheckBox11.Value
MyDoc12 = CheckBox12.Value
MyDoc13 = CheckBox13.Value
MyDoc14 = CheckBox14.Value
MyDoc15 = CheckBox15.Value
MyDoc16 = CheckBox16.Value
MyDoc17 = CheckBox17.Value
MyDoc18 = CheckBox18.Value
MyDoc20 = CheckBox20.Value
MyDoc21 = CheckBox21.Value
MyDoc22 = CheckBox22.Value
MyDoc23 = CheckBox23.Value
MyDoc24 = CheckBox24.Value
MyDoc25 = CheckBox25.Value
MyDoc26 = CheckBox26.Value
MyDoc27 = CheckBox27.Value
MyDoc28 = CheckBox28.Value
MyDoc29 = CheckBox29.Value
MyDoc30 = CheckBox30.Value
MyDoc31 = CheckBox31.Value
MyDoc32 = CheckBox32.Value
MyDoc33 = CheckBox33.Value
MyDoc34 = CheckBox34.Value
MyDoc35 = CheckBox35.Value
MyDoc36 = CheckBox36.Value

'définition des checkbox
If CheckBox1.Value = True Then
MyDoc1 = "x" & vbCrLf
ElseIf CheckBox1.Value = False Then
MyDoc1 = "" & Replace(MyDoc1, "x" & vbCrLf, "")
End If
If CheckBox2.Value = True Then
MyDoc2 = "x" & vbCrLf
ElseIf CheckBox2.Value = False Then
MyDoc2 = "" & Replace(MyDoc2, "x" & vbCrLf, "")
End If
If CheckBox3.Value = True Then
MyDoc3 = "x" & vbCrLf
ElseIf CheckBox3.Value = False Then
MyDoc3 = "" & Replace(MyDoc3, "x" & vbCrLf, "")
End If
If CheckBox4.Value = True Then
MyDoc4 = "x" & vbCrLf
ElseIf CheckBox4.Value = False Then
MyDoc4 = "" & Replace(MyDoc4, "x" & vbCrLf, "")
End If
If CheckBox5.Value = True Then
MyDoc5 = "x" & vbCrLf
ElseIf CheckBox5.Value = False Then
MyDoc5 = "" & Replace(MyDoc5, "x" & vbCrLf, "")
End If
If CheckBox6.Value = True Then
MyDoc6 = "x " & vbCrLf
ElseIf CheckBox6.Value = False Then
MyDoc6 = "" & Replace(MyDoc6, "x " & vbCrLf, "")
End If
If CheckBox7.Value = True Then
MyDoc7 = "x" & vbCrLf
ElseIf CheckBox7.Value = False Then
MyDoc7 = "x" & vbCrLf, "")
End If
If CheckBox8.Value = True Then
MyDoc8 = "x" & vbCrLf
ElseIf CheckBox8.Value = False Then
MyDoc8 = "" & Replace(MyDoc8, "x" & vbCrLf, "")
End If
If CheckBox9.Value = True Then
MyDoc9 = "x" & vbCrLf
ElseIf CheckBox9.Value = False Then
MyDoc9 = "" & Replace(MyDoc9, "x" & vbCrLf, "")
End If
If CheckBox10.Value = True Then
MyDoc10 = "x" & vbCrLf
ElseIf CheckBox10.Value = False Then
MyDoc10 = "" & Replace(MyDoc10, "x" & vbCrLf, "")
End If
If CheckBox11.Value = True Then
MyDoc11 = "x" & vbCrLf
ElseIf CheckBox11.Value = False Then
MyDoc11 = "" & Replace(MyDoc11, "x" & vbCrLf, "")
End If
If CheckBox12.Value = True Then
MyDoc12 = "x)" & vbCrLf
ElseIf CheckBox12.Value = False Then
MyDoc12 = "" & Replace(MyDoc12, "x" & vbCrLf, "")
End If
If CheckBox13.Value = True Then
MyDoc13 = "x" & vbCrLf
ElseIf CheckBox13.Value = False Then
MyDoc13 = "" & Replace(MyDoc13, "x" & vbCrLf, "")
End If
If CheckBox14.Value = True Then
MyDoc14 = "x" & vbCrLf
ElseIf CheckBox14.Value = False Then
MyDoc14 = "" & Replace(MyDoc14, "x" & vbCrLf, "")
End If
If CheckBox15.Value = True Then
MyDoc15 = "x" & vbCrLf
ElseIf CheckBox15.Value = False Then
MyDoc15 = "" & Replace(MyDoc15, "x" & vbCrLf, "")
End If
If CheckBox16.Value = True Then
MyDoc16 = "x" & vbCrLf
ElseIf CheckBox16.Value = False Then
MyDoc16 = "" & Replace(MyDoc16, "x" & vbCrLf, "")
End If
If CheckBox17.Value = True Then
MyDoc17 = "x" & vbCrLf
ElseIf CheckBox17.Value = False Then
MyDoc17 = "" & Replace(MyDoc17, "x" & vbCrLf, "")
End If
If CheckBox18.Value = True Then
MyDoc18 = "x" & vbCrLf
ElseIf CheckBox18.Value = False Then
MyDoc18 = "" & Replace(MyDoc18, "x" & vbCrLf, "")
End If
If CheckBox20.Value = True Then
MyDoc20 = "x" & vbCrLf
ElseIf CheckBox20.Value = False Then
MyDoc20 = "" & Replace(MyDoc20, "x" & vbCrLf, "")
End If
If CheckBox21.Value = True Then
MyDoc21 = "x" & vbCrLf
ElseIf CheckBox21.Value = False Then
MyDoc21 = "" & Replace(MyDoc21, "x" & vbCrLf, "")
End If
If CheckBox22.Value = True Then
MyDoc22 = "x" & vbCrLf
ElseIf CheckBox22.Value = False Then
MyDoc22 = "" & Replace(MyDoc22, "x" & vbCrLf, "")
End If
If CheckBox23.Value = True Then
MyDoc23 = "x" & vbCrLf
ElseIf CheckBox23.Value = False Then
MyDoc23 = "" & Replace(MyDoc23, "x" & vbCrLf, "")
End If
If CheckBox24.Value = True Then
MyDoc24 = "x" & vbCrLf
ElseIf CheckBox24.Value = False Then
MyDoc24 = "" & Replace(MyDoc24, "x" & vbCrLf, "")
End If
If CheckBox25.Value = True Then
MyDoc25 = "x" & vbCrLf
ElseIf CheckBox25.Value = False Then
MyDoc25 = "" & Replace(MyDoc25, "x" & vbCrLf, "")
End If
If CheckBox26.Value = True Then
MyDoc26 = "x" & vbCrLf
ElseIf CheckBox26.Value = False Then
MyDoc26 = "" & Replace(MyDoc26, "x" & vbCrLf, "")
End If
If CheckBox27.Value = True Then
MyDoc27 = "x" & vbCrLf
ElseIf CheckBox27.Value = False Then
MyDoc27 = "" & Replace(MyDoc27, "x" & vbCrLf, "")
End If
If CheckBox28.Value = True Then
MyDoc28 = "x" & vbCrLf
ElseIf CheckBox28.Value = False Then
MyDoc28 = "" & Replace(MyDoc28, "x" & vbCrLf, "")
End If
If CheckBox29.Value = True Then
MyDoc29 = "x" & vbCrLf
ElseIf CheckBox29.Value = False Then
MyDoc29 = "" & Replace(MyDoc29, "x" & vbCrLf, "")
End If
If CheckBox30.Value = True Then
MyDoc30 = "x" & vbCrLf
ElseIf CheckBox30.Value = False Then
MyDoc30 = "" & Replace(MyDoc30, "x" & vbCrLf, "")
End If
If CheckBox31.Value = True Then
MyDoc31 = "x" & vbCrLf
ElseIf CheckBox31.Value = False Then
MyDoc31 = "" & Replace(MyDoc31, "x" & vbCrLf, "")
End If
If CheckBox32.Value = True Then
MyDoc32 = "x" & vbCrLf
ElseIf CheckBox32.Value = False Then
MyDoc32 = "" & Replace(MyDoc32, "x" & vbCrLf, "")
End If
If CheckBox33.Value = True Then
MyDoc33 = "x" & vbCrLf
ElseIf CheckBox33.Value = False Then
MyDoc33 = "" & Replace(MyDoc33, "x" & vbCrLf, "")
End If
If CheckBox34.Value = True Then
MyDoc34 = "x" & vbCrLf
ElseIf CheckBox34.Value = False Then
MyDoc34 = "" & Replace(MyDoc34, "x" & vbCrLf, "")
End If
If CheckBox35.Value = True Then
MyDoc35 = "x" & vbCrLf
ElseIf CheckBox35.Value = False Then
MyDoc35 = "" & Replace(MyDoc35, "x" & vbCrLf, "")
End If
If CheckBox36.Value = True Then
MyDoc36 = "x" & vbCrLf
ElseIf CheckBox36.Value = False Then
MyDoc36 = "" & Replace(MyDoc36, "x" & vbCrLf, "")
End If

Dim Myconstruction As String
Myconstruction = "Hello," & vbCrLf & "" & vbCrLf & "x : " & Myx & "" & vbCrLf & "" & vbCrLf & "x :" & vbCrLf & "" & MyDoc1 & "" & MyDoc2 & "" & MyDoc3 & "" & MyDoc4 & "" & MyDoc5 & "" & MyDoc6 & "" & MyDoc7 & "" & MyDoc8 & "" & MyDoc9 & "" & MyDoc10 & "" & MyDoc11 & "" & MyDoc12 & "" & MyDoc13 & "" & MyDoc14 & "" & MyDoc15 & "" & MyDoc16 & "" & MyDoc17 & "" & MyDoc18 & "" & MyDoc20 & "" & MyDoc21 & "" & MyDoc22 & "" & MyDoc23 & "" & MyDoc24 & "" & MyDoc25 & "" & MyDoc26 & "" & MyDoc27 & "" & MyDoc28 & "" & MyDoc29 & "" & vbCrLf & "x :" & vbCrLf & "" & MyDoc30 & "" & MyDoc31 & "" & MyDoc32 & "" & MyDoc33 & "" & MyDoc34 & "" & MyDoc35 & "" & MyDoc36 & "" & vbCrLf & "" & vbCrLf & "Best Regards,"

    'Envoi du mail au client
        Dim OutApp As Object
        Dim OutMail As Object
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
ListeDestinataire = ""
ListeCopie = ""

On Error Resume Next
        With OutMail
            .To = ListeDestinataire
            .CC = ListeCopie
            .Subject = "x " & Myx & " - DL " & Myx & ""
.Body = Replace(Myconstruction, False, "")
            .Display
        End With

        On Error GoTo 0
        Set OutMail = Nothing
        Set OutApp = Nothing

End If

End Sub

Ca pique les yeux

Réduit ton code

ex :

Dim i As Integer
For i = 1 To 36
    CheckBox(i).Value = ""
Next i

Ca pique les yeux

Réduit ton code

ex :

Dim i As Integer
For i = 1 To 36
    CheckBox(i).Value = ""
Next i

Salut, je veux bien savoir comment tu attributs comme ça directement les valeurs des checkbox sans passer par un userform.controls, je suis preneur si tu peux donner un exemple, cette astuce m'intéresse !

ECG

lol

trop vite

For I = 1 To 36
    Me.Controls("CheckBox" & I).Value = True
Next I

Donc je viens de regarde, je vais essayé d'être clair mais je ne promets rien ^^

1.

Déjà je vois un

Private Sub UserForm_Click() 

Si tu veux initialiser tes variables ce n'est pas ce qu'il faut faire, met le plutôt par habitude dans

 Private Sub UserForm_Initialize() 

Après je ne sais pas vraiment pourquoi tu veux initialisé en "" une checkbox mais sinon quand tu lances le formulaire cela va les afficher dans l'état ou tu les mets sur ton formulaire dans VBA donc tu peux contrôler la valeur par défaut sans avoir à faire des lignes de codes non?

Dans le sens ou tu veux t'en servir pour remettre toute les checkbox.valeur d'un coup je peux le comprendre mais dans le userform_click je ne vois pas l'utilité surtout que la si une personne coche les 36 checkbox et clique sur l'userform... ça efface tout le travail ^^ j'espère que les gens ne connaissent pas l'emplacement de ton bureau ou tu bosses sinon tu risques d'avoir de la visite

Dans le cas ou tu veux vraiment initialiser tes checkboxs pour être sur (dans le userform_initialise) ou faire un bouton pour tout effacer ou tout remplir (une sub quelconque que tu appelles toi même), vu que tes noms se suivent parfaitement cela va nous aider :

For i = 1 To 36
    UserForm.Controls("CheckBox" & i).Value = true/false/"" 'selon ton cas
Next

2.

Hmm pour ta confirmation, le

ElseIf Confirmation = vbYes Then

ne sert a rien je dirais car quand tu arrives ici forcément c'est tu n'a plus que ce cas la, tu n'as pas de else par derrière possible

D'ailleurs tu pourrais faire le vbNo et VbCancel sur une seule ligne avec une condition Or car tu exécute le même code dans les 2 cas

3.

Pour la déclaration des variables, si tu déclares tout sur la même lignes il faut déclarer à chaque fois le type sinon ça les mets en variant (voir https://www.excel-pratique.com/fr/vba/variables.php) dans le sens ou

Dim X,Y,Z as string

équivaut pour Excel à

Dim X as variant, Y as variant, Z as string

il me semble.. il faut tous se les farcirs à la mains je crois une autre personne du forum pour confirmer ou infirmer ce que je raconte ^^

4.

Dim Myx, Myx, Myx As String
Myx = TextBox1.Value
Myx = TextBox2.Value
Myx = TextBox3.Value

Attention 3 fois la même variables dont tu écrases la valeur, j'imagine que c'est X/Y/Z

Met un

Option Explicit

en début de ton module, pour avoir à bien déclarer toute les variables, dans ton cas Excel te diras que tu as plusieurs fois la même

5.

MyDoc1 = CheckBox1.Value
MyDoc2 = CheckBox2.Value
MyDoc3 = CheckBox3.Value

ok tu prends en mémoire les valeurs, tu pourras boucler avec le userform.control pour le faire en une passe plutôt de faire beaucoup de ligne de code

6.

If CheckBox1.Value = True Then
MyDoc1 = "x" & vbCrLf
ElseIf CheckBox1.Value = False Then
MyDoc1 = "" & Replace(MyDoc1, "x" & vbCrLf, "")
End If

Je ne comprends pas ton ElseIf CheckBox1.Value = False, pour moi il ne sert à rien, tu as que 2 valeurs pour une checkbox de base, si elle n'est pas à True alors forcément elle vaut False, un simple if/else suffit

7.

En général je pense que tu fais des checks en trop surtout avec ta variable MyConstruction que tu refais après, je pense que tu dois tout faire d'un coup mais dur à dire juste avec ton code comme ça.

D'ailleurs quand je regarde ta MyConstruction:

Myconstruction = "Hello," & vbCrLf & "" & vbCrLf & "x : " & Myx & "" & vbCrLf & "" & vbCrLf & "x :" & vbCrLf & "" & MyDoc1 & "" & MyDoc2...

A quoi cela te sert de rajouter & "" & ? cela ajoute... rien entre deux Mydoc? tu voulais pas mettre un espace plutot? avec & " " &

Personnellement je pense que tu dois tout faire d'un coup, du genre:

Supprime tous les Mydoc et les check et fait directement:

Myconstruction = "Hello," & vbCrLf & "" & vbCrLf & "x : " & Myx & "" & vbCrLf & "" & vbCrLf & "x :" & vbCrLf 

for i = 1 to 36
    if userform.controls("Checkbox" & i).value = true then
        Myconstruction = Myconstruction & vbCrLf & useform.controls("Textbox"& i).value
    end if    

next

Au pire prenons un exemple simple: le cas ou tu aurais 2 textbox:

Cas 1: textbox1.value = "toto"

textbox2.valeur = ""

Que veut tu voir dans Myconstruction ou dans ton mail?

Case 2 : textbox1.value = ""

textbox2.valeur = ""

Que veut tu voir dans Myconstruction ou dans ton mail?

Cas 1: textbox1.value = "toto"

textbox2.valeur = "titi"

Que veut tu voir dans Myconstruction ou dans ton mail?

A noter que je fais ça de tête, il manque le formalisme d'Excel comme les Majuscules et autres mais tu t'en sortira je pense.

Après peut être que je n'ai pas compris ton cheminement mais je vais refaire rapidos ton coder pour t'éclairer mais dans un autre message ^^

ECG

Donc si je refais un peu ton fichier (en gras les changement ou j'ai retirer/modifier par rapport à toi pour bien voir un peu les différences le plus possible):

Option Explicit

Private Sub[b] UserForm_Initialize()[/b]
TextBox1.Value = ""
TextBox2.Value = ""
TextBox3.Value = ""
'allez pour 3 textbox on pourrais aussi utiliser le .controls mais bon hein :D

[b]For i = 1 To 36
    UserForm.Controls("CheckBox" & i).Value = ""
Next[/b]

End Sub

Private Sub CommandButton1_Click()

Dim Confirmation
Confirmation = MsgBox("Êtes-vous sûr de vouloir envoyer votre demande", vbYesNoCancel, "Attention !")

[b]If Confirmation = vbNo or Confirmation = vbCancel Then
    Exit Sub
end if[/b]

'donc si j'ai bien compris ou tu voulais en venir:
Dim Myx [b]as string[/b], My[b]y as string[/b], My[b]Z[/b] As String
'bon après le fait de déclarer 2 variants et une string ou 3 string ne va pas changer la face du monde :)

Myx = TextBox1.Value
My[b]y[/b] = TextBox2.Value
My[b]z[/b] = TextBox3.Value

Dim Myconstruction As String
[b]Myconstruction = "Hello," & vbCrLf & "" & vbCrLf & "x : " & Myx & "" & vbCrLf & "" & vbCrLf & "x :" & vbCrLf[/b]

[b]For i = 1 to 36
    if Userform.Controls("Checkbox" & i).value = true then
        Myconstruction = Myconstruction & vbCrLf & Useform.Controls("Textbox"& i).value
    end if    

next[/b]

[b]'je n'ai pas du tout checker la partie suivante pour la construction du mail donc je la reprends :[/b]

'Envoi du mail au client
        Dim OutApp As Object
        Dim OutMail As Object
        Set OutApp = CreateObject("Outlook.Application")
        Set OutMail = OutApp.CreateItem(0)
ListeDestinataire = ""
ListeCopie = ""

On Error Resume Next
        With OutMail
            .To = ListeDestinataire
            .CC = ListeCopie
            .Subject = "x " & Myx & " - DL " & Myx & ""
.Body = Replace(Myconstruction, False, "")
            .Display
        End With

        On Error GoTo 0
        Set OutMail = Nothing
        Set OutApp = Nothing

End If

A voir si c'est dans l'esprit de ce que tu voulais

EDIT1: bon le gras ne marche pas dans un code mais bon je le laisse quand même ça délimite ce que j'ai fais.

EDIT2: au final je suis partis sur une mauvaise information , si le checkbox.value = true tu le prends dans MyConstruction ! et pas du tout le checkbox.value correspondant comme je pensais (avec un exemple de userform ça m'aiderais à comprendre hi hi) ! du coup je ne comprends pas :/ c'est pas plutot le checkbox.caption que tu veux reprendre donc dans ton mail? l'intitulé du check quoi?

Si oui il faut corriger

Myconstruction = Myconstruction & vbCrLf & Useform.Controls("Textbox"& i).value

par

Myconstruction = Myconstruction & vbCrLf & Useform.Controls("CheckBox"& i).Caption

ou tes "x" représentent totalement une autre données? si oui il va falloir modifier le code un peu plus

ECG

Salut,

Merci pour ta réponse aussi détaillé, je sais que je n'ai pas encore les réflexes et les connaissances pour faire des codes plus "simples" et donc je fait beaucoup de lignes qui pourraient être réduites. Merci pour ton code, je regarde ça tout ce week-end (j'ai des semaines très chargées), je répondrai à tes questions pour le UserForm Control, et reprendrai mon code pour une version plus allégée.

Merci encore

Rechercher des sujets similaires à "userform outlook"