PDA

View Full Version : [SOLVED:] why text format changed ????



baset
03-03-2016, 12:05 PM
I'm using this macro to remove space at end of table cell; it works fine but the format of text changed; WHY ?



Sub CleanCellEndSpaces()'Define variables (that is, containers for information)
Dim aTable As Table
Dim aCell As Cell
Dim aRow As Row
Dim aColumn As Column
Dim aRange As Range 'A specific area of the document
Dim aLen As Integer 'A number
Dim LastChar As String 'A string of characters (text)
Dim Tracking As Boolean 'True or False
Tracking = ActiveDocument.TrackRevisions 'Get setting of revision tracking
ActiveDocument.TrackRevisions = False 'Turn off revision tracking
On Error Resume Next 'In case of tables with "vertically merged" cells
'Cycle through tables, rows, and cells
For Each aTable In ActiveDocument.Tables
For Each aRow In aTable.Rows
For Each aCell In aRow.Cells
CheckAgain:
Set aRange = aCell.Range 'Set aRange variable to the contents of the current cell
aRange.End = aRange.End - 1 'Don’t include the end-of-cell marker
aLen = Len(aRange.Text) 'Get the length of the cell’s text
aString = aRange.Text 'Assign the text to a variable
LastChar = Right(aString, 1) 'Get the last character of the text
If LastChar = " " Then 'If the last character is a space
aRange.Text = Left(aRange.Text, aLen - 1) 'Set the text to be itself minus the trailing space
GoTo CheckAgain 'Go back and check for another space (there may be several)
End If
Next aCell
Next aRow
Next aTable
ActiveDocument.TrackRevisions = Tracking 'Set revision tracking back to its original state
End Sub



From:
15549

To:
15550

gmaxey
03-03-2016, 02:02 PM
Because you coded that way:

aRange.Text = Left(aRange.Text, aLen - 1)

Defines the range text as the existing range text - 1 character. But text only not any attributes applied to it.


Sub CleanCellEndSpaces() 'Define variables (that is, containers for information)
Dim oWS As Range
Dim oTbl As Table
Dim oCell As Cell
Dim oRng As Range 'A specific area of the document
For Each oTbl In ActiveDocument.Tables
For Each oCell In oTbl.Range.Cells
Set oRng = oCell.Range 'Set oRng variable to the contents of the current cell
oRng.End = oRng.End - 1 'Don’t include the end-of-cell marker
Do While oRng.Characters.Last = " "
Set oWS = oRng.Characters.Last
oWS.Collapse wdCollapseStart
oWS.Delete Unit:=wdCharacter, Count:=1
Loop
Next oCell
Next oTbl
lbl_Exit:
Exit Sub
End Sub

baset
03-04-2016, 11:23 AM
Perfect sit; it works fine this time.

Have a good time always :hi: