![Encontre todas as ocorrências de um texto e transforme-o em um hiperlink com uma macro](https://rvso.com/image/1475813/Encontre%20todas%20as%20ocorr%C3%AAncias%20de%20um%20texto%20e%20transforme-o%20em%20um%20hiperlink%20com%20uma%20macro.png)
Obrigatório
Quero encontrar todas as ocorrências de um único texto em um documento do MS Word, transformar cada ocorrência em um hiperlink e alterar o estilo genérico do hiperlink para um de minha escolha.
O que eu tenho
Como não tenho ideia de como atingir o requisito acima como um todo, comecei por uma parte dele, ou seja, encontrar uma única instância e adaptá-la.
Então, gravei uma macro, que resultou no código a seguir. Esse código eu adaptei para que o sub pudesse pegar parâmetros para ohiperlinkTextoe o hiperlinksubendereço:
Sub AutoDetectHyperlinksForText(hyperlinkText As String, subaddress As String, style As String)
Selection.Find.ClearFormatting
With Selection.Find
.Text = hyperlinkText
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:="", _
subaddress:=subaddress, ScreenTip:="", TextToDisplay:= _
hyperlinkText
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
Selection.Style = ActiveDocument.Styles(style)
End Sub
Com isso é fácilChamaro sub para instâncias de várias palavras, como:
Call AutoDetectHyperlinksForText("broadcasts", "_broadcastService", "Subtle Emphasis")
Pergunta
Como posso adaptar esta macro para verificar todo o documento?
Pergunta bônus
Existe uma maneira de modificar o script acima para que eu possa armazenar minha seleção e eliminar a necessidade do .MoveLeft
?
Em pseudocódigo seria algo assim:
Dim mySelect as Selection
mySelect = Selection.Find.Execute
ActiveDocument.Hyperlinks.Add Anchor:=mySelect.Range, Address:="", _
subaddress:=subaddress, ScreenTip:="", TextToDisplay:= _
hyperlinkText
mySelect.Style = ActiveDocument.Styles("Subtle Emphasis")
Responder1
Isso encontrará a palavra "google" (não googles ou googled) e criará um hiperlink parahttp:\\google.com
Também aplica o estilo
Sub FindAndHyperlink()
'define the style
Dim strStyle As String
strStyle = "Subtle Emphasis"
'set the search range
Dim rngSearch As Range
Set rngSearch = ActiveDocument.Range
'set the search string
Dim strSearch As String
strSearch = "google"
'set the target address for the hyperlink
Dim strAddress As String
strAddress = "http:\\google.com"
With rngSearch.Find
Do While .Execute(findText:=strSearch, MatchWholeWord:=True, Forward:=True) = True
With rngSearch 'we will work with what is found as it will be the selection
ActiveDocument.Hyperlinks.Add Anchor:=rngSearch, Address:=strAddress
.Style = ActiveDocument.Styles(strStyle) 'throw the style on it after the link
End With
rngSearch.Collapse Direction:=wdCollapseEnd
'keep it moving
Loop
End With
End Sub
Obviamente, se você quiser passar argumentos você pode fazer isso para que ele possa ser chamado com seus parâmetros, mas esta é a base do que você precisa.