PDA

View Full Version : Replacing characters from right side needs to preserve formatting



slyguy
03-23-2015, 02:25 PM
I had to strip some characters from right side of table cells as it causes text in the table to run in on our conversion. This works, except, it causes formatting to get dropped. Is there any way to preserve formatting?

TIA?

This is what I have...


...
'loop through all tables
For itable = 1 To ActiveDocument.Tables.Count


'loop through all rows
For Row = 1 To ActiveDocument.Tables(itable).Rows.Count

'loop through all cols
For Col = 1 To ActiveDocument.Tables(itable).Columns.Count


'grab text
strCellText = ActiveDocument.Tables(itable).Cell(Row, Col).Range.Text


If Err.Number = 0 Then


Dim a, b
a = Chr(9) & Chr(13) & Chr(7)
b = Chr(7)
If Right(strCellText, 3) = a Then
strCellText = Replace(strCellText, a, b)

ActiveDocument.Tables(itable).Cell(Row, Col).Range.Text = strCellText
End If

Else
Err.Clear
End If

Next 'next/End Col loop


Next 'next/End Row loop


Next 'next/end Table loop

gmayor
03-25-2015, 04:07 AM
The following should do the job



Dim iTable As Long
Dim Row As Long
Dim col As Long
Dim oCell As Range
Dim a As String
a = Chr(9) & Chr(13)
'loop through all tables
For iTable = 1 To ActiveDocument.Tables.Count
'loop through all rows
For Row = 1 To ActiveDocument.Tables(iTable).Rows.Count
'loop through all cols
For col = 1 To ActiveDocument.Tables(iTable).Columns.Count
Set oCell = ActiveDocument.Tables(iTable).Cell(Row, col).Range
oCell.End = oCell.End - 1
If Right(oCell.Text, 2) = a Then
oCell.Start = oCell.End - 2
oCell.Delete
End If
Next 'next/End Col loop
Next 'next/End Row loop
Next 'next/end Table loop