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

VBA Word Discussion :

Macro qui encadre un texte non encadré et qui désencadre un texte encadré


Sujet :

VBA Word

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Août 2010
    Messages
    159
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2010
    Messages : 159
    Points : 73
    Points
    73
    Par défaut Macro qui encadre un texte non encadré et qui désencadre un texte encadré
    Bonjour le Forum !

    J'ai créé une macro qui crée une bordure autour d'un texte (aucun mérite, grâce à l'enregistreur de macros).

    En revanche, j'aimerais la transformer en "bascule" :
    Si le texte de départ n'est pas encadré, la macro l'encadre.
    Si le texte de départ est encadré, la macro supprime l'encadrement.
    ...mais je ne sais pas comment m'y prendre.

    Quelqu'un pourrait-il m'aider siouplé ?

    Merci !!!


    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
    Sub Format_Police_Encadré()
    '
        With Selection.ParagraphFormat
            With .Borders(wdBorderLeft)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            With .Borders(wdBorderRight)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            With .Borders(wdBorderTop)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            With .Borders(wdBorderBottom)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
            With .Borders
                .DistanceFromTop = 1
                .DistanceFromLeft = 4
                .DistanceFromBottom = 1
                .DistanceFromRight = 4
                .Shadow = False
            End With
        End With
        With Options
            .DefaultBorderLineStyle = wdLineStyleSingle
            .DefaultBorderLineWidth = wdLineWidth050pt
            .DefaultBorderColor = wdColorAutomatic
        End With
    End Sub

  2. #2
    Expert confirmé
    Avatar de Hephaistos007
    Profil pro
    Enseignant Chercheur
    Inscrit en
    Décembre 2004
    Messages
    2 493
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations professionnelles :
    Activité : Enseignant Chercheur
    Secteur : Enseignement

    Informations forums :
    Inscription : Décembre 2004
    Messages : 2 493
    Points : 4 166
    Points
    4 166
    Par défaut
    Il te faudra une structure If...Else...End If
    Il vaut mieux mobiliser son intelligence sur des conneries que mobiliser sa connerie sur des choses intelligentes --- devise SHADOKS

    Kit de survie Android : mon guide pour apprendre à programmer sur Android, mon tutoriel sur les web services et enfin l'outil en ligne pour vous faire gagner du temps - N'oubliez pas de consulter la FAQ Android

  3. #3
    Membre éclairé Avatar de Souriane
    Femme Profil pro
    Assistant aux utilisateurs
    Inscrit en
    Septembre 2009
    Messages
    541
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Canada

    Informations professionnelles :
    Activité : Assistant aux utilisateurs
    Secteur : Service public

    Informations forums :
    Inscription : Septembre 2009
    Messages : 541
    Points : 758
    Points
    758
    Par défaut
    Bonjour,

    Comme Hephaistos007 le dit, avec un If...else comme ci-dessous. Dans cet exemple cependant, la macro ne fait que vérifier s'il y a une ligne à droite du paragraphe. Tu peux l'adapter pour qu'elle vérifie aussi les 3 autres côtés si nécessaire.

    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
    Sub Format_Police_Encadré()
     
    'S'il y une bordure à droite...
    If selection.ParagraphFormat.Borders(wdBorderRight).LineStyle = wdLineStyleSingle Then
     
        'S'il y une bordure à droite enlever toutes les bordures
            With selection.ParagraphFormat
                .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
                .Borders(wdBorderRight).LineStyle = wdLineStyleNone
                .Borders(wdBorderTop).LineStyle = wdLineStyleNone
                .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
            End With
     
        Else
     
    'Sinon ajouter des bordures au 4 côtés.
     
        With selection.ParagraphFormat
            With .Borders(wdBorderLeft)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            With .Borders(wdBorderRight)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            With .Borders(wdBorderTop)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            With .Borders(wdBorderBottom)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            .Borders(wdBorderHorizontal).LineStyle = wdLineStyleNone
            With .Borders
                .DistanceFromTop = 1
                .DistanceFromLeft = 4
                .DistanceFromBottom = 1
                .DistanceFromRight = 4
                .Shadow = False
            End With
        End With
        With Options
            .DefaultBorderLineStyle = wdLineStyleSingle
            .DefaultBorderLineWidth = wdLineWidth050pt
            .DefaultBorderColor = wdColorAutomatic
        End With
     
     
        End If
     
    End Sub
    Bonne chance!

    Souriane
    __________________________________
    Une question bien posée est à moitié résolue!

    Merci de ne pas oublier de mettre RÉSOLU quand le sujet l'est. Cela aide tous les DVPnautes dans leur recherche

  4. #4
    Membre régulier
    Profil pro
    Inscrit en
    Août 2010
    Messages
    159
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2010
    Messages : 159
    Points : 73
    Points
    73
    Par défaut
    Bonjour,

    Merci à tous les 2.

    @Souriane
    Ta macro marche parfaitement pour encadrer les paragraphes.
    Comme je souhaite encadrer seulement du texte, j'ai tenté d'adapter ton code, mais ça ne marche pas.
    Pourtant il me semble bien avoir remplacé juste ce qu'il faut de ton code initial avec du code issu d'une macro qui marche et qui encadre le texte sélectionné.
    Mais peut-être que le code qui encadre le texte et celui qui encadre les paragraphes est-il différent, mais je n'ai pas trouvé sur internet.
    Ou alors c'est parce que je suis sur Word 2003 (oui, je sais, je date un peu, mais je préfère cette version).

    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
    Sub Format_Police_Encadré()
     
        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
     
        'S'il y une bordure à droite...
    If Selection.ParagraphFormat.Borders(wdBorderRight).LineStyle = wdLineStyleSingle Then
     
        'S'il y une bordure à droite enlever toutes les bordures
            With Selection.Font
                .Borders(wdBorderLeft).LineStyle = wdLineStyleNone
                .Borders(wdBorderRight).LineStyle = wdLineStyleNone
                .Borders(wdBorderTop).LineStyle = wdLineStyleNone
                .Borders(wdBorderBottom).LineStyle = wdLineStyleNone
            End With
     
        Else
     
    'Sinon ajouter des bordures aux 4 côtés.
     
        Selection.HomeKey Unit:=wdLine
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
     
        With Selection.Font
            With .Borders(1)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth025pt
                .Color = wdColorAutomatic
            End With
            .Borders.Shadow = False
        End With
        With Options
            .DefaultBorderLineStyle = wdLineStyleSingle
            .DefaultBorderLineWidth = wdLineWidth025pt
            .DefaultBorderColor = wdColorAutomatic
        End With
     
        End If
     
        Selection.HomeKey Unit:=wdLine
     
    End Sub

  5. #5
    Membre éclairé Avatar de Souriane
    Femme Profil pro
    Assistant aux utilisateurs
    Inscrit en
    Septembre 2009
    Messages
    541
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Canada

    Informations professionnelles :
    Activité : Assistant aux utilisateurs
    Secteur : Service public

    Informations forums :
    Inscription : Septembre 2009
    Messages : 541
    Points : 758
    Points
    758
    Par défaut
    Bonjour,

    Voici je t'ai fait quelque chose rapidement :

    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
    Sub test()
     
    '
     
    If selection.Font.Borders(1).LineStyle = wdLineStyleNone Then
        With selection.Font
            With .Borders(1)
                .LineStyle = wdLineStyleSingle
                .LineWidth = wdLineWidth050pt
                .Color = wdColorAutomatic
            End With
            .Borders.Shadow = False
        End With
     
        With Options
            .DefaultBorderLineStyle = wdLineStyleSingle
            .DefaultBorderLineWidth = wdLineWidth050pt
            .DefaultBorderColor = wdColorAutomatic
        End With
     
    Else
     
       With selection.Font
            With .Borders(1)
                .LineStyle = wdLineStyleNone
            End With
        End With
    End If
     
    End Sub
    Bonne chance!

    Souriane
    __________________________________
    Une question bien posée est à moitié résolue!

    Merci de ne pas oublier de mettre RÉSOLU quand le sujet l'est. Cela aide tous les DVPnautes dans leur recherche

  6. #6
    Membre régulier
    Profil pro
    Inscrit en
    Août 2010
    Messages
    159
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Août 2010
    Messages : 159
    Points : 73
    Points
    73
    Par défaut
    C'est parfait !
    Merci !!!!!

    (je me cassais les dents dessus depuis plusieurs jours)

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

Discussions similaires

  1. Réponses: 1
    Dernier message: 05/08/2016, 15h21
  2. Réponses: 0
    Dernier message: 24/07/2016, 11h45
  3. [Débutant] Comment "encadrer" les points d'un graphe qui appartiennent à une matrice ?
    Par linpro.lalaland dans le forum MATLAB
    Réponses: 8
    Dernier message: 11/07/2014, 15h31
  4. Macro qui modifie une cellule (non-voulu)
    Par spirit1300 dans le forum Macros et VBA Excel
    Réponses: 3
    Dernier message: 11/12/2007, 17h37
  5. Réponses: 3
    Dernier message: 12/05/2007, 18h24

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