Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-30-2014, 07:17 PM
treva31 treva31 is offline Losing formatting using .Text to replace Windows 7 64bit Losing formatting using .Text to replace Office 2010 32bit
Novice
Losing formatting using .Text to replace
 
Join Date: Sep 2014
Posts: 2
treva31 is on a distinguished road
Default Losing formatting using .Text to replace

Hi,



I have this little sub that removes leading and trailing spaces from text in cells of all my tables.

It works great but it turns any coloured text to black.

Is there a way to keep the formatting?

Thanks.


Code:
Sub RemoveSpaces() 
    Dim oRng As Range 
    Dim oTable As Table 
    Dim acell As Cell 
    For Each oTable In ActiveDocument.Tables 
        With oTable 
            For Each acell In oTable.Range.Cells 
                Set oRng = acell.Range 
                oRng.End = oRng.End - 1 
                oRng.Text = Trim(oRng.Text) 
            Next acell 
        End With 
    Next oTable 
End Sub

Last edited by macropod; 09-30-2014 at 09:15 PM. Reason: Added correct code tags & formatting
Reply With Quote
  #2  
Old 09-30-2014, 09:21 PM
macropod's Avatar
macropod macropod is offline Losing formatting using .Text to replace Windows 7 64bit Losing formatting using .Text to replace Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Try:
Code:
Sub RemoveSpaces()
    Dim oTable As Table, acell As Cell, oRng As Range
    For Each oTable In ActiveDocument.Tables
        With oTable
            For Each acell In oTable.Range.Cells
                Set oRng = acell.Range
                With oRng
                  .End = .End - 1
                  While .Characters.Last = " "
                    .Characters.Last = ""
                  Wend
                  While .Characters.First = " "
                    .Characters.First = ""
                  Wend
                End With
            Next acell
        End With
    Next oTable
End Sub
PS: When posting code, please use the code tags. They're on the 'Go Advanced' tab at the bottom of this screen. Your post tried to replicate them as <CODE></CODE>, whereas the tags here use [ and ] instead of < and >.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 09-30-2014, 10:31 PM
treva31 treva31 is offline Losing formatting using .Text to replace Windows 7 64bit Losing formatting using .Text to replace Office 2010 32bit
Novice
Losing formatting using .Text to replace
 
Join Date: Sep 2014
Posts: 2
treva31 is on a distinguished road
Default

Thats perfect thank you!

Yea sorry the code button doesnt seem to be working for me.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Losing formatting using .Text to replace replace letters by font without losing format romanticbiro Word VBA 31 03-16-2017 04:00 PM
Find and Replace, Formatting, Formatting Formula kjxavier Word 1 07-04-2014 09:11 AM
Losing formatting using .Text to replace How to stop losing formatting MuffinRider Word 1 02-28-2014 04:27 AM
Losing formatting using .Text to replace Word Documents Losing Formatting/Styles When Reopened jleary Word 2 02-05-2013 12:27 PM
Replace formatting with text eyehefbee Word 2 11-09-2009 02:41 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:16 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft