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

Macros et VBA Excel Discussion :

Find & Replace du texte dans un tableau PPT


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Nouveau candidat au Club
    Homme Profil pro
    Administrateur de base de données
    Inscrit en
    Août 2015
    Messages
    1
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Ille et Vilaine (Bretagne)

    Informations professionnelles :
    Activité : Administrateur de base de données

    Informations forums :
    Inscription : Août 2015
    Messages : 1
    Par défaut Find & Replace du texte dans un tableau PPT
    Bonjour à vous,

    J'utilise une macro qui cherche du texte et le remplace dans un fichier ppt : cela me permet de créer des documents ppt personnalisés rapidement.
    Ces derniers jours, j'ai essayé de rajouter des tableaux dans mon ppt et j'ai découvert que ma sub ne va pas dans les cellules de mon tableau pour trouver et remplacer le texte.

    En fait ma Sub va dans les slides, puis dans les shapes, puis dans les textframes et cherche le texte pour le remplacer si besoin.

    Je souhaiterais que ma sub aille également dans les tableaux en plus des textframes mais je ne sais pas comment faire !
    merci beaucoup par avance à ceux qui m'aideront !

    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
    Sub Switch(pptDoc As PowerPoint.Presentation, FindString As String, ReplaceString As Variant)
        With pptDoc
            For Each oSld In .Slides
                For Each oShp In oSld.Shapes
                    If oShp.HasTextFrame Then
                        If oShp.TextFrame.HasText Then
                            Set oTxtRng = oShp.TextFrame.TextRange
                            Set oTxtFnd = oTxtRng.Find(FindWhat:=FindString)
     
                            Do While Not oTxtFnd Is Nothing
                            Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, Replacewhat:=ReplaceString, After:=0, MatchCase:=True, WholeWords:=False)
                            Set oTxtFnd = oTxtRng.Find(FindWhat:=FindString)
                            Loop
     
                    Else:   Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, Replacewhat:=ReplaceString, After:=0, MatchCase:=True, WholeWords:=False)
                            Do While Not oTmpRng Is Nothing
                            Set oTmpRng = oTxtRng.Replace(FindWhat:=FindString, Replacewhat:=ReplaceString, After:=0, MatchCase:=True, WholeWords:=False)
                            Loop
     
                        End If
                    End If
                Next oShp
            Next oSld
        End With
    End Sub

  2. #2
    Expert confirmé
    Homme Profil pro
    retraité
    Inscrit en
    Juin 2012
    Messages
    3 440
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Belgique

    Informations professionnelles :
    Activité : retraité
    Secteur : Associations - ONG

    Informations forums :
    Inscription : Juin 2012
    Messages : 3 440
    Par défaut
    Bonjour,

    A tester, cette version plus complète:
    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
    '--- https://stackoverflow.com/questions/18062066/how-to-modify-text-in-powerpoint-via-excel-vba-without-changing-style
     
    Sub Replace_in_Shapes_and_Tables(pPPTFile As Presentation, sFromStr As String, sToStr As String)
        Dim sld         As Slide
        Dim shp         As Shape
        Dim i           As Long
        Dim j           As Long
        Dim m           As Long
        Dim trFoundText As TextRange
     
        For Each sld In pPPTFile.Slides
            For Each shp In sld.Shapes
     
                If shp.HasTextFrame Then
                    If shp.TextFrame.HasText Then                   ' only perform action on shape if it contains the target string
                        Set trFoundText = shp.TextFrame.TextRange.Find(sFromStr)
                        If Not (trFoundText Is Nothing) Then
                            m = shp.TextFrame.TextRange.Find(sFromStr).Characters.Start
                            shp.TextFrame.TextRange.Characters(m).InsertBefore (sToStr)
                            shp.TextFrame.TextRange.Find(sFromStr).Delete
                        End If
                    End If
                End If
     
                If shp.HasTable Then
                    For i = 1 To shp.Table.Rows.Count
                        For j = 1 To shp.Table.Columns.Count
     
                            Set trFoundText = shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Find(sFromStr)
                            If Not (trFoundText Is Nothing) Then
                                m = shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Find(sFromStr).Characters.Start
                                shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Characters(m).InsertBefore (sToStr)
                                shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Find(sFromStr).Delete
                            End If
     
                        Next j
                    Next i
                End If
     
            Next shp
        Next sld
     
        For Each shp In pPPTFile.SlideMaster.Shapes
            If shp.HasTextFrame Then
                If shp.TextFrame.HasText Then
                    Set trFoundText = shp.TextFrame.TextRange.Find(sFromStr)
                    If Not (trFoundText Is Nothing) Then
                        m = shp.TextFrame.TextRange.Find(sFromStr).Characters.Start
                        shp.TextFrame.TextRange.Characters(m).InsertBefore (sToStr)
                        shp.TextFrame.TextRange.Find(sFromStr).Delete
                    End If
     
                End If
            End If
     
            If shp.HasTable Then
                For i = 1 To shp.Table.Rows.Count
                    For j = 1 To shp.Table.Columns.Count
                        Set trFoundText = shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Find(sFromStr)
                        If Not (trFoundText Is Nothing) Then
                            m = shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Find(sFromStr).Characters.Start
                            shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Characters(m).InsertBefore (sToStr)
                            shp.Table.Rows.Item(i).Cells(j).Shape.TextFrame.TextRange.Find(sFromStr).Delete
                        End If
     
                    Next j
                Next i
            End If
        Next shp
     
    End Sub
    Cordialement.

Discussions similaires

  1. [Tableaux] Stocker un fichier texte dans un tableau
    Par clairette59 dans le forum Langage
    Réponses: 13
    Dernier message: 27/01/2006, 23h48
  2. Charger un ficher Texte dans un tableau
    Par bonjour69 dans le forum C
    Réponses: 4
    Dernier message: 12/10/2005, 20h15
  3. [Débutant]Changer du text dans un tableau
    Par Azimel dans le forum Général JavaScript
    Réponses: 1
    Dernier message: 17/09/2005, 18h38
  4. Centrer des images et du texte dans un tableau
    Par hstlaurent dans le forum Balisage (X)HTML et validation W3C
    Réponses: 8
    Dernier message: 30/08/2005, 16h34
  5. Réponses: 5
    Dernier message: 15/05/2005, 08h51

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