Why isn't "Replace" working?

E

Ed

This loops through each cell of each table to replace paragraph marks in
cells with spaces. The looping works fine - the Replace does not! Can
someone please drop-kick me in the right direction?
Ed

Sub ReplParas()

Dim d As Document
Dim t As Table
Dim x As Long
Dim c As Cell
Dim str1 As String, str2 As String

str2 = chr(13)

Set d = ActiveDocument
For x = 1 To d.Tables.Count
Set t = d.Tables(x)
For Each c In t.Range.Cells
str1 = c.Range.Text
str1 = Left(str1, Len(str1) - 2)
Replace str1, str2, " "
c.Range.Text = str1
Next c
Next x

End Sub
 
D

Doug Robbins - Word MVP

The following should do what you want:

Selection.HomeKey wdStory
Selection.Find.ClearFormatting
With Selection.Find
Do While .Execute(FindText:="^p", MatchWildcards:=False,
Wrap:=wdFindStop, Forward:=True) = True
If Selection.Information(wdWithInTable) = True Then
Selection.Text = " "
Selection.Collapse wdCollapseEnd
End If
Loop
End With


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
E

Ed

Thank you, Doug. It worked beautifully (of course!). If you could be so
kind, though, can you see what I was doing wrong with the Replace so I don't
do that again? Or does it just not work well in tables?

Ed
 
K

Klaus Linke

Hi Ed,

Replace is a function, and you'd need to use it like
str1 = Replace(str1, str2, " ")

But Doug's code also has the advantage of keeping any applied formatting,
while deleting and re-inserting the text as you do may loose local font
formatting.

If you keep your code, you might want to use "For each t in d.Tables"
instead of "For x = 1 To d.Tables.Count : Set t = d.Tables(x)" ... it's
faster if you have lots of tables.

Regards,
Klaus
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top