0
votes

I have a number of lengthy Word 2010 documents riddled with acronyms and many of the same acronyms are common to all these docs. I also have a vertical list of these acronyms that are in hyperlink format within each document in a table format (at then end of each document).

e.g. Hyperlink list:

AA1
AA2
AA3
...

I have very little Word 2010 macro experience but wonder if it's possible to have a macro that would work like this:

  1. Within any given doc, when I place my cursor on the top of the list (i.e. at the cell with hyperlink contents "AA1"), it would select the contents of this first cell, then copy it to clipboard, then do a global replace (using ^c I suppose) of all text formatted words "AA1" within the document with the hyperlink formatted word "AA1" (assuming it found the word to replace).

  2. It would then move down to the second cell with a hyperlink content of "AA2" and replace all "AA2" text within the document with the hyperlinked "AA2" value etc. until all the hyperlinked words in the hyperlinked list have replaced all the text words within the document.

Is this do-able?

1

1 Answers

1
votes

if i understand your mean correct [Question was not clear and visual] this is the base code that you need:

Sub replaceHyperLink()
Dim searchText As String
searchText = "AA1"
    Selection.Find.ClearFormatting
    With Selection.Find
        .text = ""
        .Replacement.text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchKashida = False
        .MatchDiacritics = False
        .MatchAlefHamza = False
        .MatchControl = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

Do
    With Selection.Find
    .text = searchText
    End With

    Selection.Find.Execute
If Selection.Find.Found = True Then
    ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _
        "stackoverflow.com/", SubAddress:="", ScreenTip:="", TextToDisplay:=searchText
Else
Exit Do
End If
Loop
End Sub
  • you must do it in loop for AA2,AA3,....
  • if you want to hyperlink just one column of table, select that column and then copy it in new document (code for do that is simple) then do operation and again paste it in selected column.

and if you want to get text from cells:

Dim cellText As String
For i = 1 To ActiveDocument.Tables(1).Rows.Count
ActiveDocument.Tables(1).Rows(i).Cells(3).Select
cellText = Selection.text
'do what you want with text
Next i