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 charte sous Word


Sujet :

VBA Word

  1. #1
    Candidat au Club
    Femme Profil pro
    Étudiant
    Inscrit en
    Août 2013
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Femme
    Localisation : Belgique

    Informations professionnelles :
    Activité : Étudiant

    Informations forums :
    Inscription : Août 2013
    Messages : 3
    Points : 3
    Points
    3
    Par défaut Macro charte sous Word
    Bonjour tout le monde,

    J'aimerais partager avec vous mon problème afin que vous puissiez me donner des coups de mains et m'aider à trouver une solution.
    Je veux concevoir une macro qui permet de créer une charte pour une société X,
    cette macro permet de créer des données et des tableaux.
    Pour cela tout va bien j'ai pu créer la macro qui permet d'insérer les données et les tableaux pour un document vide.
    Mon problème c'est pour les documents non vides c'est à dire qui contiennent une partie des données que je dois insérer par ma macro, quand j’exécute ma macro sur un fichier qui contient déjà l'un des ses données tout ce mélange et j'obtiens des répétitions et des tableaux l'un dans l'autre.
    J'ai opté pour la recherche par mot-clé pour insérer que les données qui n'existent pas et ça a marché sauf que l'insertion des données se met toujours à la fin du fichier hors moi je veux que l'insertion sois conforme à la charte donnée comme exemple.
    Voila l'exemple de la charte que je dois construire
    Projet.docx

    Voila le code que j'ai écris:
    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
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378
    379
    380
    381
    382
    383
    384
    385
    386
    Sub Charte()  
    '--------------------------------Recherche par blocs------------------------------------------"
     
    With Selection.Find
    .ClearFormatting
    .Text = "Projet :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_1 = True
    Else: Flag_1 = False
    End If
     
    End With
    '---------------- tester l'existance du bloc numéro 1 ----------------------------------------'
    If Flag_1 = True Then
    MsgBox "Le projte existe deja"
    Else
    MsgBox "inseretion du projet"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
            .Name = "Arial"
            .Size = 12
            .Bold = True
            .Italic = False
            .Underline = wdUnderlineNone
            .UnderlineColor = wdColorAutomatic
        End With
        Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Projet :")
    ActiveDocument.Paragraphs.Add
     
    End If
     
    With Selection.Find
    .ClearFormatting
    .Text = "Secteur d'activité :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_2 = True
    Else: Flag_2 = flase
    End If
    End With
    '---------------- tester l'existance du bloc numéro 2 ----------------------------------------'
    If Flag_2 = True Then
    MsgBox "Le secteur existe deja"
     
    ElseIf Flag_2 = False And Flag_1 = True Then
    MsgBox "inseretion du secteur aprés le projet "
    'ordre
    Selection.EndKey Unit:=wdStory
    With Selection.Font
            .Name = "Arial"
            .Size = 12
            .Bold = True
            .Italic = False
            .Underline = wdUnderlineNone
            .UnderlineColor = wdColorAutomatic
        End With
        Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Secteur d'activité :")
    ActiveDocument.Paragraphs.Add
    Else
    MyRange.InsertAfter ("Secteur d'activité :")
    ActiveDocument.Paragraphs.Add
    End If
     
     
    With Selection.Find
    .ClearFormatting
    .Text = "Numéro du projet :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_3 = True
    Else: Flag_3 = flase
    End If
    End With
     
    '---------------- tester l'existance du bloc numéro 3 ----------------------------------------'
    If Flag_3 = True Then
    MsgBox "Le Numéro du projet existe deja"
    Else
    MsgBox "inseretion du Numéro du projet :"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
            .Name = "Arial"
            .Size = 12
            .Bold = True
            .Italic = False
            .Underline = wdUnderlineNone
            .UnderlineColor = wdColorAutomatic
        End With
        Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Numéro du projet :")
    ActiveDocument.Paragraphs.Add
     
    End If
    With Selection.Find
    .ClearFormatting
    .Text = "Tableaux des données 2013:"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_4 = True
    Else: Flag_4 = flase
    End If
    End With
    '---------------- tester l'existance du bloc numéro 4----------------------------------------'
    If Flag_4 = True Then
    MsgBox "L'Tableaux des données 2013 existe deja"
    Else
    MsgBox "inseretion du Tableaux des données 2013:"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
            .Name = "Arial"
            .Size = 12
            .Bold = True
            .Italic = False
            .Underline = wdUnderlineNone
            .UnderlineColor = wdColorAutomatic
        End With
        Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Tableaux des données 2013:")
    Application.Run ("Tableau_CG_1")
    ActiveDocument.Paragraphs.Add
     
    End If
    With Selection.Find
    .ClearFormatting
    .Text = "Tableaux des données 2014 :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_5 = True
    Else: Flag_5 = flase
    End If
    End With
    '---------------- tester l'existance du bloc numéro 5 ----------------------------------------'
    If Flag_5 = True Then
    MsgBox "Le Tableaux des données 2014 existe deja"
    Else
    MsgBox "inseretion du Tableaux des données 2014"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
            .Name = "Arial"
            .Size = 12
            .Bold = True
            .Italic = False
            .Underline = wdUnderlineNone
            .UnderlineColor = wdColorAutomatic
        End With
        Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Tableaux des données 2014:")
    With Selection.Font
            .Name = "Arial"
            .Size = 12
            .Bold = False
            .Italic = False
            .Underline = wdUnderlineNone
            .UnderlineColor = wdColorAutomatic
        End With
        Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    Application.Run ("Tableau_CG_1")
    ActiveDocument.Paragraphs.Add
     
    End If
     
    With Selection.Find
    .ClearFormatting
    .Text = "Mot Clé :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_7 = True
    Else: Flag_7 = flase
    End If
    End With
     
    '---------------- tester l'existance du bloc numéro 7 ----------------------------------------'
    If Flag_7 = True Then
    MsgBox "L'Mot Clé  existe deja"
    Else
    MsgBox "inseretion de Mot Clé :"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
            .Name = "Arial"
            .Size = 12
            .Bold = True
            .Italic = False
            .Underline = wdUnderlineNone
            .UnderlineColor = wdColorAutomatic
        End With
        Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Mot Clé :")
    ActiveDocument.Paragraphs.Add
     
    End If
     
    With Selection.Find
    .ClearFormatting
    .Text = "Historique des évolutions :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_8 = True
    Else: Flag_8 = flase
    End If
    End With
     
    '---------------- tester l'existance du bloc numéro 8 ----------------------------------------'
     
    If Flag_8 = True Then
    On Error Resume Next
    MsgBox "Historique des évolutions existe deja"
    Else
    MsgBox "inseretion de l'Historique des évolutions :"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
            .Name = "Arial"
            .Size = 12
            .Bold = True
            .Italic = False
            .Underline = wdUnderlineNone
            .UnderlineColor = wdColorAutomatic
        End With
        Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Historique des évolutions :")
    With Selection.Font
            .Name = "Arial"
            .Size = 12
            .Bold = False
            .Italic = False
            .Underline = wdUnderlineNone
            .UnderlineColor = wdColorAutomatic
        End With
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    Application.Run ("Tableau_CG_2")
    ActiveDocument.Paragraphs.Add
    End If
    With Selection.Find
    .ClearFormatting
    .Text = "Sommaire"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_1 = True And ordre = 1
    Else: Flag_1 = False And ordre = 0
    End If
     
    End With
     
    '---------------- tester l'existance du bloc numéro 9 ----------------------------------------'
    If Flag_9 = True Then
    On Error Resume Next
    MsgBox ("Le sommaire n'existe pas")
     
    Else
    MsgBox "Inserertion du sommaire "
    With Selection.Font
            .Name = "Arial"
            .Size = 12
            .Bold = False
            .Italic = False
            .Underline = wdUnderlineNone
            .UnderlineColor = wdColorAutomatic
        End With
        Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    Application.Run ("Sommaire")
    ActiveDocument.Paragraphs.Add
    End If
     
     
     
    End Sub
     
    Sub Tableau_CG_1()
     
    '
    Dim oTbl As Table
     
    Selection.EndKey Unit:=wdStory
    Set oTbl = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=2, numcolumns:=3)
    With Selection
     
    oTbl.Cell(1, 1).Range.Text = "Numéro"
    oTbl.Cell(1, 2).Range.Text = "Nom de document"
    oTbl.Cell(1, 3).Range.Text = "Description du document"
     
    End With
     
    'Set oTbl = Nothing
     
     With Selection.Tables(1)
            If .Style <> "Grille du tableau" Then
                .Style = "Grille du tableau"
            End If
            .ApplyStyleHeadingRows = True
            .ApplyStyleLastRow = False
            .ApplyStyleFirstColumn = True
            .ApplyStyleLastColumn = False
            .ApplyStyleRowBands = True
            .ApplyStyleColumnBands = False
          .Cell(1, 1).Shading.Texture = wdTextureNone
          .Cell(1, 1).Shading.ForegroundPatternColor = wdColorAutomatic
          .Cell(1, 1).Shading.ForegroundPatternColor = -603923969
          .Cell(1, 2).Shading.Texture = wdTextureNone
          .Cell(1, 2).Shading.ForegroundPatternColor = wdColorAutomatic
          .Cell(1, 2).Shading.ForegroundPatternColor = -603923969
          .Cell(1, 3).Shading.Texture = wdTextureNone
          .Cell(1, 3).Shading.ForegroundPatternColor = wdColorAutomatic
          .Cell(1, 3).Shading.ForegroundPatternColor = -603923969
           End With
           'Pour éviter le problème de tableau dans un autre tableau
           Selection.EndKey Unit:=wdStory
     
     
    End Sub
     
     
    Sub Tableau_CG_2()
     
    Dim oTbl As Table
    Selection.EndKey Unit:=wdStory
    Set oTbl = ActiveDocument.Tables.Add(Range:=Selection.Range, NumRows:=2, numcolumns:=4)
    With oTbl.Borders
     
    oTbl.Cell(1, 1).Range.Text = "N°"
    oTbl.Cell(1, 2).Range.Text = "Date"
    oTbl.Cell(1, 3).Range.Text = " modification "
    oTbl.Cell(1, 4).Range.Text = "données"
     
     
    End With
     
    Set oTbl = Nothing
    ' Pour éviter le problème
     
     With Selection.Tables(1)
            If .Style <> "Grille du tableau" Then
                .Style = "Grille du tableau"
            End If
            .ApplyStyleHeadingRows = True
            .ApplyStyleLastRow = False
            .ApplyStyleFirstColumn = True
            .ApplyStyleLastColumn = False
            .ApplyStyleRowBands = True
            .ApplyStyleColumnBands = False
          .Cell(1, 1).Shading.Texture = wdTextureNone
          .Cell(1, 1).Shading.ForegroundPatternColor = wdColorAutomatic
          .Cell(1, 1).Shading.ForegroundPatternColor = -603923969
          .Cell(1, 2).Shading.Texture = wdTextureNone
          .Cell(1, 2).Shading.ForegroundPatternColor = wdColorAutomatic
          .Cell(1, 2).Shading.ForegroundPatternColor = -603923969
          .Cell(1, 3).Shading.Texture = wdTextureNone
          .Cell(1, 3).Shading.ForegroundPatternColor = wdColorAutomatic
          .Cell(1, 3).Shading.ForegroundPatternColor = -603923969
          .Cell(1, 4).Shading.Texture = wdTextureNone
          .Cell(1, 4).Shading.ForegroundPatternColor = wdColorAutomatic
          .Cell(1, 4).Shading.ForegroundPatternColor = -603923969
           End With
           'Pour éviter le problème de tableau dans un autre tableau
           Selection.EndKey Unit:=wdStory
     
    End Sub
    Sub Sommaire()
    With ActiveDocument
            .TablesOfContents.Add Range:=Selection.Range, RightAlignPageNumbers:= _
                True, UseHeadingStyles:=True, UpperHeadingLevel:=1, _
                LowerHeadingLevel:=3, IncludePageNumbers:=True, AddedStyles:="", _
                UseHyperlinks:=True, HidePageNumbersInWeb:=True, UseOutlineLevels:= _
                True
            .TablesOfContents(1).TabLeader = wdTabLeaderDots
            .TablesOfContents.Format = wdIndexIndent
        End With
     
    End Sub

  2. #2
    Expert éminent
    Avatar de Sepia
    Homme Profil pro
    Administrateur du cursus IDE@L - Chef de Projet NCU (digital learning) - Université de Rennes
    Inscrit en
    Octobre 2007
    Messages
    3 117
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Administrateur du cursus IDE@L - Chef de Projet NCU (digital learning) - Université de Rennes
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Octobre 2007
    Messages : 3 117
    Points : 6 856
    Points
    6 856
    Par défaut
    Bonjour evaro_nathaliya et bienvenue sur DVP.com

    Ton code n'est pas mal construit mais il y a 2 problèmes, à mon avis:
    le 1er concerne la présentation. Ta macro est longue (ce qui est normal pour ton cas) et tu n'utilises pas correctement l'indentation : il s'agit de conventions (donc de règles volontaires) qui améliorent la lisibilité du code. Je l'ai changé et là j'

    Mais le principal "souci" vient du changement d'interface pour le moins déroutant : le ruban. Il y a les adeptes (qui ne viennent pas forcément de chez Microsoft ) et les détracteurs (qui ne viennent pas forcément d'Open Office ). A toi de voir mais attends toi à des réactions de toute façon puisque le ruban change l'habitude des utilisateurs.

    En plus du présent forum, de nombreuses ressources sont à ta disposition :
    - une FAQ
    - des tutoriaux
    - des critiques de livres

    Si certains éléments te paraissent encore complexes ou si tu as des besoins précis, n'hésite pas à nous solliciter, nous essayerons d'y répondre.

    A bientôt



    Sub Charte()
    '--------------------------------Recherche par blocs------------------------------------------"
    With Selection.Find
    .ClearFormatting
    .Text = "Projet :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_1 = True
    Else
    Flag_1 = False
    End If
    End With
    '---------------- tester l'existance du bloc numéro 1 ----------------------------------------'
    If Flag_1 = True Then
    MsgBox "Le projet existe deja"
    Else
    MsgBox "Insertion du projet"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Bold = True
    .Italic = False
    .Underline = wdUnderlineNone
    .UnderlineColor = wdColorAutomatic
    End With
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Projet :")
    ActiveDocument.Paragraphs.Add
    End If

    With Selection.Find
    .ClearFormatting
    .Text = "Secteur d'activité :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_2 = True
    Else
    Flag_2 = flase
    End If
    End With
    '---------------- tester l'existance du bloc numéro 2 ----------------------------------------'
    If Flag_2 = True Then
    MsgBox "Le secteur existe deja"
    ElseIf Flag_2 = False And Flag_1 = True Then
    MsgBox "Insertion du secteur aprés le projet "
    'ordre
    Selection.EndKey Unit:=wdStory
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Bold = True
    .Italic = False
    .Underline = wdUnderlineNone
    .UnderlineColor = wdColorAutomatic
    End With
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Secteur d'activité :")
    ActiveDocument.Paragraphs.Add
    Else
    MyRange.InsertAfter ("Secteur d'activité :")
    ActiveDocument.Paragraphs.Add
    End If


    With Selection.Find
    .ClearFormatting
    .Text = "Numéro du projet :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_3 = True
    Else
    Flag_3 = flase
    End If
    End With
    '---------------- tester l'existance du bloc numéro 3 ----------------------------------------'
    If Flag_3 = True Then
    MsgBox "Le Numéro du projet existe deja"
    Else
    MsgBox "Insertion du numéro du projet :"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Bold = True
    .Italic = False
    .Underline = wdUnderlineNone
    .UnderlineColor = wdColorAutomatic
    End With
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Numéro du projet :")
    ActiveDocument.Paragraphs.Add
    End If


    With Selection.Find
    .ClearFormatting
    .Text = "Tableaux des données 2013:"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_4 = True
    Else
    Flag_4 = flase
    End If
    End With
    '---------------- tester l'existance du bloc numéro 4----------------------------------------'
    If Flag_4 = True Then
    MsgBox "L'Tableaux des données 2013 existe deja"
    Else
    MsgBox "inseretion du Tableaux des données 2013:"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Bold = True
    .Italic = False
    .Underline = wdUnderlineNone
    .UnderlineColor = wdColorAutomatic
    End With
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Tableaux des données 2013:")
    Application.Run ("Tableau_CG_1")
    ActiveDocument.Paragraphs.Add
    End If


    With Selection.Find
    .ClearFormatting
    .Text = "Tableaux des données 2014 :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_5 = True
    Else
    Flag_5 = flase
    End If
    End With
    '---------------- tester l'existance du bloc numéro 5 ----------------------------------------'
    If Flag_5 = True Then
    MsgBox "Le Tableaux des données 2014 existe deja"
    Else
    MsgBox "inseretion du Tableaux des données 2014"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Bold = True
    .Italic = False
    .Underline = wdUnderlineNone
    .UnderlineColor = wdColorAutomatic
    End With
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Tableaux des données 2014:")
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Bold = False
    .Italic = False
    .Underline = wdUnderlineNone
    .UnderlineColor = wdColorAutomatic
    End With
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    Application.Run ("Tableau_CG_1")
    ActiveDocument.Paragraphs.Add
    End If


    With Selection.Find
    .ClearFormatting
    .Text = "Mot Clé :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_7 = True
    Else
    Flag_7 = flase
    End If
    End With
    '---------------- tester l'existance du bloc numéro 7 ----------------------------------------'
    If Flag_7 = True Then
    MsgBox "L'Mot Clé existe deja"
    Else
    MsgBox "inseretion de Mot Clé :"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Bold = True
    .Italic = False
    .Underline = wdUnderlineNone
    .UnderlineColor = wdColorAutomatic
    End With
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Mot Clé :")
    ActiveDocument.Paragraphs.Add
    End If


    With Selection.Find
    .ClearFormatting
    .Text = "Historique des évolutions :"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_8 = True
    Else
    Flag_8 = flase
    End If
    End With
    '---------------- tester l'existance du bloc numéro 8 ----------------------------------------'
    If Flag_8 = True Then
    On Error Resume Next
    MsgBox "Historique des évolutions existe deja"
    Else
    MsgBox "inseretion de l'Historique des évolutions :"
    Selection.EndKey Unit:=wdStory
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Bold = True
    .Italic = False
    .Underline = wdUnderlineNone
    .UnderlineColor = wdColorAutomatic
    End With
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    MyRange.InsertAfter ("Historique des évolutions :")
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Bold = False
    .Italic = False
    .Underline = wdUnderlineNone
    .UnderlineColor = wdColorAutomatic
    End With
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    Application.Run ("Tableau_CG_2")
    ActiveDocument.Paragraphs.Add
    End If


    With Selection.Find
    .ClearFormatting
    .Text = "Sommaire"
    .MatchCase = False
    .Wrap = wdFindContinue
    .Execute
    If .Found Then
    Flag_1 = True And ordre = 1
    Else
    Flag_1 = False And ordre = 0
    End If
    End With

    '---------------- tester l'existance du bloc numéro 9 ----------------------------------------'
    If Flag_9 = True Then
    On Error Resume Next
    MsgBox ("Le sommaire n'existe pas")
    Else
    MsgBox "Inserertion du sommaire "
    With Selection.Font
    .Name = "Arial"
    .Size = 12
    .Bold = False
    .Italic = False
    .Underline = wdUnderlineNone
    .UnderlineColor = wdColorAutomatic
    End With
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
    Application.Run ("Sommaire")
    ActiveDocument.Paragraphs.Add
    End If
    End Sub

  3. #3
    Expert éminent
    Avatar de Sepia
    Homme Profil pro
    Administrateur du cursus IDE@L - Chef de Projet NCU (digital learning) - Université de Rennes
    Inscrit en
    Octobre 2007
    Messages
    3 117
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 61
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Administrateur du cursus IDE@L - Chef de Projet NCU (digital learning) - Université de Rennes
    Secteur : High Tech - Multimédia et Internet

    Informations forums :
    Inscription : Octobre 2007
    Messages : 3 117
    Points : 6 856
    Points
    6 856
    Par défaut
    Bonjour evaro_nathaliya et bienvenue sur DVP.com

    Ton code n'est pas mal construit mais il y a 2 problèmes, à mon avis. Le 1er concerne la présentation. Ta macro est longue (ce qui est normal pour ton cas) et tu n'utilises pas correctement l'indentation : il s'agit de conventions (donc de règles volontaires) qui améliorent la lisibilité du code. J'ai changé l'indentation pour te montrer et là j'ai vu un problème : tu utilises "myRange" pour insérer après dans la procédure "Charte" mais tu ne l'as pas initialisé et donc tu insères au petit bonheur la chance. Tu dois modifier ton code (prends celui dans lequel j'ai fait les indentations) et initialise tes variables.

    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
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    Sub Charte()
        '--------------------------------Recherche par blocs------------------------------------------"
        With Selection.Find
            .ClearFormatting
            .Text = "Projet :"
            .MatchCase = False
            .Wrap = wdFindContinue
            .Execute
            If .Found Then
                Flag_1 = True
            Else
                Flag_1 = False
            End If
        End With
        '---------------- tester l'existance du bloc numéro 1 ----------------------------------------'
        If Flag_1 = True Then
            MsgBox "Le projet existe deja"
        Else
            MsgBox "Insertion du projet"
            Selection.EndKey Unit:=wdStory
            With Selection.Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
                .Italic = False
                .Underline = wdUnderlineNone
                .UnderlineColor = wdColorAutomatic
            End With
            Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
            MyRange.InsertAfter ("Projet :")
            ActiveDocument.Paragraphs.Add
        End If
     
        With Selection.Find
            .ClearFormatting
            .Text = "Secteur d'activité :"
            .MatchCase = False
            .Wrap = wdFindContinue
            .Execute
            If .Found Then
                Flag_2 = True
            Else
                Flag_2 = flase
            End If
        End With
        '---------------- tester l'existance du bloc numéro 2 ----------------------------------------'
        If Flag_2 = True Then
            MsgBox "Le secteur existe deja"
        ElseIf Flag_2 = False And Flag_1 = True Then
                MsgBox "Insertion du secteur aprés le projet "
                'ordre
                Selection.EndKey Unit:=wdStory
                With Selection.Font
                    .Name = "Arial"
                    .Size = 12
                    .Bold = True
                    .Italic = False
                    .Underline = wdUnderlineNone
                    .UnderlineColor = wdColorAutomatic
                End With
                Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
                MyRange.InsertAfter ("Secteur d'activité :")
                ActiveDocument.Paragraphs.Add
            Else
                MyRange.InsertAfter ("Secteur d'activité :")
                ActiveDocument.Paragraphs.Add
            End If
     
     
        With Selection.Find
            .ClearFormatting
            .Text = "Numéro du projet :"
            .MatchCase = False
            .Wrap = wdFindContinue
            .Execute
            If .Found Then
                Flag_3 = True
            Else
                Flag_3 = flase
            End If
        End With
        '---------------- tester l'existance du bloc numéro 3 ----------------------------------------'
        If Flag_3 = True Then
            MsgBox "Le Numéro du projet existe deja"
        Else
            MsgBox "Insertion du numéro du projet :"
            Selection.EndKey Unit:=wdStory
            With Selection.Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
                .Italic = False
                .Underline = wdUnderlineNone
                .UnderlineColor = wdColorAutomatic
            End With
            Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
            MyRange.InsertAfter ("Numéro du projet :")
            ActiveDocument.Paragraphs.Add
        End If
     
     
        With Selection.Find
            .ClearFormatting
            .Text = "Tableaux des données 2013:"
            .MatchCase = False
            .Wrap = wdFindContinue
            .Execute
            If .Found Then
                Flag_4 = True
            Else
                Flag_4 = flase
            End If
        End With
        '---------------- tester l'existance du bloc numéro 4----------------------------------------'
        If Flag_4 = True Then
            MsgBox "L'Tableaux des données 2013 existe deja"
        Else
            MsgBox "inseretion du Tableaux des données 2013:"
            Selection.EndKey Unit:=wdStory
            With Selection.Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
                .Italic = False
                .Underline = wdUnderlineNone
                .UnderlineColor = wdColorAutomatic
            End With
            Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
            MyRange.InsertAfter ("Tableaux des données 2013:")
            Application.Run ("Tableau_CG_1")
            ActiveDocument.Paragraphs.Add
        End If
     
     
        With Selection.Find
            .ClearFormatting
            .Text = "Tableaux des données 2014 :"
            .MatchCase = False
            .Wrap = wdFindContinue
            .Execute
            If .Found Then
                Flag_5 = True
            Else
                Flag_5 = flase
            End If
        End With
        '---------------- tester l'existance du bloc numéro 5 ----------------------------------------'
        If Flag_5 = True Then
            MsgBox "Le Tableaux des données 2014 existe deja"
        Else
            MsgBox "inseretion du Tableaux des données 2014"
            Selection.EndKey Unit:=wdStory
            With Selection.Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
                .Italic = False
                .Underline = wdUnderlineNone
                .UnderlineColor = wdColorAutomatic
            End With
            Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
            MyRange.InsertAfter ("Tableaux des données 2014:")
            With Selection.Font
                .Name = "Arial"
                .Size = 12
                .Bold = False
                .Italic = False
                .Underline = wdUnderlineNone
                .UnderlineColor = wdColorAutomatic
            End With
            Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
            Application.Run ("Tableau_CG_1")
            ActiveDocument.Paragraphs.Add
        End If
     
     
        With Selection.Find
            .ClearFormatting
            .Text = "Mot Clé :"
            .MatchCase = False
            .Wrap = wdFindContinue
            .Execute
            If .Found Then
                Flag_7 = True
            Else
                Flag_7 = flase
            End If
        End With
        '---------------- tester l'existance du bloc numéro 7 ----------------------------------------'
        If Flag_7 = True Then
            MsgBox "L'Mot Clé  existe deja"
        Else
            MsgBox "inseretion de Mot Clé :"
            Selection.EndKey Unit:=wdStory
            With Selection.Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
                .Italic = False
                .Underline = wdUnderlineNone
                .UnderlineColor = wdColorAutomatic
            End With
            Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
            MyRange.InsertAfter ("Mot Clé :")
            ActiveDocument.Paragraphs.Add
        End If
     
     
        With Selection.Find
            .ClearFormatting
            .Text = "Historique des évolutions :"
            .MatchCase = False
            .Wrap = wdFindContinue
            .Execute
            If .Found Then
                Flag_8 = True
            Else
                Flag_8 = flase
            End If
        End With
        '---------------- tester l'existance du bloc numéro 8 ----------------------------------------'
        If Flag_8 = True Then
            On Error Resume Next
            MsgBox "Historique des évolutions existe deja"
        Else
            MsgBox "inseretion de l'Historique des évolutions :"
            Selection.EndKey Unit:=wdStory
            With Selection.Font
                .Name = "Arial"
                .Size = 12
                .Bold = True
                .Italic = False
                .Underline = wdUnderlineNone
                .UnderlineColor = wdColorAutomatic
            End With
            Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
            MyRange.InsertAfter ("Historique des évolutions :")
            With Selection.Font
                .Name = "Arial"
                .Size = 12
                .Bold = False
                .Italic = False
                .Underline = wdUnderlineNone
                .UnderlineColor = wdColorAutomatic
            End With
            Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
            Application.Run ("Tableau_CG_2")
            ActiveDocument.Paragraphs.Add
        End If
     
     
        With Selection.Find
            .ClearFormatting
            .Text = "Sommaire"
            .MatchCase = False
            .Wrap = wdFindContinue
            .Execute
            If .Found Then
                Flag_1 = True And ordre = 1
            Else
                Flag_1 = False And ordre = 0
        End If
        End With
     
        '---------------- tester l'existance du bloc numéro 9 ----------------------------------------'
        If Flag_9 = True Then
            On Error Resume Next
            MsgBox ("Le sommaire n'existe pas")
        Else
            MsgBox "Inserertion du sommaire "
            With Selection.Font
                .Name = "Arial"
                .Size = 12
                .Bold = False
                .Italic = False
                .Underline = wdUnderlineNone
                .UnderlineColor = wdColorAutomatic
            End With
            Selection.ParagraphFormat.Alignment = wdAlignParagraphLeft
            Application.Run ("Sommaire")
            ActiveDocument.Paragraphs.Add
        End If
    End Sub
    En plus du présent forum, de nombreuses ressources sont à ta disposition :
    - une FAQ
    - des tutoriaux
    - des critiques de livres

    Si certains éléments te paraissent encore complexes ou si tu as des besoins précis, n'hésite pas à nous solliciter, nous essayerons d'y répondre.

    A bientôt

Discussions similaires

  1. Réponses: 1
    Dernier message: 06/04/2009, 10h19
  2. macro impression sous word
    Par foliedti2 dans le forum VBA Word
    Réponses: 3
    Dernier message: 13/02/2008, 12h23
  3. [VBA-W] Macro de fermeture sous word
    Par liop49 dans le forum VBA Word
    Réponses: 2
    Dernier message: 02/01/2007, 12h02
  4. macro sous word XP
    Par delamarque dans le forum VBA Word
    Réponses: 4
    Dernier message: 05/12/2005, 10h15
  5. [VB][WORD] file browser dialog en macro VB sous word ?
    Par Nycos62 dans le forum VBA Word
    Réponses: 1
    Dernier message: 17/09/2005, 15h23

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