View Full Version : [SOLVED:] leading space
mikewi
12-17-2016, 05:12 PM
Guys I have this code to remove a leading space in a word table. It was originally written to remove leading tabs. Any ideas?
Sub RemoveLeadingSpace()
If ActiveDocument.Range(0, 1).Text = Chr(32) Then
ActiveDocument.Range(0, 1).Delete
End If
With ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = "Chr(32)"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With
End Sub
Paul_Hossler
12-17-2016, 06:48 PM
If by 'Any ideas' you mean why isn't it working, my guess is that
.Text = "Chr(32)"
should be
.Text = Chr(32)
or
.Text = " "
mikewi
12-17-2016, 07:50 PM
Thanks for the reply Paul. It still seems to be removing all spaces and not just the leading spaces.
gmayor
12-17-2016, 10:40 PM
The following macro will remove the leading spaces from all cells in the table the cursor is in
Sub RemoveLeadingSpaces()
'Graham Mayor - http://www.gmayor.com - Last updated - 18/12/2016
Dim oTable As Table
Dim oCell As Cell
Dim oRng As Range
Set oTable = Selection.Tables(1)
For Each oCell In oTable.Range.Cells
With oCell.Range
Set oRng = oCell.Range
oRng.Collapse 1
oRng.MoveEndWhile Chr(32)
oRng.Delete
End With
Next oCell
lbl_Exit:
Set oTable = Nothing
Set oCell = Nothing
Set oRng = Nothing
Exit Sub
End Sub
gmayor
12-18-2016, 05:06 AM
Even quicker
Sub RemoveLeadingSpaces2()
Dim orng As Range
Set orng = Selection.Tables(1).Range
orng.ParagraphFormat.Alignment = wdAlignParagraphCenter
orng.ParagraphFormat.Alignment = wdAlignParagraphLeft
lbl_Exit:
Set orng = Nothing
Exit Sub
End Sub
gmaxey
12-18-2016, 05:58 AM
Graham,
My experience (with Word 2010) is that while your Even quicker macro should work because it is basically exactly what happens if you select cells and Center then left align text, it rarely does work a I don't know why :-(
http://gregmaxey.com/word_tip_pages/vba_nuggets.html Trim Cell Text
I use:
Sub TrimCellText()
Dim oTbl As Table, oCell As Cell, oRng As Range
Set oTbl = Selection.Tables(1)
'For Each oTbl In ActiveDocument.Tables
For Each oCell In oTbl.Range.Cells
Set oRng = oCell.Range
oRng.End = oRng.End - 1
oRng.Text = Trim(oRng.Text)
Next oCell
'Next oTbl
lbl_Exit:
Set oTbl = Nothing: Set oCell = Nothing: Set oRng = Nothing
Exit Sub
End Sub
gmayor
12-18-2016, 07:55 AM
It works in Word 2016.
gmaxey
12-18-2016, 08:04 AM
Graham,
Must not be holding my mouth right or have some setting evoked preventing it, because it doesn't work here in any version of Word. I'm sending you a video :-)
gmayor
12-18-2016, 08:25 AM
Hmmm. I was holding my mouth better when I tested it earlier. I remembered that CTRL+A, CTRL+E, CTRL+L removed leading space (and does here) when I put it in the macro it worked. It still does sometimes, but not enough for it to be a viable option. The keyboard shortcut does still work - as does my original slower macro.
gmaxey
12-18-2016, 08:37 AM
Graham,
"I remembered that CTRL+A, CTRL+E, CTRL+L ..."
Yes, that works here as well. The mystery is that, for whatever reason, a macro recorded of those steps doesn't work here ever.
mikewi
12-18-2016, 03:53 PM
Thanks guys. Great work like usual.
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.