locked
VBA Word - Search Replace Substitute Variables RRS feed

  • Question

  • Hi friends

     

    I am trying to do some search and replacements in a document.

     

    It is set up like this

     

    Before

    ##ID

    <Test id="1234552233" name="I want this Text 1 none of this until the end">

    ##ID

    <Test id="1234552231233" name="I want this Text 2none of this until the end">

     

    After - Becomes

    I want this Text 1

    <Test id="1234552233" name="I want this Text 1 "  none of this until the end">

     

    I want this Text 2

    <Test id="1234552233" name="I want this Text 2"  none of this until the end">

    Sub SearchReplace()
    
        Dim oRng As Range
        Set oRng = ActiveDocument.Range
        With oRng.Find
            '.Text = "##ID"
            
        'Do While .Execute(Forward:=True) = True
            
            
        Do While .Execute(FindText:="name=" * """", MatchWildcards:=True)
            
        
               ' oRng.Previous.Paragraphs  = "
        
                oRng.Text = 
    
                
                oRng.Collapse wdCollapseEnd
            Loop
        End With
    
        
    End Sub
    I have tried a number of things   - but no luck yet
    any help appreciated, thank you


    Cheers Dan :)


    • Edited by Dan_CS Saturday, March 4, 2017 10:27 PM
    Saturday, March 4, 2017 10:26 PM

Answers

  • Assuming the example is an accurate reflection on the content of your document then

    Option Explicit
    
    Sub ReplaceID()
    'Graham Mayor - http://www.gmayor.com - Last updated - 05/03/2017'
    Dim oRngID As Range
    Dim oRngName As Range
        Set oRngID = ActiveDocument.Range
    
        oRngID.Text = Replace(oRngID.Text, Chr(147), Chr(34))
        oRngID.Text = Replace(oRngID.Text, Chr(148), Chr(34))
    
        With oRngID.Find
            Do While .Execute(FindText:="??ID", MatchWildcards:=True)
                oRngID.MoveEnd wdParagraph, 2
                Set oRngName = oRngID.Paragraphs(2).Range
                With oRngName
                    .Start = .Start + InStr(1, .Text, Chr(34))
                    .Start = .Start + InStr(1, .Text, Chr(34))
                    .Start = .Start + InStr(1, .Text, Chr(34))
                    .End = .Start + InStr(1, .Text, Chr(34))
                    .MoveEndWhile Chr(34) & Chr(32), wdBackward
                End With
                oRngID.End = oRngID.Paragraphs(1).Range.End - 1
                oRngID.Text = oRngName.Text
                oRngID.Collapse 0
            Loop
        End With
    lbl_Exit:
        Set oRngID = Nothing
        Set oRngName = Nothing
        Exit Sub
    End Sub
    


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Dan_CS Sunday, March 5, 2017 2:18 PM
    Sunday, March 5, 2017 5:19 AM

All replies

  • Assuming the example is an accurate reflection on the content of your document then

    Option Explicit
    
    Sub ReplaceID()
    'Graham Mayor - http://www.gmayor.com - Last updated - 05/03/2017'
    Dim oRngID As Range
    Dim oRngName As Range
        Set oRngID = ActiveDocument.Range
    
        oRngID.Text = Replace(oRngID.Text, Chr(147), Chr(34))
        oRngID.Text = Replace(oRngID.Text, Chr(148), Chr(34))
    
        With oRngID.Find
            Do While .Execute(FindText:="??ID", MatchWildcards:=True)
                oRngID.MoveEnd wdParagraph, 2
                Set oRngName = oRngID.Paragraphs(2).Range
                With oRngName
                    .Start = .Start + InStr(1, .Text, Chr(34))
                    .Start = .Start + InStr(1, .Text, Chr(34))
                    .Start = .Start + InStr(1, .Text, Chr(34))
                    .End = .Start + InStr(1, .Text, Chr(34))
                    .MoveEndWhile Chr(34) & Chr(32), wdBackward
                End With
                oRngID.End = oRngID.Paragraphs(1).Range.End - 1
                oRngID.Text = oRngName.Text
                oRngID.Collapse 0
            Loop
        End With
    lbl_Exit:
        Set oRngID = Nothing
        Set oRngName = Nothing
        Exit Sub
    End Sub
    


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by Dan_CS Sunday, March 5, 2017 2:18 PM
    Sunday, March 5, 2017 5:19 AM
  • Hi Graham,

     

    This is spectacular.

     

    How did you know to  fine tune it like that exactly.

     

    I tried so many wild card searches and regex, it was a bit of  a night mare to say the least, not being able to isololate the text.

    I also tried some powershell but that was really hard :(

     

         With oRngName                

       .Start = .Start + InStr(1, .Text, Chr(34))              

       .Start = .Start + InStr(1, .Text, Chr(34))         

        .Start = .Start + InStr(1, .Text, Chr(34))         

        .End = .Start + InStr(1, .Text, Chr(34))            

       .MoveEndWhile Chr(34) & Chr(32), wdBackward

     

     

     I need to focus on character ranges and moving until a position.

     

    But you have made my day  -  I am so relieved that something has worked!

     

    Great man Graham

     

    Thank you so much  & Thank you for your time

     

    Have  a great Sunday :)


    Cheers Dan :)


    • Edited by Dan_CS Sunday, March 5, 2017 2:37 PM
    Sunday, March 5, 2017 2:18 PM