none
Cant Find and Replace Value in a table RRS feed

  • Question

  • Sub Macro2()
    
    Dim x As String
    Dim y As String
    
     x = ActiveDocument.Tables(1).Rows(2).Cells(2).Range.Text
     y = ActiveDocument.Tables(1).Rows(2).Cells(3).Range.Text
     
    MsgBox x
    MsgBox y
      
    With ActiveDocument.Content.Find
        .Text = x
        .ClearFormatting
        .Forward = True
        .Execute
        If .Found = True Then .Replacement.Text = y
    End With
     
    End Sub
    

    Dear All,

    I am trying to write a small program to help my co-worker to spend up the task. 

    Here is the problem

    Variable

    Find

    Replace

    ContractNo

    Test 1

    Test 2

    Here is the table I am working on Find the all "Test 1" in the document and replace all "Test 1" by "Test 2" 

    So I write the above code to do what I want to do. However, I dont know where went wrong. Every time I run the program, but the value still remain the same. 

    If you noticed I have two Msgbox to locate my the text selection is correct, I am sure I am locating the cell correctly.

    But, I still cant replace the value.

    Please help me.

    Monday, April 30, 2018 4:34 AM

All replies

  • Take a look at the following and see if it helps you see the sequence that needs to be performed.

        With ActiveDocument.Content.Find
            .ClearFormatting
            .Text = x
            .Replacement.ClearFormatting
            .Replacement.Text = Y
            .Execute Replace:=wdReplaceAll, Forward:=True, _
            Wrap:=wdFindContinue
        End With


    Kind Regards, Rich ... http://greatcirclelearning.com

    Monday, April 30, 2018 10:11 PM