Consulting

Results 1 to 6 of 6

Thread: Search and replace only selected text

  1. #1
    VBAX Regular
    Joined
    Mar 2020
    Posts
    48
    Location

    Search and replace only selected text

    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

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Mar 2020
    Posts
    48
    Location
    Id = Trim(Rng.text)

    was that wrong. It did work.

  4. #4
    VBAX Regular
    Joined
    Mar 2020
    Posts
    48
    Location
    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?

  5. #5
    VBAX Regular
    Joined
    Mar 2020
    Posts
    48
    Location
    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.

  6. #6
    Sorry I missed the line Id = Trim(Rng.text)
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •