Consulting

Results 1 to 3 of 3

Thread: Word RegEx - Find String - Move 3 Words to the Left Between Quotes

  1. #1
    VBAX Regular
    Joined
    Dec 2015
    Location
    UK
    Posts
    31

    Word RegEx - Find String - Move 3 Words to the Left Between Quotes

    Hello,

    to all again. I hope every one is having a great relaxing weekend.

    I have come back to seek some advice on a regex problem - I assume.

    My document has these website code lines hidden somewhere. I need to copy and paste 3 words to the left between some quote marks, and remove the spaces.


    Here is an example:

    <a href="# "> 1.2 Science Exam - 2015 Progress </a>
    <a href="# "> 5.5 Algebra Equations - 2015 Progress </a>


    End Result

    <a href="#1.2ScienceExam"> 1.2 Science Exam - 2015 Progress </a>
    <a href="#5.5AlgebraEquations"> 5.5 Algebra Equations - 2015 Progress </a>


    My document has a lot of these hidden somewhere - as you can imagine I have to search and find these little lines.

    Then for each one copy and paste to the left - before you know it - I am getting tired fast, as I have lots more in my folder that need the same yikes.

    I recorded a macro - all it spewed up was copy paste and move back wards - it was as good as a lemon in winter.

    My Attempt

    Dim StrOld As String, StrNew As String
    Dim RngFind As Range, RngTxt As Range, i As Long
    
    StrOldRegEx = "<ahref \b[^>]*>(.*?)</a> "
    
    
    StrOldRegEx = "     <a href="# ">  *   </a>    "      ' Another try
    
    
    Set RngTxt = Selection.Range
    
    For i = 0 To UBound(split(StrOld, ","))
    Set RngFind = RngTxt.Duplicate
    With RngFind.Find
    
      While .Find.Execute
       Selection.MoveLeft Unit:=wdCharacter, Count:=4
        Selection.TypeBackspace
        Selection.MoveLeft Unit:=wdCharacter, Count:=7
        Selection.TypeBackspace
        Selection.Copy
        Selection.PasteAndFormat (wdFormatOriginalFormatting)
    I wish the word macro recorder was more helpful - so i could work out how to search for things and then perform an action.

    Any help really appreciated

    thank you in advance to the great experts

    Saphire

  2. #2
    You cannot achieve this with the macro recorder. Assuming that the quoted strings are an accurate representation, then provided their are no similar, but unrelated strings the document the following should work:
    Option Explicit
    
    Sub FormatReferences()
    Dim orng As Range, oBold As Range
    Dim strText As String
        Set orng = ActiveDocument.Range
        With orng.Find
            Do While .Execute(FindText:="<a href=" & Chr(34) & "# " & Chr(34) & "> ")
                orng.MoveEndUntil "-"
                strText = Mid(orng.Text, 14, Len(orng.Text))
                strText = Replace(strText, Chr(32), "")
                orng.Text = Replace(orng.Text, Chr(32) & Chr(34) & "> ", strText & Chr(34) & "> ")
                Set oBold = orng
                oBold.MoveStartUntil "#"
                oBold.Start = oBold.Start + 1
                oBold.MoveEndUntil Chr(34), wdBackward
                oBold.Font.Bold = True
                orng.Collapse 0
            Loop
        End With
    lbl_Exit:
        Exit Sub
    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
    Dec 2015
    Location
    UK
    Posts
    31
    Dear Graham,

    I am so excited, thank you!!

    You are a talented genius programming guru.

    It worked like sweet candy.

    As you can imagine I found it really hard making friends with the regex - it was driving me mad.

    thank you so much for your generosity - this is my favorite macro





    Really Grateful

    Have a wonderful holiday!

    Saphire
    xo



    Please mark as solved

Posting Permissions

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