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 :

Rechercher et afficher un text dans un fichier Word avec macro Excel


Sujet :

Macros et VBA Excel

  1. #1
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2010
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2010
    Messages : 185
    Points : 167
    Points
    167
    Par défaut Rechercher et afficher un text dans un fichier Word avec macro Excel
    Bonjour,
    Je suis sur un petit problème technique que je ne comprend pas et que je n'arrive pas à résoudre du coup.

    En fait, dans un fichier excel, je souhaite ouvrir un fichier word et chercher du texte contenu dans la cellule A1.

    J'ai fait 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
    Sub Test()
    Dim appWrd As Word.Application
    Dim docWord As Word.Document
     
    Set appWrd = CreateObject("Word.Application")
    appWrd.Visible = True
    Set docWord = appWrd.Documents.Open(ActiveCell.Text, ReadOnly:=True)
     
    With docWord.Content.Find
    .ClearFormatting
    .Execute Cells(1).Text
    End With
    End Sub
    Malheureusement, ça ne sélectionne pas le texte trouvé (j'ai même fait
    If .Execute(Cells(1).Text) Then MsgBox "OK" et la boîte s'affiche, donc c'est que le texte est trouvé).

    Alors que le code suivant mis directement dans Word fonctionne comme attendu.

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    With Selection.Find
    .ClearFormatting
    .Execute Cells(1).Text
    End With
    Quelqu'un aurait-il une idée ?

    Merci

    P.S : Bien sur, il y a plusieurs fichiers word et le texte à chercher peut être modifié.

  2. #2
    Futur Membre du Club
    Profil pro
    Inscrit en
    Octobre 2010
    Messages
    6
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Octobre 2010
    Messages : 6
    Points : 7
    Points
    7
    Par défaut
    Le text a chercher est sur excel ?? ou sur word ?

  3. #3
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2010
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2010
    Messages : 185
    Points : 167
    Points
    167
    Par défaut
    Le texte définissant la recherche est dans Excel, et il est à chercher dans le fichier Word.

  4. #4
    Expert éminent sénior
    Avatar de kiki29
    Homme Profil pro
    ex Observeur CGG / Analyste prog.
    Inscrit en
    Juin 2006
    Messages
    6 132
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : ex Observeur CGG / Analyste prog.

    Informations forums :
    Inscription : Juin 2006
    Messages : 6 132
    Points : 11 274
    Points
    11 274
    Par défaut
    Salut, extrait de l'aide en ligne [F1] sur Find

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    With Selection.Find
        .Forward = True
        .ClearFormatting
        .MatchWholeWord = True
        .MatchCase = False
        .Wrap = wdFindContinue
        .Execute FindText:="Microsoft"
    End With

  5. #5
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2010
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2010
    Messages : 185
    Points : 167
    Points
    167
    Par défaut
    Merci kiki29, mais ces paramètres n'ont aucun effet

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    Dim appWrd As Word.Application
    Dim docWord As Word.Document
     
    Set appWrd = CreateObject("Word.Application")
    appWrd.Visible = True
    Set docWord = appWrd.Documents.Open(ActiveCell.Text, ReadOnly:=True)
     
    With docWord.Content.Find
        .ClearFormatting
        .MatchWholeWord = True
        .MatchCase = False
        .Execute Cells(1).Text
    End With
    Quelqu'un a une autre piste ?

  6. #6
    Expert éminent sénior
    Avatar de kiki29
    Homme Profil pro
    ex Observeur CGG / Analyste prog.
    Inscrit en
    Juin 2006
    Messages
    6 132
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Finistère (Bretagne)

    Informations professionnelles :
    Activité : ex Observeur CGG / Analyste prog.

    Informations forums :
    Inscription : Juin 2006
    Messages : 6 132
    Points : 11 274
    Points
    11 274
    Par défaut
    Re, ooops erreur basique
    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
    Option Explicit
     
    Sub Trouver()
    Dim WordApp As Word.Application
    Dim WordDoc As Word.Document
    Dim sStr As String, sNom As String
     
        sStr = Feuil1.Range("A1").Text
        sNom = ThisWorkbook.Path & "\" & "Test.doc"
     
        Set WordApp = New Word.Application
        Set WordDoc = WordApp.Documents.Open(Filename:=sNom, ReadOnly:=True)
     
        WordApp.Visible = True
     
        WordApp.Selection.HomeKey wdStory
     
        With WordApp.Selection.Find
            .Forward = True
            .ClearFormatting
            .MatchWholeWord = True
            .MatchCase = False
            .Execute FindText:=sStr
        End With
     
        Set WordApp = Nothing
    End Sub

  7. #7
    Membre habitué
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Novembre 2010
    Messages
    185
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Haut Rhin (Alsace)

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : Conseil

    Informations forums :
    Inscription : Novembre 2010
    Messages : 185
    Points : 167
    Points
    167
    Par défaut
    C'est tout bon, je partais du mauvais objet (le document alors qu'il fallait partir de l'application pour bénéficier de l'objet selection).

    Un grand merci pour avoir corrigé cette erreur et méprise.

Discussions similaires

  1. [AC-2010] afficher des photos dans un fichier word
    Par da_latifa dans le forum VBA Access
    Réponses: 4
    Dernier message: 13/01/2011, 05h18
  2. ecrire du texte dans un fichier word
    Par sonia5 dans le forum ASP.NET
    Réponses: 2
    Dernier message: 16/02/2010, 01h09
  3. [WD-2007] Recherche remplace de motifs date dans un fichier word
    Par rosemary_antoine dans le forum VBA Word
    Réponses: 1
    Dernier message: 16/06/2009, 01h22
  4. Lire texte dans un fichier word
    Par ramykawkab dans le forum VB.NET
    Réponses: 0
    Dernier message: 04/06/2009, 10h59
  5. [phpToPDF] Comment afficher du texte dans le fichier pdf généré ?
    Par ginger4957 dans le forum Bibliothèques et frameworks
    Réponses: 4
    Dernier message: 18/05/2009, 11h16

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