Mettre en gras plusieurs partie d'un texte dans une zone texte

Résolu/Fermé
bassmart Messages postés 281 Date d'inscription jeudi 19 février 2015 Statut Membre Dernière intervention 19 décembre 2023 - 26 avril 2016 à 21:33
bassmart Messages postés 281 Date d'inscription jeudi 19 février 2015 Statut Membre Dernière intervention 19 décembre 2023 - 2 mai 2016 à 19:42
Bonjour à tous!

En cherchant un peu j'ai trouvé une macro pour insérer une zone de texte automatiquement sur une feuille. J'ai réussi à mettre une partie de mon texte en gras, mais je veux mettre aussi les mots "SITE:", "SONDAGE:" et ÉLÉVATION T.N:" en gras.

Mais voilà, je n'y arrive pas!!

Voici mon code:
Sub zone_texte()

ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 140, 10, _
        500, 70).Select
    With selection
    .Name = "Ma_zone"
    .ShapeRange.Fill.Visible = msoTrue
    .ShapeRange.Fill.Solid
    .ShapeRange.Fill.Transparency = 0#
    .ShapeRange.Line.Weight = 0.75
    .ShapeRange.Line.DashStyle = msoLineSolid
    .ShapeRange.Line.Style = msoLineSingle
    .ShapeRange.Line.Transparency = 0#
    .ShapeRange.Line.Visible = msoTrue
    .ShapeRange.Line.ForeColor.SchemeColor = 64
    .ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
    .Characters.Text = "SUIVI PIÉZOMÉTRIQUE" & vbCrLf & vbCr & "SITE: " & UserForm2.TextBox1 & Space(45) & "SONDAGE: " & UserForm2.TextBox2 & Space(45) & "ÉLÉVATION T.N: "
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    End With
    
    With selection.Characters(Start:=1, Length:=20).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 14
        .ColorIndex = xlAutomatic
    End With
    
    With selection.Characters(Start:=21, Length:=22).Font' Ici ça ne fonctionne pas le mots "Site" et tout ce qui suit est en gras
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 12
        .ColorIndex = xlAutomatic
    End With
              
    End Sub


Merci pour votre aide!
A voir également:

2 réponses

Frenchie83 Messages postés 2240 Date d'inscription lundi 6 mai 2013 Statut Membre Dernière intervention 11 août 2023 337
27 avril 2016 à 07:45
Bonjour
Length=indique le nombre de caractères retenus à partir du premier indiqué par la valeur de "Start". Dans votre cas, vous prenez 22 caractères à partir du 21ème. Remplacez Length:=22 par Length:=4 pour les 4 caractères de "Site".

With selection.Characters(Start:=21, Length:=4).Font

Mais vous pouvez remplacez les 2 paragraphes suivants
    With selection.Characters(Start:=1, Length:=20).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 14
        .ColorIndex = xlAutomatic
    End With
    
    With selection.Characters(Start:=21, Length:=22).Font' Ici ça ne fonctionne pas le mots "Site" et tout ce qui suit est en gras
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 12
        .ColorIndex = xlAutomatic
    End With

par
With selection.Characters(Start:=1, Length:=25).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 14
        .ColorIndex = xlAutomatic
    End With

A tester
Cdlt
0
bassmart Messages postés 281 Date d'inscription jeudi 19 février 2015 Statut Membre Dernière intervention 19 décembre 2023 1
28 avril 2016 à 18:49
Merci beaucoup pour la réponse!

J'avais mal compris la fonction.

Merci!
0
bassmart Messages postés 281 Date d'inscription jeudi 19 février 2015 Statut Membre Dernière intervention 19 décembre 2023 1
Modifié par bassmart le 28/04/2016 à 21:08
Bon petit problème!

Le problème c'est que j'insère du texte avec des textbox via un userform et les mots insérés ne sont pas toujours de la même longueur. Donc au final, mes titres (SONDAGE et ÉLÉVATION T.N ne sont pas toujours en gras comme voulu.

Il y a t'il un autre solution?

Voici mon code complet:
Sub zone_texte()

ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 10, _
        600, 70).Select
    With selection
    .Name = "Ma_zone"
    .ShapeRange.Fill.Visible = msoTrue
    .ShapeRange.Fill.Solid
    .ShapeRange.Fill.Transparency = 0#
    .ShapeRange.Line.Weight = 0.75
    .ShapeRange.Line.DashStyle = msoLineSolid
    .ShapeRange.Line.Style = msoLineSingle
    .ShapeRange.Line.Transparency = 0#
    .ShapeRange.Line.Visible = msoTrue
    .ShapeRange.Line.ForeColor.SchemeColor = 64
    .ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
    .Characters.Text = "MINISTÈRE DES TRANSPORT - SUIVI PIÉZOMÉTRIQUE" & vbCrLf & vbCr & "SITE: " & UserForm2.TextBox1 & Space(45) & "SONDAGE: " & UserForm2.TextBox2 & Space(45) & "ÉLÉVATION T.N: " & UserForm2.TextBox3
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    End With
    
    With selection.Characters(Start:=1, Length:=45).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 14
        .ColorIndex = xlAutomatic
    End With
    
     With selection.Characters(Start:=46, Length:=7).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 12
        .ColorIndex = xlAutomatic
    End With
    
    With selection.Characters(Start:=103, Length:=8).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 12
        .ColorIndex = xlAutomatic
    End With
    
    With selection.Characters(Start:=161, Length:=14).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 12
        .ColorIndex = xlAutomatic
    End With

