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 PowerPoint Discussion :

Création d'une macro pour rechercher et remplacer un mot dans toute la présentation


Sujet :

VBA PowerPoint

  1. #1
    Nouveau Candidat au Club
    Homme Profil pro
    Finance
    Inscrit en
    Avril 2019
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Finance

    Informations forums :
    Inscription : Avril 2019
    Messages : 1
    Points : 1
    Points
    1
    Par défaut Création d'une macro pour rechercher et remplacer un mot dans toute la présentation
    Bonjour à tous,

    J'essaie de développer un bout de code qui me permettrait de remplacer dans une présentation powerpoint, un mot par un autre dans tout le document (composé d'environ 50 slides)

    J'ai trouvé une macro déjà existante sur internet mais en l'essayant, elle me renvoie une erreur d'execution 13: Incompatibilité de type (ligne en rouge dans le code ci-dessous avec le debogueur)...

    Si quelqu'un pouvait m'aiguiller sur la raison de cette erreur, je lui en serai extremement reconnaissant!

    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
    Sub FindReplaceAll()
    
    Dim sld As Slide
    Dim shp As Shape
    Dim ShpTxt As TextRange
    Dim TmpTxt As TextRange
    Dim FindWord As Variant
    Dim ReplaceWord As Variant
    
    Dim PptApp As PowerPoint.Application
    Dim PptDoc As PowerPoint.Presentation
    Set PptApp = CreateObject("Powerpoint.Application")
    PptApp.Visible = True
    
    FindWord = "United States"
    ReplaceWord = InputBox("Saisir le mois de présentation du rapport")
    
    'Loop through each slide in Presentation
      For Each sld In PptApp.ActivePresentation.Slides
            
            For Each shp In sld.Shapes
        
          'Store shape text into a variable
            Set ShpTxt = shp.TextFrame.TextRange
          
          'Ensure There is Text To Search Through
            If ShpTxt <> "" Then
      
              'Store text into a variable
                Set ShpTxt = shp.TextFrame.TextRange
              
              'Find First Instance of "Find" word (if exists)
                Set TmpTxt = ShpTxt.Replace( _
                 FindWhat:=FindWord, _
                 Replacewhat:=ReplaceWord, _
                 WholeWords:=True)
          
              'Find Any Additional instances of "Find" word (if exists)
                Do While Not TmpTxt Is Nothing
                  Set ShpTxt = ShpTxt.Characters(TmpTxt.Start + TmpTxt.Length, ShpTxt.Length)
                  
                  Set TmpTxt = ShpTxt.Replace( _
                   FindWhat:=FindWord, _
                   Replacewhat:=ReplaceWord, _
                   WholeWords:=True)
                Loop
              
            End If
            
        Next shp
          
      Next sld
    
    End Sub

  2. #2
    Nouveau Candidat au Club
    Homme Profil pro
    DataScientist
    Inscrit en
    Avril 2019
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 27
    Localisation : France, Eure et Loir (Centre)

    Informations professionnelles :
    Activité : DataScientist

    Informations forums :
    Inscription : Avril 2019
    Messages : 1
    Points : 1
    Points
    1
    Par défaut
    Salut,
    Il est possible que ton objet Shape ne soit pas du texte (une flèche par exemple), ainsi tu ne peux pas rechercher un mot dans cet objet.
    Essaye ça, si ça fonctionne.

    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 FindReplaceAll()
    
    Dim sld As Slide
    Dim shp As Shape
    Dim ShpTxt As TextRange
    Dim TmpTxt As TextRange
    Dim FindWord As Variant
    Dim ReplaceWord As Variant
    
    Dim PptApp As PowerPoint.Application
    Dim PptDoc As PowerPoint.Presentation
    Set PptApp = CreateObject("Powerpoint.Application")
    PptApp.Visible = True
    
    On Error Resume Next
    
    FindWord = "United States"
    ReplaceWord = InputBox("Saisir le mois de présentation du rapport")
    
    'Loop through each slide in Presentation
      For Each sld In PptApp.ActivePresentation.Slides
            
            For Each shp In sld.Shapes
        
          'Store shape text into a variable
            Set ShpTxt = shp.TextFrame.TextRange
          
          'Ensure There is Text To Search Through
            If ShpTxt <> "" Then
      
              'Store text into a variable
                Set ShpTxt = shp.TextFrame.TextRange
              
              'Find First Instance of "Find" word (if exists)
                Set TmpTxt = ShpTxt.Replace( _
                 FindWhat:=FindWord, _
                 Replacewhat:=ReplaceWord, _
                 WholeWords:=True)
          
              'Find Any Additional instances of "Find" word (if exists)
                Do While Not TmpTxt Is Nothing
                  Set ShpTxt = ShpTxt.Characters(TmpTxt.Start + TmpTxt.Length, ShpTxt.Length)
                  
                  Set TmpTxt = ShpTxt.Replace( _
                   FindWhat:=FindWord, _
                   Replacewhat:=ReplaceWord, _
                   WholeWords:=True)
                Loop
              
            End If
            
        Next shp
          
      Next sld
    
    End Sub

Discussions similaires

  1. [WD-2003] Macro pour rechercher et remplacer sur table Index
    Par milia123 dans le forum VBA Word
    Réponses: 0
    Dernier message: 12/01/2013, 15h02
  2. [XL-2007] création d'une macro pour mettre à jour une BDD
    Par morguie31 dans le forum Macros et VBA Excel
    Réponses: 11
    Dernier message: 17/11/2012, 16h10
  3. Création d'une macro pour générer un xml depuis excel
    Par vieri dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 16/12/2011, 09h46
  4. [XL-2003] création d'une macro pour formulaire
    Par Amadeon dans le forum Macros et VBA Excel
    Réponses: 4
    Dernier message: 18/05/2010, 12h56
  5. Création d'une macro pour remplissage auto
    Par Laura-c dans le forum Macros et VBA Excel
    Réponses: 25
    Dernier message: 19/06/2008, 16h40

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