Home > Software engineering >  How replace word tables chr(13)?
How replace word tables chr(13)?

Time:09-23

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"

Dim oLookWordDoc As Word.document
Dim oLookwordTbl As Word.Table
Dim iRow As Long 'row index
Dim iCloum As Long
Dim r As Object  'As Word.Range
'Grab the word table
Set oLookwordTbl = oLookWordDoc.Tables(1)
Set r = oLookWordDoc.Tables(1)
ActiveDocument.Tables(1).Range.Select
With Selection.Find
.Text = ">P"
.Replacement.Text = "^p"
.Forward = True
.wrap = wdFindAsk
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
'rows 2 - end
For iRow = 2 To r.Rows.Count
 r.Rows(iRow).Range.Copy
'Paste
xWs.Paste
xWs.Cells(xWs.Rows.Count, 1).End(3).Offset(1).Select
Next
End sub

enter image description here

CodePudding user response:

With Selection.find references the Excel selection object. But you want to work with the Word selection object.

Do you have a variable for the word application, e.g. appWord? Use this: With appWord.Selection.find

If not With oLookWordDoc.parent.selection.find should work

CodePudding user response:

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
  • Related