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 :

Rechercher/Remplacer en VB avec multiples sections


Sujet :

VBA Word

  1. #1
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 9
    Points : 8
    Points
    8
    Par défaut Rechercher/Remplacer en VB avec multiples sections
    Bonjour,

    J'essaye en vain de faire une fonction rechercher/remplacer en VB car j'ai 500 documents à modifier.

    Le texte à remplacer se trouve dans le corps du document et dans les en-têtes et pieds de pages, qui sont sous différentes sections.

    Ca semble simple et pourtant ça ne fonctionne pas. Voila le code de ma macro. Elle est une répétition de recherche/remplace sur les sections de headers/footers puis un recherche/remplace sur le document lui-même.

    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
     
    Sub RechercheRemplace()
     
     
        On Error Resume Next
     
        If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
            ActiveWindow.Panes(2).Close
        End If
        If ActiveWindow.ActivePane.View.Type = wdNormalView Or ActiveWindow. _
            ActivePane.View.Type = wdOutlineView Then
            ActiveWindow.ActivePane.View.Type = wdPrintView
        End If
     
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
     
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
     
        ActiveWindow.ActivePane.View.NextHeaderFooter
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
     
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
     
        ActiveWindow.ActivePane.View.NextHeaderFooter
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
     
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
     
        ActiveWindow.ActivePane.View.NextHeaderFooter
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageHeader
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
     
        ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
     
        ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
     
    End Sub
    J'ai enlevé volontairement les textes.

    Le résultat est que les footers semblent modifiés mais pas les headers.
    Je ne vois pas ce qui bloque, avez vous une piste ?

    Merci d'avance

  2. #2
    Rédacteur/Modérateur

    Avatar de Heureux-oli
    Homme Profil pro
    Contrôleur d'industrie
    Inscrit en
    Février 2006
    Messages
    21 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 59
    Localisation : Belgique

    Informations professionnelles :
    Activité : Contrôleur d'industrie
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 21 087
    Points : 42 926
    Points
    42 926
    Par défaut
    Salut,

    Le premier problème que tu rencontres est lié à la sélection des tes entêtes.

    Pour accéder aux entêtes et pieds de pages, nul besoin de les ouvrir.


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.text
    Avec Section(X) la section souhaitée.

    Si tu utilises une variable, tu peux boucler sur toutes les sections de ton document et de cette manière, tu évite à Word de gérer l'affichage des entête et pieds de page.

    Il faut imbriquer les boucles les unes dans les autres.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    For Each MyDoc in ....
    For Each X in myDoc.Sections.count
    myDoc.Section(X)....
    Et pour chaque morceau de document, tu lances le R/R

  3. #3
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 9
    Points : 8
    Points
    8
    Par défaut
    ok !

    Voila donc ce que j'ai fait pour aller au plus vite :

    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
     
    (...)
        ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
     
        ActiveDocument.Sections(2).Headers(wdHeaderFooterPrimary).Range.Text
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    (...)
    Problème, word plante sur la ligne"ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text" et me dit "invalid use of property" sur le ".Text"

    Pas facile à debugger sans trop connaitre le VB !

    Merci de ton aide !

  4. #4
    Rédacteur/Modérateur

    Avatar de Heureux-oli
    Homme Profil pro
    Contrôleur d'industrie
    Inscrit en
    Février 2006
    Messages
    21 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 59
    Localisation : Belgique

    Informations professionnelles :
    Activité : Contrôleur d'industrie
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 21 087
    Points : 42 926
    Points
    42 926
    Par défaut
    Salut,

    C'est de ma faute.

    Il faut remplacer Text par Select, une recherche ne se fait que sur une selection.

  5. #5
    Futur Membre du Club
    Profil pro
    Inscrit en
    Mai 2008
    Messages
    9
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2008
    Messages : 9
    Points : 8
    Points
    8
    Par défaut
    Malheureusement ça ne fonctionne pas.
    Le footer n'est pas modifié dans la section 2.
    Je désespère...

  6. #6
    Rédacteur/Modérateur

    Avatar de Heureux-oli
    Homme Profil pro
    Contrôleur d'industrie
    Inscrit en
    Février 2006
    Messages
    21 087
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 59
    Localisation : Belgique

    Informations professionnelles :
    Activité : Contrôleur d'industrie
    Secteur : Aéronautique - Marine - Espace - Armement

    Informations forums :
    Inscription : Février 2006
    Messages : 21 087
    Points : 42 926
    Points
    42 926
    Par défaut
    Salut,

    Je viens de faire un rapide test avec ce code :

    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
     
    Sub remplacer()
    Dim intx As Integer
    For intx = 1 To ActiveDocument.Sections.Count
    ActiveDocument.Sections(intx).Headers(wdHeaderFooterPrimary).Range.Select
    With Selection.Find
            .Text = "Texte"
            .Replacement.Text = "Bonjour"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = False
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
     
    Next intx
    End Sub
    Il me remplace bien le mot Texte dans mes sections par le mot Bonjour.

    Maintenant, tu dois être certains de tes Headers ou Footers, il en existe trois sortes.

Discussions similaires

  1. Macro rechercher remplace multiple
    Par supai dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 19/02/2010, 08h34
  2. VB rechercher/remplacer en masse et avec parametre
    Par newbi_ dans le forum VBA Word
    Réponses: 0
    Dernier message: 11/01/2010, 15h01
  3. [RegEx] Rechercher/remplacer avec preg_replace
    Par olbouss dans le forum Langage
    Réponses: 5
    Dernier message: 13/01/2009, 12h00
  4. [VS] rechercher/remplacer avec des regexp
    Par v1nce dans le forum Visual Studio
    Réponses: 4
    Dernier message: 03/07/2007, 14h58
  5. [RegEx] Rechercher et remplacer plusieurs occurences avec expression
    Par nabab dans le forum Langage
    Réponses: 2
    Dernier message: 12/12/2005, 17h12

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