PDA

View Full Version : [SOLVED:] Search and replace only selected text



JPG
03-26-2020, 01:52 AM
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

gmayor
03-26-2020, 02:15 AM
Your macro does not define 'Id'?


Private Sub CommandButton16StrongsLinksOnSelectedText_Click()
Dim output As String
Dim Rng As Range
Dim searchstring As String
Dim Id As String
Dim Link As String, strOutput As String
Dim Count As Integer

If Len(Selection.Range) = 0 Then
Beep
MsgBox ("Select some text")
Exit Sub
End If

Set Rng = Selection.Range

searchstring = "[HG][0-9]{1,4}>"
With Rng.Find
.Font.Underline = wdUnderlineNone
.MatchWildcards = True
Selection.Find.Font.Underline = wdUnderlineNone
Count = 0
Do While .Execute(findText:=searchstring) = True
If Rng.InRange(Selection.Range) Then
Link = "tw://[strong]?" & Id
ActiveDocument.Hyperlinks.Add Anchor:=Rng, _
Address:=Link, _
SubAddress:="", _
ScreenTip:="", _
TextToDisplay:=Rng
Count = Count + 1
Rng.Collapse 0
End If
Loop
End With

MsgBox "Parsed " & Count & " Strong's number/s"
Application.StatusBar = "Parsed " & Count & " Strong's number/s"
strOutput = "Parsed " & Count & " Strong's number/s."
Set Rng = Nothing
End Sub

JPG
03-26-2020, 02:24 AM
Id = Trim(Rng.text)

was that wrong. It did work.

JPG
03-26-2020, 02:35 AM
your code works, but it appears to parse the selection and then the mouse pointer shows busy and flashes for a few seconds like it is doing something else. Why is this so?

JPG
03-26-2020, 02:41 AM
I changed this line and I don't get the flashing pointer.


Do While .Execute(FindText:=searchstring, Forward:=False) = True

Thanks for your guidance, very much appreciated.

gmayor
03-26-2020, 03:51 AM
Sorry I missed the line Id = Trim(Rng.text) :doh: