|
|
|
date: Fri, 22 Aug 2008 19:27:04 +0100,
group: microsoft.public.word.vba.beginners
back
Re: Replace in table
To: CS,
'
' Trim Text in Tables
'
Sub TrimTextInTables()
Dim oTable As Table
Dim oCell As Cell
Dim sStr As String
For Each oTable In ActiveDocument.Tables
For Each oCell In oTable.Range.Cells
sStr = oCell.Range.Text
If Len(sStr) > 2 Then
sStr = Left(sStr, Len(sStr) - 2)
sStr = Replace(sStr, Chr(160), " ")
sStr = Trim$(sStr)
oCell.Range.Delete
oCell.Range.Text = sStr
End If
Next oCell
Next oTable
End Sub
Please note that the line: sStr = Replace(sStr, Chr(160), " ")
Replaces nonbreaking spaces with a normal space.
You may delete that line, if you wish.
The function Trim removes both leading and trailing spaces.
It trims text in every table.
If you prefer it to work only on one table, then:
'
' Trim Text in a Table
'
Sub TrimTextInATable()
Dim oTable As Table
Dim oCell As cell
Dim sStr As String
If Selection.Information(wdWithInTable) = False Then
MsgBox "The cursor must be positioned in the table."
Exit Sub
End If
Set oTable = Selection.Tables(1)
For Each oCell In oTable.Range.Cells
sStr = oCell.Range.Text
If Len(sStr) > 2 Then
sStr = Left(sStr, Len(sStr) - 2)
sStr = Replace(sStr, Chr(160), " ")
sStr = Trim$(sStr)
oCell.Range.Delete
oCell.Range.Text = sStr
End If
Next oCell
End Sub
Steven Craig Miller
"CS" wrote:
> Steven,
>
> Many, many thanks for this its works a treat.
>
> One quick question, is there a way to remove leading spaces from the
> text entered. Now that I've sorted out the first step I've noticed a few
> records with leading spaces.
>
> Chris
date: Sat, 23 Aug 2008 05:36:01 -0700
author: StevenM stevencraigmiller(at)comcast(dot)net
|
|