End Sub


Merci pour votre aide!
0
Frenchie83 Messages postés 2240 Date d'inscription lundi 6 mai 2013 Statut Membre Dernière intervention 11 août 2023 337
29 avril 2016 à 10:36
Bonjour
Essayez ceci
Sub zone_texte()
    Dim Phrase, PosSondage, PosElevation
    Phrase = "MINISTÈRE DES TRANSPORT - SUIVI PIÉZOMÉTRIQUE" & vbCrLf & vbCr & "SITE: " & UserForm2.TextBox1 & Space(45) & "SONDAGE: " & UserForm2.TextBox2 & Space(45) & "ÉLÉVATION T.N: " & UserForm2.TextBox3
    PosSondage = InStr(1, Phrase, "SONDAGE", 1)
    PosElevation = InStr(1, Phrase, "ÉLÉVATION T.N", 1)
    
    ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 100, 10, 600, 70).Select
    With Selection
        .Name = "Ma_zone"
        .ShapeRange.Fill.Visible = msoTrue
        .ShapeRange.Fill.Solid
        .ShapeRange.Fill.Transparency = 0#
        .ShapeRange.Line.Weight = 0.75
        .ShapeRange.Line.DashStyle = msoLineSolid
        .ShapeRange.Line.Style = msoLineSingle
        .ShapeRange.Line.Transparency = 0#
        .ShapeRange.Line.Visible = msoTrue
        .ShapeRange.Line.ForeColor.SchemeColor = 64
        .ShapeRange.Line.BackColor.RGB = RGB(255, 255, 255)
        .Characters.Text = Phrase
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
    End With
    
    With Selection.Characters(Start:=1, Length:=45).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 14
        .ColorIndex = xlAutomatic
    End With
    
     With Selection.Characters(Start:=PosSondage, Length:=7).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 12
        .ColorIndex = xlAutomatic
    End With
    
    With Selection.Characters(Start:=103, Length:=8).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 12
        .ColorIndex = xlAutomatic
    End With
    
    With Selection.Characters(Start:=PosElevation, Length:=14).Font
        .Name = "Arial"
        .FontStyle = "Bold"
        .Size = 12
        .ColorIndex = xlAutomatic
    End With
End Sub
Cdlt
0
bassmart Messages postés 281 Date d'inscription jeudi 19 février 2015 Statut Membre Dernière intervention 19 décembre 2023 1
2 mai 2016 à 18:12
Merci beaucoup!

Ça fonctionne mais la mise en forme ne s'effectue pas pour la première lettre de chaque mots?
0
Frenchie83 Messages postés 2240 Date d'inscription lundi 6 mai 2013 Statut Membre Dernière intervention 11 août 2023 337 > bassmart Messages postés 281 Date d'inscription jeudi 19 février 2015 Statut Membre Dernière intervention 19 décembre 2023
2 mai 2016 à 18:53
Bonjour
ajouter -1 en bout de chaque ligne suivantes:
    PosSondage = InStr(1, Phrase, "SONDAGE", 1)
    PosElevation = InStr(1, Phrase, "ÉLÉVATION T.N", 1)

ce qui donne
    PosSondage = InStr(1, Phrase, "SONDAGE", 1)-1
    PosElevation = InStr(1, Phrase, "ÉLÉVATION T.1N", 1)-1

ça devrait aller
Cdlt
0
bassmart Messages postés 281 Date d'inscription jeudi 19 février 2015 Statut Membre Dernière intervention 19 décembre 2023 1 > Frenchie83 Messages postés 2240 Date d'inscription lundi 6 mai 2013 Statut Membre Dernière intervention 11 août 2023
2 mai 2016 à 19:42
Merci!

Ça fonctionne très bien en ajoutant -1 au bout de chacune des lignes!
0