In a document I need to hyperlink certain text that is found in selected text. I thought this was working but it was just a fluke. It was actually going through the whole document.
The text it finds and makes to Hyperlinks is any number prefixed with a G or H and followed by 1-4 digits. (Not relevant to the issue, but just saying what I am doing)
The below macro works but does not limit itself to my selected text. For example if I had selected the first sentence only, it carries past what I thought I had set as the end of the selection and processes the remainder of the document.
So it could be blah blah blah G50 blah blah
blah blah blah G5 blah blah blah. Blah blah blah G540 blah blah
blah blah blah G45 blah blah blah.
Private Sub CommandButton16StrongsLinksOnSelectedText_Click()
Dim output As String
Dim Rng As Range
Dim searchstring As String
Dim EndString As String
Dim Id As String
Dim Link As String
Dim Count As Integer
''''Application.StatusBar = "Making Strong's Links, Please be Patient"
''''InSelection = False
''''If selection.Type = wdSelectionIP Then InSelection = True
''''
''''If InSelection = True Then
''''
'''' MsgBox ("select some text")
'''' Exit Sub
''''End If
Set Rng = ActiveDocument.Range(Start:=selection.Start, End:=selection.End)
searchstring = "[HG][0-9]{1,4}>"
With Rng.Find
.Font.Underline = wdUnderlineNone
.MatchWildcards = True
.MatchCase = True
selection.Find.Font.Underline = wdUnderlineNone
Do While .Execute(FindText:=searchstring, Forward:=False) = True
Rng.MoveStartUntil ("[HG][0-9]{1,4}")
Rng.MoveEndUntil ("")
Id = Trim(Rng.text)
'''' If CheckBox11HyperlinkBold.Value = True Then
'''' Rng.Font.Bold = True
'''' End If
'''' If CheckBox12HyperlinkBlue.Value = True Then
'''' Rng.Font.ColorIndex = wdBlue
'''' End If
Link = "tw://[strong]?" & Id
ActiveDocument.Hyperlinks.Add Anchor:=Rng, _
Address:=Link, _
SubAddress:="", ScreenTip:="", TextToDisplay:=Rng
Rng.Collapse wdCollapseStart
Count = Count + 1
Loop
End With
MsgBox "Parsed " & Count & " Strong's number/s"
Application.StatusBar = "Parsed " & Count & " Strong's number/s"
outptut = "Parsed " & Count & " Strong's number/s."
End Sub