#1
|
|||
|
|||
Macro to remove spaces between characters
Hi all,
Been Googling this for the last couple of days and have come up with nothing--nada! My problem is this: I have a document that contains about 75% of the words with spaces between the characters like the image attached. Crazy, eh? Well, it certainly begs the question, "how the heck did that happen?" What I have learned from my research is sometimes this can occur in PDF to Word conversions and vice versa. As it's about 150 pages long you can imagine how long it would take to manually take out all the spaces, even with find and replace. Soooooo, I'm looking for a macro that will remove the single spaces between the characters (I can manually remove the doubles between the words with F&R). This is what I've developed so far: Sub RemoveSpaces() ' ' RemoveSpaces Macro ' ' Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting With Selection.Find .Text = "^? ^?" .Replacement.Text = "" .Forward = True .Wrap = wdFindContinue .Format = False .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With Selection.Find.Execute Selection.MoveLeft Unit:=wdCharacter, Count:=1 Selection.MoveRight Unit:=wdCharacter, Count:=1 Selection.Delete Unit:=wdCharacter, Count:=1 End Sub However i can't get it to loop. Please advise. Thank you, David Matthews (not the band ) |
#2
|
|||
|
|||
Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 5/9/2018 Dim oRng As Word.Range Set oRng = ActiveDocument.Range With oRng.Find .Text = " " .Replacement.Text = "***" .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range With oRng.Find .Text = "(*) (*)" .Replacement.Text = "\1\2" .MatchWildcards = True .Execute Replace:=wdReplaceAll End With Set oRng = ActiveDocument.Range With oRng.Find .Text = "***" .Replacement.Text = " " .Execute Replace:=wdReplaceAll End With lbl_Exit: Exit Sub End Sub |
#3
|
|||
|
|||
Oh, my gosh. You just cut my work on this doc about 90%!
It isn't getting everything perfectly, but it functioned on the vast majority! What the devil is that thing doing??? Thank you, thank you, thank you! David Matthews |
#4
|
||||
|
||||
You don't need a macro for this - just a single wildcard Find/Replace, where:
Find = ([! ])^32 Replace = \1
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#5
|
|||
|
|||
Well, based on your sample where words are separated by two spaces, it replaces those two spaces with a temporary marker "***". Then it joined any character separated from any other character with a space. Finally it replaced the temporary marker with a space.
Paul is right, you don't need a macro. You asked for one though. Using Paul's refined construction: Code:
Sub ScratchMacro() 'A basic Word macro coded by Greg Maxey, http://gregmaxey.com/word_tips.html, 5/9/2018 Dim oRng As Word.Range Set oRng = ActiveDocument.Range With oRng.Find .Text = "([! ])^32 " .Replacement.Text = "\1" .MatchWildcards = True .Execute Replace:=wdReplaceAll End With lbl_Exit: Exit Sub End Sub |
#6
|
||||
|
||||
which could be reduced to:
Code:
Sub Demo() ActiveDocument.Range.Find.Execute FindText:="([! ])^32 ", ReplaceWith:="\1", MatchWildcards:=True, Replace:=wdReplaceAll End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#7
|
|||
|
|||
Aye, and further reduced to:
Code:
Sub Demo() ActiveDocument.Range.Find.Execute "([! ]) ", , , True, , , , , , "\1", 2 End Sub |
#8
|
||||
|
||||
Or even:
Code:
Sub Demo(): ActiveDocument.Range.Find.Execute "([! ]) ", , , True, , , , , , "\1", 2: End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#9
|
|||
|
|||
Bloated with needless excess:
Code:
Sub D(): ActiveDocument.Range.Find.Execute "([! ]) ", , , True, , , , , , "\1", 2: End Sub |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
How to remove blank spaces between rows in a table, | Jamal NUMAN | Word | 2 | 04-28-2017 12:59 PM |
A Widlcard or Macro to Properly Remove Spaces Between Commas? | CrossReach | Word | 6 | 01-23-2017 04:28 AM |
Can I remove spaces between only Chinese characters? | sivartnosredna7 | Word | 2 | 01-09-2017 05:33 AM |
How to remove spaces from Cells which are at the end of value? | LearnerExcel | Excel | 2 | 12-19-2016 01:10 PM |
editing text and remove spaces | romanticbiro | Word VBA | 5 | 07-04-2014 07:42 PM |