Converter todos os links de texto simples em hiperlinks

3

Eu tenho um arquivo do Word bastante grande (+1.100 páginas) que contém links de texto sem formatação da cobertura da mídia. Por exemplo, o documento do Word contém:

The News Leader (AP) Va. police officer who died on her first shift laid to rest http://www.newsleader.com/story/news/nation-now/2016/03/01/va-police-officer-who-died-her-first-shift-laid-rest/81183272/ 03.01.16

Livingston Daily Va. police officer who died on her first shift laid to rest http://www.livingstondaily.com/story/news/nation-now/2016/03/01/va-police-officer-who-died-her-first-shift-laid-rest/81183272/ 03.01.16

(No documento do Word, esses links não são hiperlinks, apenas texto!)

Atualmente, a maneira como adquirimos os dados e os mesclamos no Word formata os links como texto simples em vez de hiperlinks.

Gostaria de usar o VBA para converter todos esses links de texto simples em hiperlinks clicáveis, preservando o texto do link (por exemplo, hiperlinks com o texto âncora do link restante).

Eu encontrei exemplos de código para encontrar e substituir strings específicas com links específicos, por exemplo:

Sub FindAndHyperlink()
    'define the style
    Dim strStyle As String
    strStyle = "Normal"
    'set the search range
    Dim rngSearch As Range
    Set rngSearch = ActiveDocument.Range
    'set the search string
    Dim strSearch As String
    strSearch = "tuesday"
    '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

Mas não consigo descobrir como alterar dinamicamente as funções de pesquisa / seleção / substituição.

O que eu gostaria:

  1. Pesquise por "http"
  2. Selecionar hiperlink inteiro
  3. Transforme em um hiperlink, não alterando o texto âncora
  4. Repita para todas as instâncias de hiperlinks de texto simples

Alguma sugestão?

    
por stupidpoeticjustice 04.04.2016 / 20:26

1 resposta

3

Tente isto:

Sub Hyperlinker()

    Dim Rng As Range

    Set Rng = ActiveDocument.Range
    With Rng.Find
        Do While .Execute(findText:="http:", Forward:=False) = True
            Rng.MoveEndUntil (" ")
            ActiveDocument.Hyperlinks.Add _
                Anchor:=Rng, _
                Address:=Rng.Text, _
                SubAddress:="", _
                ScreenTip:="", _
                TextToDisplay:=Rng.Text
            Rng.Collapse wdCollapseStart
        Loop
    End With

End Sub

Após a substituição de todos, talvez seja necessário salvar e reabrir o documento do Word para que os links comecem a funcionar.

    
por 04.04.2016 / 21:01