Bonjour à toutes et à tous,
Je tente de faire fonctionner une macro vba à partir d'excel 365, macro qui donne pleinement satisfaction lorsqu'elle est exécutée dans Word 365 .
La macro source d'origine est disponible ici : https://stackoverflow.com/questions/77284063/a-macro-to-search-a-word-document-for-a-string-and-change-it-to-a-hyperlink
Je reprends une partie du code :
'Find all instances of text `findWhat` in document `doc`, and
' apply a hyperlink using URL `linkURL`
'Add a fourth parameter if you also need to control the link text.
Sub Linkify(doc As Document, findWhat As String, linkURL As String)
Dim rng As Range, col As New Collection, i As Long
Set rng = doc.Range
ResetFind rng.Find
Do While rng.Find.Execute(findtext:=findWhat)
' `rng` is now the found text
col.Add rng.Duplicate 'collect all found ranges
Loop
Debug.Print col.Count & " instances of '" & findWhat & "'"
For i = col.Count To 1 Step -1 'link all found ranges
doc.Hyperlinks.Add Anchor:=col(i), Address:=linkURL, _
SubAddress:="", ScreenTip:="", TextToDisplay:=col(i).Text
Next i
End Sub
Sub ResetFind(f As Find)
With f
.ClearFormatting
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
End Sub
Sous excel, moyennant l'ajout de la référence Object library Word 16.0, le code se déroule bien jusqu'à l'instruction :
ResetFind rng.Find
Où excel me met le message suivant : "Erreur de compilation - Argument non facultatif" ...
Je me répète : la même séquence dans Word ne fait pas apparaitre cette erreur de compilation et s’exécute parfaitement ...
Quelqu'un aurait une piste à me proposer ?
Partager