vbams-wordword-table

How replace word tables chr(13)?


I am working on copy and past the word tables to excel. but there are a lot of 'enter' key in word tables. could I know how to replace the the enter key in whole word tables. I am encountering issue" wrong number of argument or invalid property assignment"


Solution

  • You have more than one problem with this code.

    The first is that you are not setting oLookWordDoc to point to a document, so none of the Word code will work.

    Second, you have two variables pointing to the same table, oLookwordTbl and r. You only need one of these.

    Third, you are selecting the table to run Find instead of simply using the Find method of Table.Range.

    Fourth, your find and replacement texts are incorrect.

    The tidied code below will replace the paragraph marks in the table with a space.

    Dim oLookWordDoc As Word.document
    Dim oLookwordTbl As Word.Table
    Dim iRow As Long 'row index
    
    'you need to set oLookWordDoc to point to a document here
    
    'Grab the word table
    Set oLookwordTbl = oLookWordDoc.Tables(1)
    With oLookwordTbl.Range.Find
        .Text = "^p"
        .Replacement.Text = " "
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute Replace:=wdReplaceAll
    End With
    'rows 2 - end
    For iRow = 2 To oLookwordTbl.Rows.Count
        oLookwordTbl.Rows(iRow).Range.Copy
        'Paste
        xWs.Paste
        xWs.Cells(xWs.Rows.Count, 1).End(3).Offset(1).Select
    Next