Twitter iPhone pliant OnePlus 11 PS5 Disney+ Orange Livebox Windows 11

Recherche de texte word à partir d'Excel

1 réponse
Avatar
bullrot94
Bonjour à tous,
Voici mon problème.
J’ai un fichier Excel comportant un lien hypertexte vers un fichier Word.
Je souhaiterais qu’une macro :
- ouvre le fichier Word
- recherche dans ce fichier un mot bien précis (« date d’application »)
- copie un nombre de caractères qui suivent ce mot dans une cellule (dans mon cas C5)
- ferme le fichier Word.

Cette macro me permet de tenir à jour mon fichier Excel, le copier-coller par liaison n’étant pas fiable (lignes amenées à être décalées dans le fichier word).

Je vous remercie d’avance pour votre aide, ci-dessous ma macro qui bugge :

Sub test()

Range("B5").Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True


Selection.Find.ClearFormatting
With Selection.Find
.Text = "date d'application"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=4
Selection.MoveRight Unit:=wdCharacter, Count:=10, Extend:=wdExtend
Selection.Copy
ActiveWindow.Close
Application.Quit

Range("C5").Select
ActiveSheet.Paste

End Sub

1 réponse

Avatar
Daniel.C
Bonjour.
La macro suivante copie les 10 caractères suivant « date d’application ».
Change le chemin et le nom du document Word suivant tes besoins. Je n'ai pas
besoin de l'hyperlien, mais je peux l'utiliser pour récupérer le nom du
document Word. Dans la fenêtre VBE, clique sur "Outils" et "Références" et
coche "Microsoft Word nn Object Library" :

Sub CopyWordExcel()
Dim Wordobj As Object
Set Wordobj = CreateObject("Word.Application")
'Set Wordobj = GetObject(, "Word.Application")
Wordobj.Visible = True
Wordobj.Documents.Open "E:donneesdanielmpfetest.DOC"
With Wordobj.Selection.Find
.ClearFormatting
.Text = " date d’application "
.Execute Forward:=True
End With
Wordobj.Selection.MoveRight Unit:=wdCharacter, Count:=1
Wordobj.Selection.MoveRight Unit:=wdCharacter, Count:,
Extend:=wdExtend
Wordobj.Selection.Copy
[C5].Select
ActiveSheet.Paste
Wordobj.ActiveDocument.Close wdSaveChanges
End Sub



--
Cordialement.
Daniel
"bullrot94" a écrit dans le message de news:

Bonjour à tous,
Voici mon problème.
J’ai un fichier Excel comportant un lien hypertexte vers un fichier Word.
Je souhaiterais qu’une macro :
- ouvre le fichier Word
- recherche dans ce fichier un mot bien précis (« date d’application »)
- copie un nombre de caractères qui suivent ce mot dans une cellule (dans
mon
cas C5)
- ferme le fichier Word.

Cette macro me permet de tenir à jour mon fichier Excel, le copier-coller
par
liaison n’étant pas fiable (lignes amenées à être décalées dans le fichier
word).

Je vous remercie d’avance pour votre aide, ci-dessous ma macro qui bugge :

Sub test()

Range("B5").Select
Selection.Hyperlinks(1).Follow NewWindow:úlse, AddHistory:=True


Selection.Find.ClearFormatting
With Selection.Find
.Text = "date d'application"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
Selection.MoveRight Unit:=wdCharacter, Count:=4
Selection.MoveRight Unit:=wdCharacter, Count:, Extend:=wdExtend
Selection.Copy
ActiveWindow.Close
Application.Quit

Range("C5").Select
ActiveSheet.Paste

End Sub