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

Résolu
bassmart Messages postés 281 Date d'inscription   Statut Membre Dernière intervention   -  
bassmart Messages postés 281 Date d'inscription   Statut Membre Dernière intervention   -
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!

2 réponses

  1. Frenchie83 Messages postés 2254 Statut Membre 339
     
    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
    1. bassmart Messages postés 281 Date d'inscription   Statut Membre Dernière intervention   1
       
      Merci beaucoup pour la réponse!

      J'avais mal compris la fonction.

      Merci!
      0
    2. bassmart Messages postés 281 Date d'inscription   Statut Membre Dernière intervention   1
       
      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
  2. Frenchie83 Messages postés 2254 Statut Membre 339
     
    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
    1. bassmart Messages postés 281 Date d'inscription   Statut Membre Dernière intervention   1
       
      Merci beaucoup!

      Ça fonctionne mais la mise en forme ne s'effectue pas pour la première lettre de chaque mots?
      0
      1. Frenchie83 Messages postés 2254 Statut Membre 339 > bassmart Messages postés 281 Date d'inscription   Statut Membre Dernière intervention  
         
        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
      2. bassmart Messages postés 281 Date d'inscription   Statut Membre Dernière intervention   1 > Frenchie83 Messages postés 2254 Statut Membre
         
        Merci!

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